レールの3つのXML Builder/API Twilio
-
28-09-2019 - |
質問
このAPI Twilioサンプルコードな作業にレール3:
#voice_controller.rb
def reminder
@postto = BASE_URL + '/directions'
respond_to do |format|
format.xml { @postto }
end
end
#reminder.xml.builder
xml.instruct!
xml.Response do
xml.Gather(:action => @postto, :numDigits => 1) do
xml.Say "Hello this is a call from Twilio. You have an appointment
tomorrow at 9 AM."
xml.Say "Please press 1 to repeat this menu. Press 2 for directions.
Or press 3 if you are done."
end
end
そのアイデア?
Twilioのように成功し、電話(できのparamsと自分の電話番号、所在地等) そのことが漠然とした対象にコード:
Completed 406 Not Acceptable in 0ms
解決
TwilioはAccept Httpヘッダーを送信しません その要求, 、Rails 3が適切なコンテンツタイプで応答できないと判断します。ただし、以下はあなたのためにそれを回避すると思います:
# voice_controller.rb def reminder @postto = BASE_URL + '/directions' render :content_type => 'application/xml' end
他のヒント
Twilioの社員です。あるいは束の変化をレールこそが掲載され、たいと思った方が取り組むことでこの問題を使用レール4,懸念のTwilio Rubyの逸品です。●
のコード例では、以下を定義しコントローラ /controllers/voice_controller.rb
などの懸念というWebhookable.のWebhookable懸念で封止ロジックに関連しTwilio web通知設定のHTTPリダイレクトのHTTPレスポンスヘッダをtext/xml、レンダリングTwiMLし、検証する要求から発生しTwilioなど)を単一のモジュールです。
require 'twilio-ruby'
class VoiceController < ApplicationController
include Webhookable
after_filter :set_header
# controller code here
end
の心に生活を /controllers/concerns/webhookable.rb
やはシンプルなものになっている。現在では、単純に設定しますコンテンツタイプtext/xmlのためのすべての行動をとるメソッドを提供します描画のTwiMLオブジェクトです。しんののコードを検証することの要望からTwilioが、そう簡単に追加:
module Webhookable
extend ActiveSupport::Concern
def set_header
response.headers["Content-Type"] = "text/xml"
end
def render_twiml(response)
render text: response.text
end
end
最後に、ここでは、お reminder
行動のような利用、Twilioの逸品をTwiMLとの懸念を描画するのにこのオブジェクトとしてテキスト:
def reminder
response = Twilio::TwiML::Response.new do |r|
r.Gather :action => BASE_URL + '/directions', :numDigits => 1 do |g|
g.Say 'Hello this is a call from Twilio. You have an appointment
tomorrow at 9 AM.'
g.Say 'Please press 1 to repeat this menu. Press 2 for directions.
Or press 3 if you are done.'
end
end
render_twiml response
end
所属していません StackOverflow