簡體   English   中英

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

[英]send text message using twilio ruby and html

我需要嘗試將文本消息發送到用戶輸入的特定號碼的幫助,我在HTML里有一個文本框,要求用戶輸入數字,而我的紅寶石代碼在實際發送文本時遇到了麻煩! 我正在使用Heroku來部署應用程序,我似乎無法理解我到底需要將每個代碼放在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',  
})

添加到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" ,然后您將發送SMS代碼放在錯誤的位置。 嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM