Rails 3 XML 生成器/Twilio API
此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里是 Twilio 员工。自从这个最初的问题发布以来,Rails 发生了很多变化,我想分享如何使用 Rails 4、Concerns 和 Twilio Ruby gem 解决这个问题。
在下面的代码示例中,我在
/controllers/voice_controller.rb
中定义控制器,并包含一个名为 Webhookable 的关注点。 Webhookable Concern 让我们将与 Twilio webhooks 相关的逻辑(将 HTTP 响应标头设置为 text/xml、呈现 TwiML、验证请求是否源自 Twilio 等)封装到单个模块中。关注点本身位于
/controllers/concerns/webhookable.rb
中,并且相当简单。现在,它只是将所有操作的 Content-Type 设置为 text/xml,并提供渲染 TwiML 对象的方法。我没有包含用于验证请求是否源自 Twilio 的代码,但这很容易添加:最后,您的
reminder
操作可能如下所示,使用 Twilio gem 生成 TwiML 并使用将此对象呈现为文本的关注点: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.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: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:Twilio 不会在其请求中发送 Accept HTTP 标头,这会导致 Rails 3以确定它无法以适当的内容类型进行响应。我认为以下内容可以帮助您解决这个问题:
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: