gpt4 book ai didi

ruby-on-rails - Twilio 短信表单适用于本地但未推送到 heroku - Ruby

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:33 24 4
gpt4 key购买 nike

这里是 ruby​​ on rails 的新手 - 一直无法弄清楚这个问题。这可能是非常简单的事情。

我已经使用 Twilio 的 api/sms 功能构建了一个 RoR 站点,所以我可以 send text messages给我的用户(使用 activeadmin 和设计)。一切似乎在本地运行良好,但当我推送到 Heroku 时,我遇到了问题。

在我的仪表板上的 activeadmin 内部,我添加了表单字段来发送短信( http://i.stack.imgur.com/TXLrq.png )

我有内部 View > 管理 > 仪表板 > _send_a_message.html.erb 和 _send_message_all.html.erb 我有

<%= form_tag('/send_sms', method: 'POST') do %>
<input type="text" placeholder="enter number" name="number">
<input type="text" placeholder="enter message here" name="message">
<button type="submit">Submit</button>
<% end %>

<%= form_tag('/send_sms', method: 'POST') do %>
<input type="text" placeholder="enter number" name="number">
<input type="text" placeholder="enter message here" name="message">
<button type="submit">Submit</button>
<% end %>

我将它们呈现到 activeadmin 仪表板中,然后在我的“TwilioController”中我有这两个:

def send_sms
message = params[:message]
number = params[:number]
account_sid = 'my sid'
auth_token = 'my token'

@client = Twilio::REST::Client.new account_sid, auth_token

@message = @client.account.messages.create ({:to => "+1"+"#{number}",
:from => "+my number",
:body => "#{message}"})
redirect_to '/admin/dashboard', :flash => { :success => "Your message sent!" }

rescue Twilio::REST::RequestError => e
puts e.message
end

def send_sms_all
user_phones = User.where(:subscribed => true).pluck(:phone)
message = params[:message]
account_sid = 'my sid'
auth_token = 'my token'

@client = Twilio::REST::Client.new account_sid, auth_token

user_phones.each do |number|
@client.account.messages.create ({:to => "+1"+"#{number}",
:from => "+my number",
:body => "#{message}"})
end

redirect_to '/admin/dashboard', :flash => { :success => "Your message sent!" }

rescue Twilio::REST::RequestError => e
puts e.message
end

我的路线是

post '/send_sms' => 'twilio#send_sms'
post '/send_sms_all' => 'twilio#send_sms_all'

这一切似乎都在本地正常工作 - 可以发送消息。当我推送到 heroku 并提交任一表单时,它会将我引导至/send_sms 页面,该页面会显示“我们很抱歉,但出了点问题。”页面并且不发送短信。

我的 heroku 日志中似乎没有任何“错误”指示。这个问题可能与 get vs post 有关吗?我已经尝试过 heroku run rake 和 db:migrate 以确保一切都已设置并且看起来很好。

有什么帮助吗?如果您需要查看其他内容,请告诉我。

更新:我最初调用的是我的 secrets.yml 文件,我的 token 和 SID 存储在该文件中,但 Heroku 不喜欢这样。在我的 production.rb 文件中,我有这个:config.middleware.use Rack::TwilioWebhookAuthentication, Rails.application.secrets.twilio_auth_token, '/send_sms', '/send_sms_all'

所以我将它们移到一个 .env 文件中,并改为这样调用它们:config.middleware.use Rack::TwilioWebhookAuthentication, ENV['TWILIO_AUTH_TOKEN'], '/send_sms', '/send_sms_all' 但是这给了我一个新一期“Twilio 请求验证失败”。我通过制作两个“//send_sms”、“//send_sms_all”来修复。

希望这一切对 future 的其他人有所帮助!感谢以下所有帮助过的人!

最佳答案

UPDATE: I originally was calling on my secrets.yml file where my token and SID were stored and Heroku didn't like that. In my production.rb file I had this: config.middleware.use
Rack::TwilioWebhookAuthentication,
Rails.application.secrets.twilio_auth_token, '/send_sms',
'/send_sms_all'

So I moved them into a .env file and called them like this instead:config.middleware.use Rack::TwilioWebhookAuthentication,
ENV['TWILIO_AUTH_TOKEN'], '/send_sms', '/send_sms_all'
BUT that gave me a new issue of "Twilio Request Validation Failed." which I fixed by making the two '//send_sms', '//send_sms_all'.

关于ruby-on-rails - Twilio 短信表单适用于本地但未推送到 heroku - Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330759/

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