此Twilio API示例代码在Rails 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似乎成功地打了电话(我可以看到我的电话号码,位置等的参数),但是返回此模糊的响应代码:

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员工在这里。自从发布此原始问题以来,Rails发生了很多变化,我想分享您如何使用Rails 4,Conforts和Twilio Ruby Gem解决这个问题。

在下面的代码示例中,我定义了控制器 /controllers/voice_controller.rb 并包括一个称为Webhookable的关注点。 Webhook的关注使我们能够将与Twilio Webhooks相关的逻辑封装(将HTTP响应标头设置为文本/XML,渲染Twiml,验证该请求源自Twilio等的请求)。

require 'twilio-ruby'

class VoiceController < ApplicationController
  include Webhookable

  after_filter :set_header

  # controller code here

end

关注本身生活在 /controllers/concerns/webhookable.rb 而且很简单。现在,它只是将内容类型设置为所有操作的文本/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 Gem生成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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top