おうむ返しbotを作りたい
現状、ターミナルにリクエストがちゃんと来ていて、client.reply_message(event['replyToken'], message)以外の部分で特に問題はなさそうです。
なぜ、client.reply_message(event['replyToken'], message)が動かないのか、ご教授ください。
zsh
1# ngrok 側のターミナル 2 3ngrok by @inconshreveable (Ctrl+C to quit) 4 5Session Status online 6Session Expires 1 hour, 23 minutes 7Version 2.3.40 8Region United States (us) 9Web Interface http://127.0.0.1:4040 10Forwarding http://3014-60-125-209-90.ngrok.io -> http://localhost:3000 11Forwarding https://3014-60-125-209-90.ngrok.io -> http://localhost:3000 12 13Connections ttl opn rt1 rt5 p50 p90 14 8 0 0.00 0.00 20.30 20.46 15 16HTTP Requests 17------------- 18 19POST /callback 200 OK 20POST /callback 200 OK 21POST /callback 200 OK 22POST /callback 200 OK 23POST /callback 200 OK 24POST /callback 200 OK 25POST /callback 200 OK 26POST /callback 200 OK
zsh
1# rails サーバー側のターミナル 2 3 4Started GET "/" for ::1 at 2021-10-15 14:28:38 +0900 5 (0.0ms) SELECT sqlite_version(*) 6Processing by HelloController#index as HTML 7 Rendering hello/index.html.erb within layouts/application 8 Rendered hello/index.html.erb within layouts/application (Duration: 1.4ms | Allocations: 186) 9[Webpacker] Everything's up-to-date. Nothing to do 10Completed 200 OK in 35ms (Views: 30.9ms | ActiveRecord: 0.0ms | Allocations: 8304) 11 12 13Started POST "/callback" for 147.92.149.165 at 2021-10-15 14:33:15 +0900 14Cannot render console from 147.92.149.165! Allowed networks: 127.0.0.0/127.255.255.255, ::1 15Processing by LineBotController#callback as HTML 16 Parameters: {"destination"=>"U2ddd1dbbf3e4674173ab1bf56bbf689c", "events"=>[{"type"=>"message", "message"=>{"type"=>"text", "id"=>"14916580578592", "text"=>"らならな"}, "timestamp"=>1634275994351, "source"=>{"type"=>"user", "userId"=>"Ucf3253cdda0ab9f615c390fddc69af71"}, "replyToken"=>"3ccbae4f2b584d6489b1e788f4897549", "mode"=>"active"}], "line_bot"=>{"destination"=>"U2ddd1dbbf3e4674173ab1bf56bbf689c", "events"=>[{"type"=>"message", "message"=>{"type"=>"text", "id"=>"14916580578592", "text"=>"らならな"}, "timestamp"=>1634275994351, "source"=>{"type"=>"user", "userId"=>"Ucf3253cdda0ab9f615c390fddc69af71"}, "replyToken"=>"3ccbae4f2b584d6489b1e788f4897549", "mode"=>"active"}]}} 17Completed 200 OK in 65ms (ActiveRecord: 0.0ms | Allocations: 1110)
rb
1class LineBotController < ApplicationController 2 protect_from_forgery except: [:callback] 3 4 def callback 5 body = request.body.read 6 signature = request.env['HTTP_X_LINE_SIGNATURE'] 7 unless client.validate_signature(body, signature) 8 return head :bad_request 9 end 10 events = client.parse_events_from(body) 11 events.each do |event| 12 case event 13 when Line::Bot::Event::Message 14 case event.type 15 when Line::Bot::Event::MessageType::Text 16 message = { 17 type: 'text', 18 text: event.message['text'] 19 } 20 client.reply_message(event['replyToken'], message) 21 end 22 end 23 end 24 head :ok 25 end 26 27 private 28 def client 29 @client ||= Line::Bot::Client.new { |config| 30 config.channel_secret = ENV["LINE_CHANNEL_SECRET"] 31 config.channel_token = ENV["LINE_CHANNEL_TOKEN"] 32 } 33 end 34end
解決するために実行したこと
- チャネルトークンとチャネルシークレットキーを確認。
- チャネル側の登録ドメインを切り替え、再接続
- event['replyToken']のアクセスできているか、pp event['replyToken']でターミナルに出力させて確認。
rb
1class LineBotController < ApplicationController 2 . 3 . 4 def callback 5 . 6 . 7 . 8 when Line::Bot::Event::MessageType::Text 9 message = { 10 type: 'text', 11 text: event.message['text'] 12 } 13 client.reply_message(event['replyToken'], message) 14 pp event['replyToken'] 15 pp message 16 end 17 end 18 end 19 head :ok 20 end 21. 22. 23. 24. 25 26end
zsh
1Started POST "/callback" for 147.92.149.165 at 2021-10-15 15:01:26 +0900 2Cannot render console from 147.92.149.165! Allowed networks: 127.0.0.0/127.255.255.255, ::1 3 (0.1ms) SELECT sqlite_version(*) 4Processing by LineBotController#callback as HTML 5 Parameters: {"destination"=>"U2ddd1dbbf3e4674173ab1bf56bbf689c", "events"=>[{"type"=>"message", "message"=>{"type"=>"text", "id"=>"14916696216493", "text"=>"たやたや"}, "timestamp"=>1634277685467, "source"=>{"type"=>"user", "userId"=>"Ucf3253cdda0ab9f615c390fddc69af71"}, "replyToken"=>"e650a69c228445d78f96089f6719aa7e", "mode"=>"active"}], "line_bot"=>{"destination"=>"U2ddd1dbbf3e4674173ab1bf56bbf689c", "events"=>[{"type"=>"message", "message"=>{"type"=>"text", "id"=>"14916696216493", "text"=>"たやたや"}, "timestamp"=>1634277685467, "source"=>{"type"=>"user", "userId"=>"Ucf3253cdda0ab9f615c390fddc69af71"}, "replyToken"=>"e650a69c228445d78f96089f6719aa7e", "mode"=>"active"}]}} 6"e650a69c228445d78f96089f6719aa7e" 7{:type=>"text", :text=>"たやたや"} 8Completed 200 OK in 86ms (ActiveRecord: 0.0ms | Allocations: 1215)
個人的な結論
line側の問題なのかなと考えています。
原因をご教授していただける方はお願いいたします。
あなたの回答
tips
プレビュー