简体   繁体   中英

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). The app works fine locally using ngrok in dev and production mode.

Heroku is a different story. 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. Here is the class that has the sms being sent:

require '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. Same thing to discover that it doesn't run anything below the message block.

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.

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. I have been working on this for 30+ hours and am totally stumped.

--EDIT I noticed I didn't have the required code at the top of my notifications controller that was in the tutorial such as:

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. I put the method in a different class in the same notifications_controller file and called and it works.

But when it gets called the intended way the path is this:

  1. routes hits notifications#parse
  2. parse regexes for message starting with 'test'
  3. parse method then calls model method 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
  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. 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. 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. That value came from my ENV variables

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 . 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. 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? Though it does pass the 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"
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM