简体   繁体   English

在运行时在heroku中发送文本时,Rails Twilio App会挂起,但在heroku(以及所有环境中的本地)的Rail console中可以运行

[英]Rails Twilio App hangs when sending text in heroku on run, but works in rail console on heroku (and locally in all environments)

I built a twilio-powered rails app that has a method which sends an SMS with parameters (to, from, body). 我构建了一个twilio驱动的rails应用程序 ,该应用程序具有一种发送带有参数(到,来自,正文)的SMS的方法。 The app works fine locally using ngrok in dev and production mode. 该应用程序可以在开发和生产模式下使用ngrok在本地正常运行。

Heroku is a different story. Heroku是一个不同的故事。 Once it gets to the part where the text message is built it doesn't go past it and the logs don't show any problems. 一旦到达生成文本消息的部分,它就不会越过文本,并且日志也不会显示任何问题。 It's like it stalls out and twilio gives up on waiting for a response after 15 seconds. 就像它停了下来,twilio放弃了等待15秒后等待响应。 Here is the class that has the sms being sent: 这是发送短信的类:

require 'twilio-ruby' 需要“ twilio-ruby”

class SmsActions
   def self.compose_message(to, from, body)

    account_sid = Rails.application.secrets.twilio_account_sid
    auth_token = Rails.application.secrets.twilio_auth_token

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

    message = @client.account.messages.create({
        from: from,
        to: to,
        body: body,
        statusCallback:  "http://fptracker.herokuapp.com/twilio/callback"
    })
end

I have used "puts" statements to log and to confirm that it sees the account_sid and auth_token. 我已经使用“ puts”语句进行记录并确认它可以看到account_sid和auth_token。 Same thing to discover that it doesn't run anything below the message block. 发现它在message块下没有任何运行的发现也是一样。

The weird thing is I can run hoerku run rails, input the exact same code that sends the text (hard-coding the account_sid and token) and it works. 奇怪的是,我可以运行hoerku run rails,输入发送文本的完全相同的代码(硬编码account_sid和token),并且可以正常工作。

So I don't think it's missing credentials, I don't think it's the middleware (because it works manually in heroku), it's not the production env because it works locally in production. 因此,我不认为它缺少凭据,也不认为它是中间件(因为它可以在heroku中手动运行),而不是生产环境,因为它可以在生产环境中本地运行。 I have been working on this for 30+ hours and am totally stumped. 我已经为此工作了30多个小时,并且完全陷入困境。

--EDIT I noticed I didn't have the required code at the top of my notifications controller that was in the tutorial such as: --EDIT我注意到本教程中的通知控制器顶部没有所需的代码,例如:

require 'twilio-ruby'

class NotificationsController < ApplicationController
   include Webhookable

   after_filter :set_header

   skip_before_action :verify_authenticity_token

But after adding that it still doesn't work. 但是添加之后,它仍然不起作用。

The second thing I noticed is that when I put the code to create the SMS directly in the controller, it works on heroku. 我注意到的第二件事是,当我将代码直接用于在控制器中创建SMS时,它可以在heroku上运行。 I put the method in a different class in the same notifications_controller file and called and it works. 我将该方法放在相同的notifications_controller文件中的另一个类中,然后调用它并起作用。

But when it gets called the intended way the path is this: 但是,当它被调用为预期方式时,路径是这样的:

  1. routes hits notifications#parse 路由点击通知#parse
  2. parse regexes for message starting with 'test' 解析正则表达式以获取以“ test”开头的消息
  3. parse method then calls model method Message.auto_reply 然后解析方法调用模型方法Message.auto_reply
  4. Message.auto_reply gets my twilio number from secrets and has a few puts statements and then calls SmsActions.compose_message Message.auto_reply从秘密中获取我的twilio编号,并具有一些puts语句,然后调用SmsActions.compose_message
  5. compose message is the class listed above and creates the text message. 撰写消息是上面列出的类,并创建文本消息。

So compose_message is the class that works in local environments but not on heroku. 因此compose_message是可在本地环境中使用的类,但不适用于heroku。 It does get hit, as the logs show the puts statements i put in there, but it freezes/stops on that Twilio message creation in heroku. 它确实受到了打击,因为日志显示了我放入的puts语句,但是它冻结/停止了在heroku中创建Twilio消息的操作。 Something is happening between the controller and that method. 控制器和该方法之间发生了某些情况。

Solved. 解决了。 It had nothing to do with Twilio exactly, but me passing a nil value in the "from" variable when trying to create the text message. 它与Twilio完全无关,但是我在尝试创建文本消息时在“ from”变量中传递了nil值。 That value came from my ENV variables 这个值来自我的ENV变量

from = Rails.application.secrets.twilio_number

When that is called from within the controller, it accesses the variable from environmental variables successfully, but it is nil when called from anywhere outside of the controller . 从控制器内部调用时,它成功访问了环境变量中的变量, 但是从控制器外部的任何位置调用时它为nil I don't know why yet. 我不知道为什么。 So this block below would get stuck in limbo with that nil value but nothing in heroku logs showed me that. 因此,下面的该块将被卡在该值为nil的边缘中,但是heroku日志中没有任何内容显示出这一点。 Maybe there is a way on Twilio's website to see that, but I doubt it if it can't associate it with a number? 也许在Twilio的网站上可以看到这种方式,但是我怀疑它是否不能将其与数字关联? Though it does pass the account_sid. 虽然确实通过了account_sid。

message = @client.account.messages.create({
    from: from, # this was passing in a nil value
    to: to,
    body: body,
    statusCallback:  "http://fptracker.herokuapp.com/twilio/callback"
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails应用程序在本地运行,但在部署到Heroku时则不行 - Rails app works locally but not when deployed to Heroku Rails App可在本地运行,但在Heroku上部署时无法运行 - Rails App works locally but not when deployed on Heroku Rails 4-应用程序可在本地运行,但不能在Heroku上运行 - Rails 4 - App works locally, but not on Heroku Heroku:Rails应用程序可在本地运行,但在Heroku上崩溃 - Heroku: Rails app works locally but crashes on heroku 通过STDIN提供输入时,heroku run rails控制台挂起 - heroku run rails console hangs when input is provided via STDIN Rails App搜索栏可在本地使用,但不能在Heroku上使用 - Rails App search bar works locally but not on Heroku Rails 应用程序在本地工作,但不适用于 Heroku 的生产环境 - Rails App works locally, but not in production with Heroku Rails应用程序在本地运行,在Heroku上崩溃 - Rails app works locally, crashes on Heroku 应用程序在本地工作但不在Heroku上工作 - App works locally but not on Heroku 我的应用程序在Heroku上无法正常运行,在运行“ heroku run rails test”时没有错误,但是在运行“ heroku run rails console”时出现许多错误 - My app is down on Heroku, no errors when run “heroku run rails test”, but many errors on running “heroku run rails console”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM