0

I am having an issue with outbound SMS thru TwiML Message functions. I am able to see my post on my Sinatra server session here:

== Sinatra (v2.0.0) has taken the stage on 4567 for development with backup from WEBrick
[2018-01-24 15:30:55] INFO  WEBrick::HTTPServer#start: pid=67403 port=4567
<?xml version="1.0" encoding="UTF-8"?><Response><Message to="+1904XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message><Message to="+1786XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message><Message to="+1904XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message></Response>

50.235.219.155 - - [24/Jan/2018:15:31:16 -0500] "POST /message HTTP/1.1" 200 - 0.0022
::1 - - [24/Jan/2018:15:31:16 EST] "POST /message HTTP/1.1" 200 0
- -> /message

I see the inbound logs here, but nothing outbound. I have even elevated this to a paid account to make sure it wasn't a trail thing.

This code is based on this walkthru.

My full ruby code for the app is here:

require 'yaml'
require 'open-uri'
require 'sinatra'
require 'twilio-ruby'

MY_NUMBER = '+1904XXXXXXXX'

def spreadsheet_url
  'contacts.yml'
end

def sanitize(number)
  "+1" + number.gsub(/^1|\D/, "")
end

def data_from_spreadsheet
  file = open(spreadsheet_url).read
  YAML.load(file)
end

def contacts_from_spreadsheet
  contacts = {}
  data_from_spreadsheet.each do |entry|
    name = entry['name']
    number = entry['phone_number'].to_s
    contacts[sanitize(number)] = name
  end
  contacts
end

def contacts_numbers
  contacts_from_spreadsheet.keys
end

def contact_name(number)
  contacts_from_spreadsheet[number]
end

get '/' do
  "Devotional Broadcast is Up & Running!"
end

get '/message' do
  "Things are Working!"
end

post '/message' do
  from = params['From']
  body = params['Body']
  media_url = params['MediaUrl0']

  if from == MY_NUMBER
    twiml = send_to_contacts(body, media_url)
  else
    twiml = send_to_me(from, body, media_url)
  end

  content_type 'text/xml'
  puts twiml
end

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

Any help or insight would be great! Thanks!

nbkkb7x
  • 131
  • 1
  • 1
  • 8

1 Answers1

1

I think I got it. Swapped out puts for .to_s Documentation Example here: Receive & Reply to SMS & MMS

post '/message' do
  from = params['From']
  body = params['Body']
  media_url = params['MediaUrl0']

  if from == MY_NUMBER
    twiml = send_to_contacts(body, media_url)
  else
    twiml = send_to_me(from, body, media_url)
  end

  content_type 'text/xml'
  twiml.to_s
end

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
  response.to_s
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
  response.to_s
end
nbkkb7x
  • 131
  • 1
  • 1
  • 8