使用twilio ruby​​和html发送短信

时间:2015-10-08 14:41:23

标签: ruby twilio

我需要帮助尝试将短信发送到用户输入的特定号码我有我的HTML,其中有一个文本框要求用户输入一个号码,我有我的红宝石代码我有麻烦实际发送文本!我正在使用Heroku来部署app.I似乎无法理解我需要将每个代码放在index.erb和ruby文件中

HTML:

<!doctype html>
<html lang="en">
<head>
<style>
input[type="text"] {  
  width:200px;
  display:block;
  margin:10px 0;
}
</style>
</head>
<body>
<h1>Please enter a number</h1>
<form action="/send" method="/POST">
  <input type="text" name="phone" placeholder="enter a phone number"/>
  <input type= "submit" value= "send me text!"
</form>
 </body>
</html>

红宝石:

require 'ruby gems'
require 'twilio-ruby'
require 'sinatra'


get '/' do 
erb :index
end 

post '/send' do 
to_number = params[:number]

end
account_sid = 'XXXXXXXXXXXXXXXXXXXX' 
auth_token = 'YYYYYYYYYYYYYYYYYYYY' 

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

@client.account.messages.create({
:from => '+12013409425', 
:to => to_number, 
:body => 'hey',  
})

1 个答案:

答案 0 :(得分:2)

添加到Gemfile:

gem "twilio-ruby"

运行bundle install

中创建config/initializers/twilio.rb
Twilio.configure do |config|
  config.account_sid = ""
  config.auth_token = ""
end

发送消息:

def action
  client = Twilio::REST::Client.new
  client.messages.create(from: ENV['TWILIO_FROM'], to: "+7999808630", body: "Say hello!")
end

首先你写method="/POST"但它必须是method="POST"然后你把你的发送短信代码放在错误的地方。试试这个:

require 'ruby gems'
require 'twilio-ruby'
require 'sinatra'


get '/' do 
erb :index
end 

post '/send' do 
  to_number = params[:number]
  account_sid = 'DO NOT POST REAL SID :)' 
  auth_token = 'DO NOT POST REAL TOKEN :)' 

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

  @client.account.messages.create({
    :from => '+12013409425', 
    :to => to_number, 
    :body => 'hey',  
  })
end