Ruby on rails Twilio SMS不适用于rails 4
我已经按照 , 尝试在rails 4.0中发送SMS。我有一个带有简单Rails控制器的试用帐户,如下所示Ruby on rails Twilio SMS不适用于rails 4,ruby-on-rails,twilio,Ruby On Rails,Twilio,我已经按照 , 尝试在rails 4.0中发送SMS。我有一个带有简单Rails控制器的试用帐户,如下所示 class MessagesController < ApplicationController skip_before_filter :verify_authenticity_token # skip_before_filter :authenticate_user!, :only => "reply" def reply message_body =
class MessagesController < ApplicationController
skip_before_filter :verify_authenticity_token
# skip_before_filter :authenticate_user!, :only => "reply"
def reply
message_body = params["Body"]
from_number = params["From"]
boot_twilio
sms = @client.messages.create(
from: Rails.application.secrets.twilio_number,
to: from_number,
body: "Hello there, thanks for texting me. Your number is #{from_number}."
)
end
private
def boot_twilio
account_sid = Rails.application.secrets.twilio_sid
auth_token = Rails.application.secrets.twilio_token
@client = Twilio::REST::Client.new account_sid, auth_token
end
end
在我的路由表中,这里是Twilio开发者福音传道者 问题是您在Twilio控制台中输入了ngrok URL的位置。实际上,您已经在故障转移URL框中输入了它。如果你再看一看,你会发现当一条消息进来时,你正在使用一个名为“SMS Getting Started TwiML”的TwiML容器,这就是为什么你仍然收到你的教程回复的原因 您需要将左侧的下拉列表从“TwiML”更改为“Webhook”,然后在显示的输入中输入URL 第二点是,您正在使用RESTAPI来响应传入的消息,但是您可以使用TwiML来代替。只需将您的操作更新为:
def reply
from_number = params["From"]
resp = Twilio::TwiML::Response.new do |r|
r.Message "Hello there, thanks for texting me. Your number is #{from_number}."
end
render xml: resp.to_xml
end
如果有帮助,请告诉我。问题已解决。这也是一种更好的回答方式。谢谢,亲爱的!你介意把答案标记为正确的吗?这样其他看到这个问题的人也能看到它的效果?谢谢另外,不要忘记在
ApplicationController
中添加protect\u from\u forgery with::exception
。由于这个特性,我损失了将近2天的时间。您不想为Twilio webhook端点提供CSRF保护,因为webhook是一个跨站点请求,这是您所期望的。这就是为什么最初的问题在控制器中有跳过\u之前的\u过滤器:验证\u真实性\u令牌
。
def reply
from_number = params["From"]
resp = Twilio::TwiML::Response.new do |r|
r.Message "Hello there, thanks for texting me. Your number is #{from_number}."
end
render xml: resp.to_xml
end