gpt4 book ai didi

ruby-on-rails - Rails 3 XML 生成器/Twilio API

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:02 25 4
gpt4 key购买 nike

此 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 员工。自发布这个原始问题以来,Rails 发生了一系列变化,我想分享您如何使用 Rails 4、Concerns 和 Twilio Ruby gem 解决这个问题。

在下面的代码示例中,我在 /controllers/voice_controller.rb 中定义了 Controller ,并包含了一个名为 Webhookable 的关注点。 Webhookable Concern 使我们能够将与 Twilio webhook 相关的逻辑(将 HTTP 响应 header 设置为 text/xml、呈现 TwiML、验证请求来自 Twilio 等)封装到单个模块中。

require 'twilio-ruby'

class VoiceController < ApplicationController
include Webhookable

after_filter :set_header

# controller code here

end

Concern 本身位于 /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

最后,这是您的提醒 操作可能看起来像使用 Twilio 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

关于ruby-on-rails - Rails 3 XML 生成器/Twilio API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com