前提・実現したいこと
line messaging api を用いておうむ返しbotを作っています。
記事を参考にしています。ngrokを用いています。
(開発中のウェブサービスを line messaing api と連携させようと思っているため関係のないファイルがたくさんあります。)
メッセージをbotに送っても返信が来ません。どうしたら良いでしょうか?
発生している問題・エラーメッセージ
line dvelopers上
Webhookが無効なHTTPステータスコードを返しました(期待されるステータスコードは200です)
ngrok上
HTTP Requests
POST /callback 302 Found
POST /callback 302 Found
rails s
Started POST "/callback" for 147.92.149.165 at 2019-04-18 14:15:59 +0900
Cannot render console from 147.92.149.165! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by LinebotController#callback as /
Parameters: {"events"=>[{"type"=>"message", "replyToken"=>"529e6f24af7742689e4678c74ef4a5be", "source"=>{"userId"=>"U08f073c130655542785e036fac8f3975", "type"=>"user"}, "timestamp"=>1555564559692, "message"=>{"type"=>"text", "id"=>"9713085378705", "text"=>"あああ"}}], "destination"=>"U9c3c28732a433b535c9c43577fd739e5", "linebot"=>{"events"=>[{"type"=>"message", "replyToken"=>"529e6f24af7742689e4678c74ef4a5be", "source"=>{"userId"=>"U08f073c130655542785e036fac8f3975", "type"=>"user"}, "timestamp"=>1555564559692, "message"=>{"type"=>"text", "id"=>"9713085378705", "text"=>"あああ"}}], "destination"=>"U9c3c28732a433b535c9c43577fd739e5"}}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)
該当のソースコード
ruby
1app/controllers/linebot_controller.rb 2 3class LinebotController < ApplicationController 4 require 'line/bot' 5 protect_from_forgery :except => [:callback] 6 7 def callback 8 body = request.body.read 9 10 signature = request.env['HTTP_X_LINE_SIGNATURE'] 11 unless client.validate_signature(body, signature) 12 error 400 do 'Bad Request' end 13 end 14 15 events = client.parse_events_from(body) 16 events.each { |event| 17 case event 18 when Line::Bot::Event::Message 19 case event.type 20 when Line::Bot::Event::MessageType::Text 21 message = { 22 type: 'text', 23 text: event.message['text'] 24 } 25 response = client.reply_message(event['replyToken'], message) 26 p response 27 end 28 end 29 } 30 head :ok 31 end 32 33 private 34 def client 35 @client ||= Line::Bot::Client.new { |config| 36 config.channel_secret = ENV['LINE_CHANNEL_SECRET'] 37 config.channel_token = ENV['LINE_CHANNEL_TOKEN'] 38 } 39 end 40end
ruby
1 2config/routes.rb 3 4Rails.application.routes.draw do 5 resources :profiles 6 resources :publishers 7 devise_for :users 8 resources :categories 9 devise_for :admin_users, ActiveAdmin::Devise.config 10 ActiveAdmin.routes(self) 11 resources :coupons 12 post '/callback' => 'linebot#callback' 13 match ':controller(/:action(/:id))', via: [ :get, :post, :patch ] 14 root "categories#index" 15end
試したこと
アクセストークンの問題のように思ったので、再発行などを行いました。
補足情報(FW/ツールのバージョンなど)
環境変数に関してはgem 'dotenv'を用いていて、rails cで呼び出せていることを確認済みです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/18 06:13