Search code examples
rubysinatratwiliongrok

NameError - uninitialized constant Twilio::TwiML::Response (Possibly from old API Code?)


I am in need of some help with setting up a Twilio SMS Broadcast App running on Sinatra. They build is based off this tutorial: Send Mass SMS Broadcasts in Ruby

When I make an HTTP POST I get this message in my Terminal when running Sinatra & Ngrok.

NameError - uninitialized constant Twilio::TwiML::Response
Did you mean?  Twilio::Response:
    broadcast.rb:75:in `send_to_me'
    broadcast.rb:53:in `block in <main>'

The code it is having an issue with is:

def send_to_contacts(body, media_url = nil)
  response = Twilio::TwiML::Response.new do |r|
    contacts_numbers.each do |num|
      r.Message to: num do |msg|
        msg.Body body
        msg.Media media_url unless media_url.nil?
      end
    end
  end
  response.text
end

def send_to_me(from, body, media_url = nil)
  name = contact_name(from)
  body = "#{name} (#{from}):\n#{body}"
  response = Twilio::TwiML::Response.new do |r|
    r.Message to: MY_NUMBER do |msg|
      msg.Body body
      msg.Media media_url unless media_url.nil?
    end
  end
  response.text
end

I have noticed most new Twilio walkthrus are now using API Auths & Tokens with an

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

Is this something I need to be implementing? Any guidance on how I can migrate these two methods to that type of format and keep my features?

Thanks!


Solution

  • Update:

    Twilio::TwiML::Response has been replaced by Twilio::TwiML::VoiceResponse & Twilio::TwiML::MessagingResponse. It worked when I changed the code to this:

    def send_to_contacts(body, media_url = nil)
      response = Twilio::TwiML::MessagingResponse.new do |r|
        contacts_numbers.each do |num|
          r.message to: num do |msg|
            msg.body body
            msg.media media_url unless media_url.nil?
          end
        end
      end
      puts response
    end
    
    def send_to_me(from, body, media_url = nil)
      name = contact_name(from)
      body = "#{name} (#{from}):\n#{body}"
      response = Twilio::TwiML::MessagingResponse.new do |r|
        r.message to: MY_NUMBER do |msg|
          msg.body body
          msg.media media_url unless media_url.nil?
        end
      end
      puts response
    end