2018/1/17
第二十九天:卡米狗發公告
今天我們要作的是主動傳訊息的功能。
目前我們用到的都只是回覆訊息的功能:
# 認識 Push Message API
```
# 傳送訊息到 line
def reply_to_line(reply_text)
return nil if reply_text.nil?
# 取得 reply token
reply_token = params['events'][0]['replyToken']
# 設定回覆訊息
message = {
type: 'text',
text: reply_text
}
# 傳送訊息
line.reply_message(reply_token, message)
end
```
上面這個函數是我們之前寫好的 `reply_to_line` 函數,裡面的最後一行:
```
line.reply_message(reply_token, message)
```
這是在呼叫 line 提供給我們的回覆訊息函數,而 line 也有提供讓我們主動發訊息的函數:
```
response = line.push_message(channel_id, message)
```
我們需要傳遞 channel_id,告訴 Line 誰應該收到這個訊息,channel_id 就是 userId, groupId 或 roomId。所以我們需要一個資料模型去保存所有頻道的 channel_id。
文件參考在這裡:[https://developers.line.me/en/docs/messaging-api/reference/#send-push-message](https://developers.line.me/en/docs/messaging-api/reference/#send-push-message)
# 保存所有頻道
### 建立資料模型
```
rails g model channel channel_id
```
建立一個資料表叫作 channel,裡面有個欄位叫作 channel_id。
### 資料庫遷移
```
rails db:migrate
```
bj4
### 儲存頻道
在主程式中加入一行:
```
Channel.create(channel_id: channel_id)
```
如果你覺得是這樣寫,那你就錯了,因為這樣會導致相同的資料會一直被存進去,到時候你發公告,同一個人就會收到超多次。
```
Channel.find_or_create_by(channel_id: channel_id)
```
先看有沒有相同的資料,如果已經有資料的話就不寫入。如果沒有資料才作寫入。這邊有詳細的說明:[https://rails.ruby.tw/active_record_querying.html#find-or-create-by](https://rails.ruby.tw/active_record_querying.html#find-or-create-by)
加入後的主程式長這樣:
```
def webhook
# 紀錄頻道
Channel.find_or_create_by(channel_id: channel_id)
# 學說話
reply_text = learn(channel_id, received_text)
# 關鍵字回覆
reply_text = keyword_reply(channel_id, received_text) if reply_text.nil?
# 推齊
reply_text = echo2(channel_id, received_text) if reply_text.nil?
# 記錄對話
save_to_received(channel_id, received_text)
save_to_reply(channel_id, reply_text)
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
```
接下來要作一個後台網頁去發這個公告。我們會需要兩個 Action,一個 get new Action 用來顯示發公告的後台頁面,另一個 post create Action 用來接收發公告訊息的請求。
# 管理後台
這次我們手動新增,不使用產生器。
### 加 Route
修改 `config/routes.rb`,新增一行:
```
resources :push_messages, only: [:new, :create]
```
這是加入一組資源,但我們只使用其中的 new 和 create。
### 加 Controller
在 `app/controllers` 資料夾下建立一個叫作 `push_messages_controller.rb` 的檔案:
```
class PushMessagesController < ApplicationController
before_action :authenticate_user!
# GET /push_messages/new
def new
end
# POST /push_messages
def create
end
end
```
我們檢查使用者必須先登入,然後開了兩個空的 Action,之後再回頭來改。
### 加 View
我們要在 `app/views/push_messages` 下新增一個檔案 `new.html.erb`。
這是 `new.html.erb` 所需要的全部程式碼:
```
<%= form_with(url: '/push_messages', local: true) do |form| %>
<%= text_area_tag 'text' %>
<%= submit_tag "送出" %>
<% end %>
```
一個表單的開始是 `<%= form_with ..... do ... %>`,結束是 `<% end %>`。表單預設是用 post 方法,所以就不用特別寫出來。
`<%= text_area_tag 'text' %>` 是輸入文字框。
`<%= submit_tag "送出" %>` 則是送出按鈕。
如果要了解更多的話可以參考:[Action View 表單輔助方法](https://rails.ruby.tw/form_helpers.html)
### 改 Controller
我們在接收到請求之後要作發訊息的動作:
```
def create
text = params[:text]
Channel.all.each do |channel|
push_to_line(channel.channel_id, text)
end
redirect_to '/push_messages/new'
end
```
`text = params[:text]` 這是取得剛剛在輸入文字框填的文字
`Channel.all.each do |channel|` ... `end` 這段是指我們想要對每一個 channel 作一些事情。
`push_to_line(channel.channel_id, text)` 這是說我們要主動發訊息 `text` 給頻道 `channel.channel_id`,這個函數我們待會才會寫。
### push_to_line
```
# 傳送訊息到 line
def push_to_line(channel_id, text)
return nil if channel_id.nil? or text.nil?
# 設定回覆訊息
message = {
type: 'text',
text: text
}
# 傳送訊息
line.push_message(channel_id, message)
end
```
長得跟之前的 `reply_to_line` 有 87% 像,就不解釋了。
### 對一下程式碼
完整的 `push_messages_controller.rb` 應該長這樣:
```
require 'line/bot'
class PushMessagesController < ApplicationController
before_action :authenticate_user!
# GET /push_messages/new
def new
end
# POST /push_messages
def create
text = params[:text]
Channel.all.each do |channel|
push_to_line(channel.channel_id, text)
end
redirect_to '/push_messages/new'
end
# 傳送訊息到 line
def push_to_line(channel_id, text)
return nil if channel_id.nil? or text.nil?
# 設定回覆訊息
message = {
type: 'text',
text: text
}
# 傳送訊息
line.push_message(channel_id, message)
end
# Line Bot API 物件初始化
def line
@line ||= Line::Bot::Client.new { |config|
config.channel_secret = '9160ce4f0be51cc72c3c8a14119f567a'
config.channel_token = '2ncMtCFECjdTVmopb/QSD1PhqM6ECR4xEqC9uwIzELIsQb+I4wa/s3pZ4BH8hCWeqfkpVGVig/mIPDsMjVcyVbN/WNeTTw5eHEA7hFhaxPmQSY2Cud51LKPPiXY+nUi+QrXy0d7Hi2YUs65B/tVOpgdB04t89/1O/w1cDnyilFU='
}
end
end
```
# 發布和測試
自己試試看,你們需要練習自己解決卡關,要養成看 log 的習慣。
# 關於怎麼做鬧鐘
你需要把主動發訊息這件事情加入排程,請參考:[Active Job 基礎](https://rails.ruby.tw/active_job_basics.html#%E4%BB%BB%E5%8B%99%E6%8E%92%E7%A8%8B) 以及 [Delayed Job (DJ)](https://devcenter.heroku.com/articles/delayed-job)。
自己摸這個要有花上一週的心理準備,加油加油加油,你是最棒的,耶!
# 本日重點
- 學會記錄頻道
- 學會主動發訊息
- 學會寫 view 的表單
沒意外的話,明天就講怎麼查天氣。
2018/1/16
第二十八天:建立管理後台
應觀眾要求,今天我們作一個管理後台,讓我們可以在網頁上管理關鍵字。
在開始之前,先大概說明一下今天要學習的範圍有哪些:
- 網頁的呈現需要使用 HTML 和 CSS
- 既然是後台,就要作登入功能
我們作的網站到目前為止沒有碰過任何的 HTML 和 CSS,突然要寫個管理後台也許會很吃力。不過還好是作後台,不需要多美觀。
# 使用產生器製作後台
幸好 Rails 有一個內建指令直接生成網頁,不一定要自己寫。
指令是 `rails generate scaffold 資料模型名稱 和欄位們`
```
rails g scaffold keyword_mapping channel_id keyword message --skip
```
後面的 `--skip` 是指定當發生衝突時應該略過。衝突的意思是指 rails 想新增一個檔案,剛好在目錄裡已經有個同名的檔案。
```
D:\只要有心,人人都可以作卡米狗\ironman>rails g scaffold keyword_mapping channel_id keyword message --skip
invoke active_record
skip db/migrate/20180115144538_create_keyword_mappings.rb
identical app/models/keyword_mapping.rb
invoke test_unit
identical test/models/keyword_mapping_test.rb
skip test/fixtures/keyword_mappings.yml
invoke resource_route
route resources :keyword_mappings
invoke scaffold_controller
create app/controllers/keyword_mappings_controller.rb
invoke erb
create app/views/keyword_mappings
create app/views/keyword_mappings/index.html.erb
create app/views/keyword_mappings/edit.html.erb
create app/views/keyword_mappings/show.html.erb
create app/views/keyword_mappings/new.html.erb
create app/views/keyword_mappings/_form.html.erb
invoke test_unit
create test/controllers/keyword_mappings_controller_test.rb
invoke helper
create app/helpers/keyword_mappings_helper.rb
invoke test_unit
invoke jbuilder
create app/views/keyword_mappings/index.json.jbuilder
create app/views/keyword_mappings/show.json.jbuilder
create app/views/keyword_mappings/_keyword_mapping.json.jbuilder
invoke test_unit
create test/system/keyword_mappings_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/keyword_mappings.coffee
invoke scss
create app/assets/stylesheets/keyword_mappings.scss
invoke scss
create app/assets/stylesheets/scaffolds.scss
D:\只要有心,人人都可以作卡米狗\ironman>
```
以下說明到底生成了什麼東西。
# 生成 Routes
他會在 `config/routes.rb` 生成一個 resource:
```
resources :keyword_mappings
```
這是資源(resource),提供一個資源的存取所需要的網址和 Controller 的對應。
這行會生成 8 組網址與 7 個 Controller Action 的對應,可以使用 `rails routes` 觀察:
```
D:\只要有心,人人都可以作卡米狗\ironman>rails routes
Prefix Verb URI Pattern Controller#Action
keyword_mappings GET /keyword_mappings(.:format) keyword_mappings#index
POST /keyword_mappings(.:format) keyword_mappings#create
new_keyword_mapping GET /keyword_mappings/new(.:format) keyword_mappings#new
edit_keyword_mapping GET /keyword_mappings/:id/edit(.:format) keyword_mappings#edit
keyword_mapping GET /keyword_mappings/:id(.:format) keyword_mappings#show
PATCH /keyword_mappings/:id(.:format) keyword_mappings#update
PUT /keyword_mappings/:id(.:format) keyword_mappings#update
DELETE /keyword_mappings/:id(.:format) keyword_mappings#destroy
kamigo_eat GET /kamigo/eat(.:format) kamigo#eat
kamigo_request_headers GET /kamigo/request_headers(.:format) kamigo#request_headers
kamigo_request_body GET /kamigo/request_body(.:format) kamigo#request_body
kamigo_response_headers GET /kamigo/response_headers(.:format) kamigo#response_headers
kamigo_response_body GET /kamigo/response_body(.:format) kamigo#show_response_body
kamigo_sent_request GET /kamigo/sent_request(.:format) kamigo#sent_request
kamigo_webhook POST /kamigo/webhook(.:format) kamigo#webhook
```
7 個 Action 分別為 index, create, new, edit, show, update, destroy,接下來說明各個 Action 的功能:
以下屬於 GET request,這些都是網頁:
- index:列表頁
- new:新增資料頁
- show:檢視資料頁
- edit:編輯資料頁
以下非 GET request,都是請求資料變更:
- create:請求新增資料
- update:請求更新資料
- destroy:請求刪除資料
# 生成 Controller
生成了一個 Controller 在:`app/controllers/keyword_mappings_controller.rb`。7 個對應的 Action 都寫好了,這裡就不多介紹。
# 生成 View
生成了一整個資料夾的 View,其中最重要的 4 個:
```
app/views/keyword_mappings/index.html.erb
app/views/keyword_mappings/edit.html.erb
app/views/keyword_mappings/show.html.erb
app/views/keyword_mappings/new.html.erb
```
這就是那些 GET request 會用到的網頁檔,也都寫好了。
# 實測
既然都寫好了就來試用看看,先執行網頁伺服器:
```
rails s
```
然後開啟網頁 [http://localhost:3000/keyword_mappings](http://localhost:3000/keyword_mappings):
### index 列表頁

### new 新增資料頁

隨便亂填:

### show 檢視資料頁

所以其實不考慮美觀性的話,其實後台只要一個指令就完成了。
# 建立登入功能
使用知名套件 [devise](https://github.com/plataformatec/devise) 來作。
我相信現在的你如果沒有英文閱讀障礙,應該已經能看懂這個[使用說明](https://github.com/plataformatec/devise#getting-started)。
總而言之先在 Gemfile 加這行:
```
gem 'devise'
```
然後在小黑框打 `bundle install` 安裝套件。
裝好之後使用 devise 提供的產生器指令進行初始化: `rails generate devise:install`。
```
D:\只要有心,人人都可以作卡米狗\ironman>rails generate devise:install
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================
Some setup you must do manually if you haven't yet:
1. Ensure you have defined default url options in your environments files. Here
is an example of default_url_options appropriate for a development environment
in config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
In production, :host should be set to the actual host of your application.
2. Ensure you have defined root_url to *something* in your config/routes.rb.
For example:
root to: "home#index"
3. Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
irb(main):002:0>
```
看到倒數第二行:
```
=> #
```
就表示建立好帳號了。
# 線上實測
[https://people-all-love-kamigo.herokuapp.com/keyword_mappings](https://people-all-love-kamigo.herokuapp.com/keyword_mappings):
大家可以用我的帳號登入看看。
帳號:kamigo.service@gmail.com
密碼:kamigo
# 本日重點
- 學會使用 scaffold
- 學會作登入系統
你們可以透過閱讀 scaffold 產生出來的程式碼來學習 HTML 和 Controller Action 的寫法。這跟學英文一樣,看到不懂的單字就 Google,這單字量還比英文少超多,大概 100~200 個字而已。
明天講怎麼發公告。
<%= notice %>
<%= alert %>
4. You can copy Devise views (for customization) to your app by running: rails g devise:views =============================================================================== D:\只要有心,人人都可以作卡米狗\ironman> ``` 他說有幾個步驟產生器沒搞頭,必須手動進行。我們先不管他,等到真正出問題再回頭來解決。 使用產生器產生用戶資料模型: ``` rails generate devise user ``` ``` D:\只要有心,人人都可以作卡米狗\ironman>rails generate devise user invoke active_record create db/migrate/20180115152537_devise_create_users.rb create app/models/user.rb invoke test_unit create test/models/user_test.rb create test/fixtures/users.yml insert app/models/user.rb route devise_for :users D:\只要有心,人人都可以作卡米狗\ironman> ``` 跟剛剛的 scaffold 差不多,該生的都生好了。 註冊頁:[http://localhost:3000/users/sign_up](http://localhost:3000/users/sign_up) 登入頁:[http://localhost:3000/users/sign_in](http://localhost:3000/users/sign_in) # 關閉註冊功能 我們要將註冊功能關閉,如果大家都能註冊,那還要後台幹嘛? 在 `app/models/user.rb`: ``` class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end ``` 刪除 `:registerable,`: ``` class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable end ``` # 登入後才能管理關鍵字 我們希望只有登入後的人才能進入管理關鍵字的頁面。 在 `app/controllers/keyword_mappings_controller.rb` 加入: ``` before_action :authenticate_user! ``` 看起來像這樣: ``` class KeywordMappingsController < ApplicationController before_action :authenticate_user! before_action :set_keyword_mapping, only: [:show, :edit, :update, :destroy] ...下略 ``` 這時候開啟網址:[http://localhost:3000/keyword_mappings](http://localhost:3000/keyword_mappings),就會因為尚未登入,而被引導至登入頁。 # 發布流程 - 上傳程式碼 - Heroku 上的資料庫遷移 # 關閉了註冊功能後要怎麼新增自己的帳號? 使用 `rails console` 連上去新增帳號: ``` heroku run rails console ``` 連上後會是 `rails console` 的樣子: ``` D:\只要有心,人人都可以作卡米狗\ironman>heroku run rails console Running rails console on people-all-love-kamigo... up, run.2165 (Free) Loading production environment (Rails 5.1.4) irb(main):001:0> ``` 寫一行程式碼新增資料: ``` User.create(email:'kamigo.service@gmail.com', password:'kamigo') ``` 會有一些 SQL 的訊息: ``` irb(main):001:0> User.create(email:'kamigo.service@gmail.com', password:'kamigo') D, [2018-01-15T15:42:28.307402 #4] DEBUG -- : (6.0ms) BEGIN D, [2018-01-15T15:42:28.313291 #4] DEBUG -- : User Exists (2.1ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "kamigo.service@gmail.com"], ["LIMIT", 1]] D, [2018-01-15T15:42:28.317361 #4] DEBUG -- : SQL (1.9ms) INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["email", "kamigo.service@gmail.com"], ["encrypted_password", "$2a$11$EyR.yuDYI3J2s9/Q8Etk5evQzsz2bGAPdvdcr.xmFQbzYbBPQk/kK"], ["created_at", "2018-01-15 15:42:28.313883"], ["updated_at", "2018-01-15 15:42:28.313883"]] D, [2018-01-15T15:42:28.320139 #4] DEBUG -- : (2.0ms) COMMIT => #2018/1/15
Rails - 多檔上傳
假設有一個 Controller 叫做 `imgur`,大概是這樣:
```
rails g scaffold imgur pictures
```
而 Controller 內的 params 只允許傳遞 pictures 陣列:
```
def imgur_params
params.require(:imgur).permit(pictures: [])
end
```
一個最基本的檔案上傳表單長這樣:
```
<%= form_for(imgur) do |f| %>
<%= f.file_field :pictures %>
<%= f.submit %>
<% end %>
```
會因為 pictures 只吃陣列的關係,就傳不進去。如果想要上傳多個檔案,要加上 `multiple: true`:
```
<%= form_for(imgur) do |f| %>
<%= f.file_field :pictures, multiple: true %>
<%= f.submit %>
<% end %>
```
可以使用多個 f.file_field 來做上傳:
```
<%= form_for(imgur) do |f| %>
<%= f.file_field :pictures, multiple: true %>
<%= f.file_field :pictures, multiple: true %>
<%= f.submit %>
<% end %>
```
如果想要改用 `file_field_tag` 的話,就必須在 `form_for` 加上 `multipart: true`:
```
<%= form_for(imgur, html: { multipart: true }) do |f| %>
<%= file_field_tag "imgur[pictures][]" %>
<%= f.submit %>
<% end %>
```
加上 [] 之後,即使只傳一個檔也能通過 `params.permit`。
如果想要多檔上傳:
```
<%= form_for(imgur, html: { multipart: true }) do |f| %>
<%= file_field_tag "imgur[pictures][]", multiple: true %>
<%= f.submit %>
<% end %>
```
也可以這樣:
```
<%= form_for(imgur, html: { multipart: true }) do |f| %>
<%= file_field_tag "imgur[pictures][]" %>
<%= file_field_tag "imgur[pictures][]" %>
<%= f.submit %>
<% end %>
```
或者這樣:
```
<%= form_for(imgur, html: { multipart: true }) do |f| %>
<%= file_field_tag "imgur[pictures][]", multiple: true %>
<%= file_field_tag "imgur[pictures][]", multiple: true %>
<%= f.submit %>
<% end %>
```
第二十七天:卡米狗見人說人話,見鬼說鬼話
在[第二天:認識卡米狗](https://ithelp.ithome.com.tw/articles/10192575)提到過,見人說人話,見鬼說鬼話功能是考慮到多個群組都教了相同的關鍵字時,卡米狗應該在每個群組做出不同的回應,這樣才不會被討厭,於是就加入了這樣的功能。當有人說「姆咪姆咪」時,卡米狗會先檢查這個群組有沒有人教過看到「姆咪姆咪」要回應,如果教過多次,就回應最後一次學過的內容,如果都沒學過,那麼就再檢查其他群組有沒有學過「姆咪姆咪」。
也就是說,學說話指令在儲存時,應該也要儲存是在哪個頻道學會的。
# 修改學說話指令
目前的學說話指令:
```
# 學說話
def learn(received_text)
#如果開頭不是 卡米狗學說話; 就跳出
return nil unless received_text[0..6] == '卡米狗學說話;'
received_text = received_text[7..-1]
semicolon_index = received_text.index(';')
# 找不到分號就跳出
return nil if semicolon_index.nil?
keyword = received_text[0..semicolon_index-1]
message = received_text[semicolon_index+1..-1]
KeywordMapping.create(keyword: keyword, message: message)
'好哦~好哦~'
end
```
應該要改成這樣:
```
# 學說話
def learn(channel_id, received_text)
...略
KeywordMapping.create(channel_id: channel_id, keyword: keyword, message: message)
...略
end
```
重點是多傳入一個參數 `channel_id`,然後存入 `KeywordMapping`。
# 修改關鍵字回覆
在關鍵字回覆的部分,原本是:
```
# 關鍵字回覆
def keyword_reply(received_text)
KeywordMapping.where(keyword: received_text).last&.message
end
```
則是改為:
```
# 關鍵字回覆
def keyword_reply(channel_id, received_text)
message = KeywordMapping.where(channel_id: channel_id, keyword: received_text).last&.message
return message unless message.nil?
KeywordMapping.where(keyword: received_text).last&.message
end
```
多加了這兩行:
```
message = KeywordMapping.where(channel_id: channel_id, keyword: received_text).last&.message
return message unless message.nil?
```
這兩行的意思是,先找同一個頻道內教過的關鍵字,如果有找到的話就直接回傳。
如果你想要深入學習資料模型的查詢,官方也有提供中文版的說明文件,在這裡:[Active Record 查詢](https://rails.ruby.tw/active_record_querying.html#%E6%A2%9D%E4%BB%B6)。
# 主程式
要記得把參數也傳給剛剛改好的函數,原本的主程式是這樣:
```
def webhook
# 學說話
reply_text = learn(received_text)
# 關鍵字回覆
reply_text = keyword_reply(received_text) if reply_text.nil?
# 推齊
reply_text = echo2(channel_id, received_text) if reply_text.nil?
# 記錄對話
save_to_received(channel_id, received_text)
save_to_reply(channel_id, reply_text)
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
```
要改成:
```
def webhook
# 學說話
reply_text = learn(channel_id, received_text)
# 關鍵字回覆
reply_text = keyword_reply(channel_id, received_text) if reply_text.nil?
...略
end
```
這樣就作完了嗎!?
還沒呢,我們的 `KeywordMapping` 根本沒有 `channel_id` 欄位呀!
# 在 KeywordMapping 資料模型中新增欄位
我們需要使用資料庫遷移的方式來對 KeywordMapping 新增欄位,首先要先建立一個資料庫遷移檔。
指令是 `rails generate migration` 加上註解:
```
rails generate migration add_channel_id_to_keyword_reply
```
```
D:\只要有心,人人都可以作卡米狗\ironman>rails generate migration add_channel_id_to_keyword_reply
invoke active_record
create db/migrate/20180114163555_add_channel_id_to_keyword_reply.rb
D:\只要有心,人人都可以作卡米狗\ironman>
```
生成了一個檔案在 `db/migrate` 裡面,我們要在這個資料庫遷移檔裡打一點字,這是他目前的樣子:
```
class AddChannelIdToKeywordReply < ActiveRecord::Migration[5.1]
def change
end
end
```
要加一個欄位的話,要這樣寫:
```
class AddChannelIdToKeywordReply < ActiveRecord::Migration[5.1]
def change
add_column :keyword_mappings, :channel_id, :string
end
end
```
在 `add_column` 後面第一個參數是`資料表名稱`,第二個參數是`要新增的欄位名稱`,以及第三個參數:`要新增的欄位格式`。欄位格式的話,沒意外通常都會是 `:string`。
這裡寫好之後存檔,就可以作資料庫遷移了。為什麼我知道是這樣寫呢?文件在這裡:[Active Record 遷移](https://rails.ruby.tw/active_record_migrations.html#%E6%96%B0%E5%BB%BA%E7%8D%A8%E7%AB%8B%E7%9A%84%E9%81%B7%E7%A7%BB)
# 資料庫遷移
```
D:\只要有心,人人都可以作卡米狗\ironman>rails db:migrate
== 20180114163555 AddChannelIdToKeywordReply: migrating =======================
-- add_column(:keyword_mappings, :channel_id, :string)
-> 0.0012s
== 20180114163555 AddChannelIdToKeywordReply: migrated (0.0020s) ==============
D:\只要有心,人人都可以作卡米狗\ironman>
```
如果資料庫遷移檔沒打錯字的話,就會看到這個結果。
# 進行實測
首先上傳程式碼,要養成開著 `heroku logs -t` 的習慣。
測了一下會發現:
```
2018-01-14T16:56:36.918562+00:00 app[web.1]: I, [2018-01-14T16:56:36.918392 #4] INFO -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] Started POST "/kamigo/webhook" for 203.104.146.154 at 2018-01-14 16:56:36 +0000
2018-01-14T16:56:36.920295+00:00 app[web.1]: I, [2018-01-14T16:56:36.920209 #4] INFO -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] Processing by KamigoController#webhook as */*
2018-01-14T16:56:36.920486+00:00 app[web.1]: I, [2018-01-14T16:56:36.920397 #4] INFO -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] Parameters: {"events"=>[{"type"=>"message", "replyToken"=>"bffeaf21d2b64743b3268bd177ebbaff", "source"=>{"userId"=>"Uc68d82df46b7899e7d716f396ae8e91a", "type"=>"user"}, "timestamp"=>1515948996430, "message"=>{"type"=>"text", "id"=>"7310568889858", "text"=>"A"}}], "kamigo"=>{"events"=>[{"type"=>"message", "replyToken"=>"bffeaf21d2b64743b3268bd177ebbaff", "source"=>{"userId"=>"Uc68d82df46b7899e7d716f396ae8e91a", "type"=>"user"}, "timestamp"=>1515948996430, "message"=>{"type"=>"text", "id"=>"7310568889858", "text"=>"A"}}]}}
2018-01-14T16:56:36.920998+00:00 app[web.1]: W, [2018-01-14T16:56:36.920917 #4] WARN -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] Can't verify CSRF token authenticity.
2018-01-14T16:56:36.925356+00:00 app[web.1]: D, [2018-01-14T16:56:36.925257 #4] DEBUG -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] KeywordMapping Load (1.6ms) SELECT "keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_mappings"."channel_id" = $1 AND "keyword_mappings"."keyword" = $2 ORDER BY "keyword_mappings"."id" DESC LIMIT $3 [["channel_id", "Uc68d82df46b7899e7d716f396ae8e91a"], ["keyword", "A"], ["LIMIT", 1]]
2018-01-14T16:56:36.925763+00:00 app[web.1]: I, [2018-01-14T16:56:36.925658 #4] INFO -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.6ms)
2018-01-14T16:56:36.927283+00:00 app[web.1]: F, [2018-01-14T16:56:36.927195 #4] FATAL -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c]
2018-01-14T16:56:36.927428+00:00 app[web.1]: F, [2018-01-14T16:56:36.927362 #4] FATAL -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column keyword_mappings.channel_id does not exist
2018-01-14T16:56:36.927431+00:00 app[web.1]: LINE 1: ...keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_m...
2018-01-14T16:56:36.927432+00:00 app[web.1]: ^
2018-01-14T16:56:36.927438+00:00 app[web.1]: : SELECT "keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_mappings"."channel_id" = $1 AND "keyword_mappings"."keyword" = $2 ORDER BY "keyword_mappings"."id" DESC LIMIT $3):
2018-01-14T16:56:36.927567+00:00 app[web.1]: F, [2018-01-14T16:56:36.927496 #4] FATAL -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c]
2018-01-14T16:56:36.927701+00:00 app[web.1]: F, [2018-01-14T16:56:36.927580 #4] FATAL -- : [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] app/controllers/kamigo_controller.rb:82:in `keyword_reply'
2018-01-14T16:56:36.927702+00:00 app[web.1]: [2a0784f2-c2b7-46c1-818e-5e5dd799e64c] app/controllers/kamigo_controller.rb:10:in `webhook'
2018-01-14T16:56:36.929306+00:00 heroku[router]: at=info method=POST path="/kamigo/webhook" host=people-all-love-kamigo.herokuapp.com request_id=2a0784f2-c2b7-46c1-818e-5e5dd799e64c fwd="203.104.146.154" dyno=web.1 connect=0ms service=11ms status=500 bytes=1827 protocol=https
```
我先把前面那些多餘的字移除:
```
Started POST "/kamigo/webhook" for 203.104.146.154 at 2018-01-14 16:56:36 +0000
Processing by KamigoController#webhook as */*
Parameters: {"events"=>[{"type"=>"message", "replyToken"=>"bffeaf21d2b64743b3268bd177ebbaff", "source"=>{"userId"=>"Uc68d82df46b7899e7d716f396ae8e91a", "type"=>"user"}, "timestamp"=>1515948996430, "message"=>{"type"=>"text", "id"=>"7310568889858", "text"=>"A"}}], "kamigo"=>{"events"=>[{"type"=>"message", "replyToken"=>"bffeaf21d2b64743b3268bd177ebbaff", "source"=>{"userId"=>"Uc68d82df46b7899e7d716f396ae8e91a", "type"=>"user"}, "timestamp"=>1515948996430, "message"=>{"type"=>"text", "id"=>"7310568889858", "text"=>"A"}}]}}
Can't verify CSRF token authenticity.
KeywordMapping Load (1.6ms) SELECT "keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_mappings"."channel_id" = $1 AND "keyword_mappings"."keyword" = $2 ORDER BY "keyword_mappings"."id" DESC LIMIT $3 [["channel_id", "Uc68d82df46b7899e7d716f396ae8e91a"], ["keyword", "A"], ["LIMIT", 1]]
Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.6ms)
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column keyword_mappings.channel_id does not exist
LINE 1: ...keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_m...
^
SELECT "keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_mappings"."channel_id" = $1 AND "keyword_mappings"."keyword" = $2 ORDER BY "keyword_mappings"."id" DESC LIMIT $3):
app/controllers/kamigo_controller.rb:82:in `keyword_reply'
app/controllers/kamigo_controller.rb:10:in `webhook'
at=info method=POST path="/kamigo/webhook" host=people-all-love-kamigo.herokuapp.com request_id=2a0784f2-c2b7-46c1-818e-5e5dd799e64c fwd="203.104.146.154" dyno=web.1 connect=0ms service=11ms status=500 bytes=1827 protocol=https
```
我們要關注的重點在:
```
Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.6ms)
```
當你看到 `500 Internal Server Error`,表示程式跑到一半就掛了,掛點原因通常會寫在這個訊息後面。
一個正常的 Log 是長這樣:
```
Completed 200 OK in 269ms (ActiveRecord: 9.9ms)
```
掛點原因:
```
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column keyword_mappings.channel_id does not exist
LINE 1: ...keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_m...
^
SELECT "keyword_mappings".* FROM "keyword_mappings" WHERE "keyword_mappings"."channel_id" = $1 AND "keyword_mappings"."keyword" = $2 ORDER BY "keyword_mappings"."id" DESC LIMIT $3):
app/controllers/kamigo_controller.rb:82:in `keyword_reply'
app/controllers/kamigo_controller.rb:10:in `webhook'
```
他說:`ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column keyword_mappings.channel_id does not exist`,意思是 `keyword_mappings` 表格裡面沒有 `channel_id` 這個欄位。
這個叫做 `exception message`。一般來說遇到絕大多數的問題都可以拿 `exception message` 去餵給 google ,就能得到問題的答案。不過看到這裡應該就能猜到是忘記作 Heroku 上的資料庫遷移了。
另外,最後面的那兩行:
```
app/controllers/kamigo_controller.rb:82:in `keyword_reply'
app/controllers/kamigo_controller.rb:10:in `webhook'
```
這個叫做 `stack trace`。
意思是他死在 `kamigo_controller.rb` 的第 `82` 行,是在 `keyword_reply` 方法裡。而為什麼他會跑進這個方法呢?原來是在 `kamigo_controller.rb` 在 `webhook` 方法裡的第 `10` 行的呼叫了 `keyword_reply` 方法。
透過閱讀 `stack trace` 你通常就能夠找到錯誤的根源。
# 在 Heroku 上的資料庫遷移
一如往常:
```
heroku run rake db:migrate
```
如果你明明已經跑了資料庫遷移程式,但他還是找不到新欄位的話,可以試試看重開 heroku server:
```
heroku restart
```
### 正確的測試流程
- 把他邀請進群組 1
- 把他邀請進群組 2
- 在群組 1 教他看到 A 要回答 B
- 在群組 2 教他看到 A 要回答 C
- 在群組 1 說 A 看他是不是回答 B
- 在群組 2 說 A 看他是不是回答 C
應該是順利啦~
# 本日重點
- 學會怎麼對已經存在的資料模型加一個欄位
- 學會見人說人話,見鬼說鬼話的本領
- 學會在 Heoku 上除錯的方法
2018/1/14
第二十六天:卡米狗推齊
今天要作的是卡米狗的推齊功能,也就是當看到有兩次以上有人說出相同的句子,那麼就跟著說的功能。要作到這件事,卡米狗必須要有一點記性才行,所以我們必須記錄每個群組中所發生的對話。當有人說出一句話時,就檢查最近有沒有人也說出相同的話,如果有的話卡米狗就跟著說。
# 使用情境
我們希望的是這樣:
```
B哥:「采瑤生日快樂~~」
小昕:「采瑤生日快樂~~」
卡米狗:「采瑤生日快樂~~」
```
但事實上是這樣:
```
B哥:「采瑤生日快樂~~」
小昕:「采瑤生日快樂~~」
卡米狗:「采瑤生日快樂~~」
毛毛:「采瑤生日快樂~~」
卡米狗:「采瑤生日快樂~~」
```
卡米狗不應該推齊兩次的,因為正常人推齊只會推一次,所以卡米狗要記得自己上次說了什麼。
# 推齊的邏輯
整理了一下之後,我們可以寫一個大概的程式碼如下:
```
def 推齊(channel_id, received_text)
如果在 channel_id 最近沒人講過 received_text,卡米狗就不回應
如果在 channel_id 卡米狗上一句回應是 received_text,卡米狗就不回應
回應 received_text
end
```
這種不能執行的程式碼稱為虛擬碼,是用來表達邏輯、幫助思考和討論用的。
`channel_id` 代表目前的群組、聊天室或私聊的 ID,我們這裡姑且通稱為頻道 ID。
# 修改主程式
這是目前的程式碼:
```
def webhook
# 學說話
reply_text = learn(received_text)
# 關鍵字回覆
reply_text = keyword_reply(received_text) if reply_text.nil?
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
```
在卡米狗中,推齊功能的順位是最低的,所以我們會把推齊擺在關鍵字回覆的後面。
```
def webhook
# 學說話
reply_text = learn(received_text)
# 關鍵字回覆
reply_text = keyword_reply(received_text) if reply_text.nil?
# 推齊
reply_text = echo2(channel_id, received_text) if reply_text.nil?
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
```
我們還需要記錄對話:
```
def webhook
# 學說話
reply_text = learn(received_text)
# 關鍵字回覆
reply_text = keyword_reply(received_text) if reply_text.nil?
# 推齊
reply_text = echo2(channel_id, received_text) if reply_text.nil?
# 記錄對話
save_to_received(channel_id, received_text)
save_to_reply(channel_id, reply_text)
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
```
主程式大概就是這樣了。
我們需要實作 `channel_id`、`save_to_received`、`save_to_reply`、`echo2` 這四個函數,並且需要兩個資料模型,分別儲存`收到的對話`以及`回應的對話`。
# 建立資料模型
建立 `received` 資料模型:`rails generate model received channel_id text`
```
D:\只要有心,人人都可以作卡米狗\ironman>rails generate model received channel_id text
invoke active_record
create db/migrate/20180113153959_create_receiveds.rb
create app/models/received.rb
invoke test_unit
create test/models/received_test.rb
create test/fixtures/receiveds.yml
D:\只要有心,人人都可以作卡米狗\ironman>
```
建立 `reply` 資料模型:`rails generate model reply channel_id text`
```
D:\只要有心,人人都可以作卡米狗\ironman>rails generate model reply channel_id text
invoke active_record
create db/migrate/20180113154217_create_replies.rb
create app/models/reply.rb
invoke test_unit
create test/models/reply_test.rb
create test/fixtures/replies.yml
D:\只要有心,人人都可以作卡米狗\ironman>
```
進行資料庫遷移:`rails db:migrate`
```
D:\只要有心,人人都可以作卡米狗\ironman>rails db:migrate
== 20180113153959 CreateReceiveds: migrating ==================================
-- create_table(:receiveds)
-> 0.5013s
== 20180113153959 CreateReceiveds: migrated (0.5027s) =========================
== 20180113154217 CreateReplies: migrating ====================================
-- create_table(:replies)
-> 0.0013s
== 20180113154217 CreateReplies: migrated (0.0024s) ===========================
D:\只要有心,人人都可以作卡米狗\ironman>
```
# 頻道 ID
根據 [Line Messaging API 的文件](https://developers.line.me/en/docs/messaging-api/reference/#common-properties),我們知道要從 `params['events'][0]['source']` 底下去找 `groupId`、`roomId` 或者是 `userId`。
如果對話是發生在群組,`groupId` 就會有值,如果對話是發生在聊天室,`roomId` 就會有值。
所以我們要這樣寫:
```
# 頻道 ID
def channel_id
source = params['events'][0]['source']
return source['groupId'] unless source['groupId'].nil?
return source['roomId'] unless source['roomId'].nil?
source['userId']
end
```
可以浪漫一點:
```
# 頻道 ID
def channel_id
source = params['events'][0]['source']
source['groupId'] || source['roomId'] || source['userId']
end
```
# 儲存對話
在儲存前應該先檢查有沒有值,因為 `received_text` 不一定有值。
```
# 儲存對話
def save_to_received(channel_id, received_text)
return if received_text.nil?
Received.create(channel_id: channel_id, text: received_text)
end
```
# 儲存回應
```
# 儲存回應
def save_to_reply(channel_id, reply_text)
return if reply_text.nil?
Reply.create(channel_id: channel_id, text: reply_text)
end
```
# 推齊
按照我們一開始講的虛擬碼邏輯去寫:
```
def echo2(channel_id, received_text)
# 如果在 channel_id 最近沒人講過 received_text,卡米狗就不回應
recent_received_texts = Received.where(channel_id: channel_id).last(5)&.pluck(:text)
return nil unless received_text.in? recent_received_texts
# 如果在 channel_id 卡米狗上一句回應是 received_text,卡米狗就不回應
last_reply_text = Reply.where(channel_id: channel_id).last&.text
return nil if last_reply_text == received_text
received_text
end
```
# 發布
### 對一下程式碼
```
require 'line/bot'
class KamigoController < ApplicationController
protect_from_forgery with: :null_session
def webhook
# 學說話
reply_text = learn(received_text)
# 關鍵字回覆
reply_text = keyword_reply(received_text) if reply_text.nil?
# 推齊
reply_text = echo2(channel_id, received_text) if reply_text.nil?
# 記錄對話
save_to_received(channel_id, received_text)
save_to_reply(channel_id, reply_text)
# 傳送訊息到 line
response = reply_to_line(reply_text)
# 回應 200
head :ok
end
# 頻道 ID
def channel_id
source = params['events'][0]['source']
source['groupId'] || source['roomId'] || source['userId']
end
# 儲存對話
def save_to_received(channel_id, received_text)
return if received_text.nil?
Received.create(channel_id: channel_id, text: received_text)
end
# 儲存回應
def save_to_reply(channel_id, reply_text)
return if reply_text.nil?
Reply.create(channel_id: channel_id, text: reply_text)
end
def echo2(channel_id, received_text)
# 如果在 channel_id 最近沒人講過 received_text,卡米狗就不回應
recent_received_texts = Received.where(channel_id: channel_id).last(5)&.pluck(:text)
return nil unless received_text.in? recent_received_texts
# 如果在 channel_id 卡米狗上一句回應是 received_text,卡米狗就不回應
last_reply_text = Reply.where(channel_id: channel_id).last&.text
return nil if last_reply_text == received_text
received_text
end
# 取得對方說的話
def received_text
message = params['events'][0]['message']
message['text'] unless message.nil?
end
# 學說話
def learn(received_text)
#如果開頭不是 卡米狗學說話; 就跳出
return nil unless received_text[0..6] == '卡米狗學說話;'
received_text = received_text[7..-1]
semicolon_index = received_text.index(';')
# 找不到分號就跳出
return nil if semicolon_index.nil?
keyword = received_text[0..semicolon_index-1]
message = received_text[semicolon_index+1..-1]
KeywordMapping.create(keyword: keyword, message: message)
'好哦~好哦~'
end
# 關鍵字回覆
def keyword_reply(received_text)
KeywordMapping.where(keyword: received_text).last&.message
end
# 傳送訊息到 line
def reply_to_line(reply_text)
return nil if reply_text.nil?
# 取得 reply token
reply_token = params['events'][0]['replyToken']
# 設定回覆訊息
message = {
type: 'text',
text: reply_text
}
# 傳送訊息
line.reply_message(reply_token, message)
end
# Line Bot API 物件初始化
def line
@line ||= Line::Bot::Client.new { |config|
config.channel_secret = '9160ce4f0be51cc72c3c8a14119f567a'
config.channel_token = '2ncMtCFECjdTVmopb/QSD1PhqM6ECR4xEqC9uwIzELIsQb+I4wa/s3pZ4BH8hCWeqfkpVGVig/mIPDsMjVcyVbN/WNeTTw5eHEA7hFhaxPmQSY2Cud51LKPPiXY+nUi+QrXy0d7Hi2YUs65B/tVOpgdB04t89/1O/w1cDnyilFU='
}
end
def eat
render plain: "吃土啦"
end
def request_headers
render plain: request.headers.to_h.reject{ |key, value|
key.include? '.'
}.map{ |key, value|
"#{key}: #{value}"
}.sort.join("\n")
end
def response_headers
response.headers['5566'] = 'QQ'
render plain: response.headers.to_h.map{ |key, value|
"#{key}: #{value}"
}.sort.join("\n")
end
def request_body
render plain: request.body
end
def show_response_body
puts "===這是設定前的response.body:#{response.body}==="
render plain: "虎哇花哈哈哈"
puts "===這是設定後的response.body:#{response.body}==="
end
def sent_request
uri = URI('http://localhost:3000/kamigo/eat')
http = Net::HTTP.new(uri.host, uri.port)
http_request = Net::HTTP::Get.new(uri)
http_response = http.request(http_request)
render plain: JSON.pretty_generate({
request_class: request.class,
response_class: response.class,
http_request_class: http_request.class,
http_response_class: http_response.class
})
end
def translate_to_korean(message)
"#{message}油~"
end
end
```
上傳程式碼囉~
### Heroku 上的資料庫遷移
要在上傳程式碼之後才能作資料庫遷移,因為資料庫遷移需要讀取資料庫遷移檔。Heroku 上的資料庫遷移指令是 `heroku run rake db:migrate`:
```
D:\只要有心,人人都可以作卡米狗\ironman>heroku run rake db:migrate
Running rake db:migrate on people-all-love-kamigo... up, run.4769 (Free)
D, [2018-01-13T16:57:33.099237 #4] DEBUG -- : (0.6ms) SELECT pg_try_advisory_lock(8162367372296191845)
D, [2018-01-13T16:57:33.115389 #4] DEBUG -- : (2.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
I, [2018-01-13T16:57:33.116984 #4] INFO -- : Migrating to CreateReceiveds (20180113153959)
D, [2018-01-13T16:57:33.119682 #4] DEBUG -- : (0.6ms) BEGIN
== 20180113153959 CreateReceiveds: migrating ==================================
-- create_table(:receiveds)
D, [2018-01-13T16:57:33.166042 #4] DEBUG -- : (45.6ms) CREATE TABLE "receiveds" ("id" bigserial primary key, "channel_id" character varying, "text" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0463s
== 20180113153959 CreateReceiveds: migrated (0.0464s) =========================
D, [2018-01-13T16:57:33.170513 #4] DEBUG -- : SQL (0.7ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version" [["version", "20180113153959"]]
D, [2018-01-13T16:57:33.173887 #4] DEBUG -- : (3.1ms) COMMIT
I, [2018-01-13T16:57:33.174003 #4] INFO -- : Migrating to CreateReplies (20180113154217)
D, [2018-01-13T16:57:33.174944 #4] DEBUG -- : (0.6ms) BEGIN
== 20180113154217 CreateReplies: migrating ====================================
-- create_table(:replies)
D, [2018-01-13T16:57:33.184287 #4] DEBUG -- : (8.8ms) CREATE TABLE "replies" ("id" bigserial primary key, "channel_id" character varying, "text" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0093s
== 20180113154217 CreateReplies: migrated (0.0093s) ===========================
D, [2018-01-13T16:57:33.185682 #4] DEBUG -- : SQL (0.6ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version" [["version", "20180113154217"]]
D, [2018-01-13T16:57:33.187624 #4] DEBUG -- : (1.7ms) COMMIT
D, [2018-01-13T16:57:33.193606 #4] DEBUG -- : ActiveRecord::InternalMetadata Load (2.0ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]]
D, [2018-01-13T16:57:33.201843 #4] DEBUG -- : (0.5ms) BEGIN
D, [2018-01-13T16:57:33.204353 #4] DEBUG -- : (1.6ms) COMMIT
D, [2018-01-13T16:57:33.205359 #4] DEBUG -- : (0.7ms) SELECT pg_advisory_unlock(8162367372296191845)
D:\只要有心,人人都可以作卡米狗\ironman>
```
# 實測

成功!
失敗的在底下留言,謝謝。
# 本日重點
- 學會了判斷目前的頻道
- 學會了如何根據前後文作出不同的回應
# 接下來
剩沒幾天了,還有很多可以學的,我想知道你們比較想學些什麼?
接下來我們還可以做的事情有這些:
- 現在的程式有點亂了,而且還有些問題,需要整理
- 讓卡米狗的關鍵字回應能根據目前頻道,作出不同的回應
- 讓卡米狗能抽籤
- 讓卡米狗能擷取用戶的使用者名稱以及大頭貼
- 讓卡米狗能接收及傳送貼圖
- 讓卡米狗能接收及傳送圖片
- 讓卡米狗能傳送含有按鈕的選單
- 讓卡米狗能查天氣
- 打造一個管理後台
- 讓卡米狗能發公告
- 製作小遊戲,比方說井字遊戲
或者你有想到,但上面沒列出來的也可以。
請在本文留言讓我知道你想學些什麼,可複選。
訂閱:
文章 (Atom)