Rails 3 XML 生成器/Twilio API

发布于 2024-09-26 13:31:20 字数 689 浏览 4 评论 0原文

此 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

This Twilio API Sample code isn't working in 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

Any ideas?

Twilio seems to successfully make the phone call (I can see the params with my phone number, location, etc.) but then return this vague response code:

Completed 406 Not Acceptable in 0ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眸 2024-10-03 13:31:20

这里是 Twilio 员工。自从这个最初的问题发布以来,Rails 发生了很多变化,我想分享如何使用 Rails 4、Concerns 和 Twilio Ruby gem 解决这个问题。

在下面的代码示例中,我在 /controllers/voice_controller.rb 中定义控制器,并包含一个名为 Webhookable 的关注点。 Webhookable Concern 让我们将与 Twilio webhooks 相关的逻辑(将 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

最后,您的 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

Twilio employee here. There have been a bunch of changes to Rails since this original question was posted, and I wanted to share how you might tackle this problem using Rails 4, Concerns and the Twilio Ruby gem.

In the code sample below, I define the controller in /controllers/voice_controller.rb and include a Concern called Webhookable. The Webhookable Concern lets us encapsulate logic related to Twilio webhooks (setting the HTTP response header to text/xml, rendering TwiML, validating that requests originate from Twilio, etc) into a single module.

require 'twilio-ruby'

class VoiceController < ApplicationController
  include Webhookable

  after_filter :set_header

  # controller code here

end

The Concern itself lives in /controllers/concerns/webhookable.rb and is fairly simple. Right now it simply sets the Content-Type to text/xml for all action and provides a method to render the TwiML object. I haven't included the code to validate that requests are originating from Twilio, but that would be easy to add:

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

Finally, here's what your reminder action might look like using the Twilio gem to generate TwiML and using the Concern to render this object as text:

  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
咋地 2024-10-03 13:31:20

Twilio 不会在其请求中发送 Accept HTTP 标头,这会导致 Rails 3以确定它无法以适当的内容类型进行响应。我认为以下内容可以帮助您解决这个问题:

# voice_controller.rb

  def reminder
    @postto = BASE_URL + '/directions'

    render :content_type => 'application/xml'
  end

Twilio doesn't send an Accept HTTP header in its requests, which causes Rails 3 to decide that it can't respond with an appropriate content type. I think the following will get around that for you though:

# voice_controller.rb

  def reminder
    @postto = BASE_URL + '/directions'

    render :content_type => 'application/xml'
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文