简体   繁体   中英

send text message using twilio ruby and html

I need help trying to get the text message to send to the specific number that a user inputs I have my HTML where there is a textbox asking the user to enter a number and I have my ruby code I'm having trouble actually sending the text! I am using Heroku to deploy the app.I can't seem to understand where exactly I need to put each code I have it in index.erb and a ruby file

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>

ruby:

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',  
})

Add to Gemfile:

gem "twilio-ruby"

Run bundle install

Create config/initializers/twilio.rb within

Twilio.configure do |config|
  config.account_sid = ""
  config.auth_token = ""
end

Send a message:

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

First at all you write method="/POST" but it must be method="POST" and next you put your send sms code at wrong place. Try this:

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

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