rails、herokuを用いてLINEBOTを作成しようとしております。
heroku run rails consoleをすると正常に動くのですが、実際にLINEで話し掛けると
どうも上手くいかないためherokuにてログを参照したところ、以下のようなActionViewエラーが出ます。
Completed 500 Internal Server Error in 8ms ActionView::MissingTemplate (Missing template webhook/callback, application/callback with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/app/app/views" ): app/controllers/webhook_controller.rb:10:in `callback'
callbackアクションに対して表示するviewがないよ!と言ってそうな気がしますが、webhookコントローラー内callbackアクションにて「render :nothing => true」で何も表示しないと指定しています。
その後の「application/callback」がミソのような気がしますが、applicationコントローラーにはcallbackアクションは無い・・・と、ここで詰まっております。
「render :nothing => true」が効かない条件がコントローラー内にあるのでしょうか?
また、「app/controllers/webhook_controller.rb:10:in `callback'」もヒントになるような気はするのですが、わからずじまいです。(webhookコントローラーの10行目にrender :nothing => trueを書いてる)
(追記)もしかしてcallback内に「render :nothing => true」が2つあるのが問題かと思いましたが、and returnを入れても変化なかったので違いそうです。
以下にherokuのログと、コントローラー(webhook_controller.rb)、routes.rbのコードを投げておきます。
(コントローラーはこちらを参考にしております(https://qiita.com/YoheiMiyamoto/items/f6851cdb40891edf0e57))
lang
1class WebhookController < ApplicationController 2 protect_from_forgery except: :callback 3 4 CHANNEL_SECRET = ENV['CHANNEL_SECRET'] 5 OUTBOUND_PROXY = ENV['OUTBOUND_PROXY'] 6 CHANNEL_ACCESS_TOKEN = ENV['CHANNEL_ACCESS_TOKEN'] 7 8 def callback 9 unless is_validate_signature 10 render :nothing => true, status: 470 11 end 12 13 event = params["events"][0] 14 event_type = event["type"] 15 replyToken = event["replyToken"] 16 17 case event_type 18 when "message" 19 input_text = event["message"]["text"] 20 output_text = input_text 21 end 22 23 client = LineClient.new(CHANNEL_ACCESS_TOKEN, OUTBOUND_PROXY) 24 res = client.reply(replyToken, output_text) 25 26 if res.status == 200 27 logger.info({success: res}) 28 else 29 logger.info({fail: res}) 30 end 31 32 render :nothing => true, status: :ok 33 end 34 35 private 36 # verify access from LINE 37 def is_validate_signature 38 signature = request.headers["X-LINE-Signature"] 39 http_request_body = request.raw_post 40 hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, CHANNEL_SECRET, http_request_body) 41 signature_answer = Base64.strict_encode64(hash) 42 signature == signature_answer 43 end 44end
#routes.rb Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html post '/callback' => 'webhook#callback' end
2017-10-10T12:52:52.629674+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] Started POST "/callback" for 203.104.156.75 at 2017-10-10 12:52:52 +0000 2017-10-10T12:52:52.634068+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] Processing by WebhookController#callback as HTML 2017-10-10T12:52:52.634233+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] Parameters: {"events"=>[{"replyToken"=>"***", "type"=>"message", "timestamp"=>***, "source"=>{"type"=>"user", "userId"=>"***"}, "message"=>{"id"=>"***", "type"=>"text", "text"=>"Hello, world"}}, {"replyToken"=>"***", "type"=>"message", "timestamp"=>***, "source"=>{"type"=>"user", "userId"=>"***"}, "message"=>{"id"=>"***", "type"=>"sticker", "packageId"=>"1", "stickerId"=>"1"}}], "webhook"=>{"events"=>[{"replyToken"=>"***", "type"=>"message", "timestamp"=>***, "source"=>{"type"=>"user", "userId"=>"***"}, "message"=>{"id"=>"***", "type"=>"text", "text"=>"Hello, world"}}, {"replyToken"=>"***", "type"=>"message", "timestamp"=>***, "source"=>{"type"=>"user", "userId"=>"***"}, "message"=>{"id"=>"***", "type"=>"sticker", "packageId"=>"1", "stickerId"=>"1"}}]}} 2017-10-10T12:52:52.642587+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] Can't verify CSRF token authenticity. 2017-10-10T12:52:52.644502+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] Completed 500 Internal Server Error in 10ms 2017-10-10T12:52:52.645073+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] 2017-10-10T12:52:52.645114+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] ActionView::MissingTemplate (Missing template webhook/callback, application/callback with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: 2017-10-10T12:52:52.645116+00:00 app[web.1]: * "/app/app/views" 2017-10-10T12:52:52.645162+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] 2017-10-10T12:52:52.645116+00:00 app[web.1]: ): 2017-10-10T12:52:52.645209+00:00 app[web.1]: [b6566130-8446-4c9b-8b6c-b6d653fb0c07] app/controllers/webhook_controller.rb:10:in `callback' 2017-10-10T12:53:32.725642+00:00 heroku[router]: at=info method=POST path="/callback" host=***.herokuapp.com request_id=f8695c6b-0fc7-44a7-966a-65b353dcf3d2 fwd="203.104.146.154" dyno=web.1 connect=1ms service=16ms status=500 bytes=1827 protocol=https 2017-10-10T12:53:32.711833+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] Started POST "/callback" for 203.104.146.154 at 2017-10-10 12:53:32 +0000 2017-10-10T12:53:32.713687+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] Processing by WebhookController#callback as */* 2017-10-10T12:53:32.713835+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] Parameters: {"events"=>[{"type"=>"message", "replyToken"=>"***", "source"=>{"userId"=>"***", "type"=>"user"}, "timestamp"=>***, "message"=>{"type"=>"text", "id"=>"***", "text"=>"ぼー"}}], "webhook"=>{"events"=>[{"type"=>"message", "replyToken"=>"***", "source"=>{"userId"=>"***", "type"=>"user"}, "timestamp"=>***, "message"=>{"type"=>"text", "id"=>"***", "text"=>"ぼー"}}]}} 2017-10-10T12:53:32.714357+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] Can't verify CSRF token authenticity. 2017-10-10T12:53:32.720612+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] Completed 500 Internal Server Error in 7ms 2017-10-10T12:53:32.721551+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] 2017-10-10T12:53:32.721607+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] ActionView::MissingTemplate (Missing template webhook/callback, application/callback with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: 2017-10-10T12:53:32.721609+00:00 app[web.1]: * "/app/app/views" 2017-10-10T12:53:32.721610+00:00 app[web.1]: ): 2017-10-10T12:53:32.721635+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] 2017-10-10T12:53:32.721685+00:00 app[web.1]: [f8695c6b-0fc7-44a7-966a-65b353dcf3d2] app/controllers/webhook_controller.rb:10:in `callback'
(補足)herokuログに「Can't verify CSRF token authenticity.」とありますが、CSRFエラーの方は解決しております。
protect_from_forgery except: :callback
でcallbackをよけてあげると通りました。
コードの方は修正してあります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/01 07:50
2017/11/06 07:06