TwiML导出到Sinatra,但实际上并没有发送短信

时间:2018-01-24 20:49:33

标签: ruby sinatra yaml twilio twiml

我遇到了通过TwiML消息功能的出站短信问题。我可以在我的Sinatra服务器会话上看到我的帖子:

== 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

我看到了inbound logs here,但没有出站。我甚至把它提升到一个付费帐户,以确保它不是一件小事。

此代码基于此walkthru

我的应用程序的完整ruby代码在这里:

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

任何帮助或见解都会很棒!谢谢!

1 个答案:

答案 0 :(得分:1)

我想我明白了。交换掉了.to_s文档示例: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