1

このTwilioAPIサンプルコードはRails3では機能しません。

#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は正常に電話をかけたようですが(電話番号や場所などのパラメータを確認できます)、次のあいまいな応答コードを返します。

Completed 406 Not Acceptable in 0ms
4

2 に答える 2

3

Twilio の従業員です。この最初の質問が投稿されて以来、Rails には多くの変更が加えられています。Rails 4、Concerns、および Twilio Ruby gem を使用して、この問題にどのように対処できるかを共有したいと思います。

以下のコード サンプルでは、​​コントローラーを定義し、/controllers/voice_controller.rbWebhookable という名前の懸念を含めます。Webhookable Concern を使用すると、Twilio Webhook に関連するロジック (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、かなり単純です。現時点では、すべてのアクションに対して Content-Type を 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

最後に、reminderTwilio gem を使用して TwiML を生成し、Concern を使用してこのオブジェクトをテキストとしてレンダリングするアクションは次のようになります。

  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
于 2014-01-13T22:42:45.773 に答える
2

Twilio はリクエストでAccept HTTP ヘッダーを送信しないため、Rails 3 は適切なコンテンツ タイプで応答できないと判断します。ただし、次の方法でそれを回避できると思います。

# voice_controller.rb

  デフリマインダー
    @postto = BASE_URL + '/方向'

    render :content_type => 'application/xml'
  終わり
于 2010-10-06T22:21:58.163 に答える