[英]Rails - How to send confirmation code via SMS using nexmo api
我們正在創建一個 Rails 應用程序來實現 Nexmo API。 在注冊過程中,我們需要使用Nexmo API從服務器端通過短信向用戶發送確認碼。 但我們對此一無所知。 我們只想為印度實施此功能。
我使用過https://github.com/dotpromo/nexmos gem,我的代碼是:
# Gemfile
gem 'nexmos'
#sms_controller.rb:
class SmsController < ApplicationController
def new
@sms = Sms.new
end
def createclient = ::Nexmos::Message.new('#', '#')
res = client.send_text(from: '+910000000000', to: '+910000000000', text: 'Hello world!')
if res.success?
puts "ok"
else
puts "fail"
end
end
end
我用我的“key”和“Secret”憑據代替了“#”,並用我的電話號碼替換了 from 和 to。 但它不起作用。 即使在沒有向其他號碼發送短信后,成功塊也在執行。
任何人都可以指導我們實施這一點嗎?
好吧,您似乎想要使用為 Nexmo 構建的Ruby 庫。
基本上你要做的是把這個示例代碼:
nexmo = Nexmo::Client.new('...API KEY...', '...API SECRET...')
response = nexmo.send_message({
from: 'RUBY',
to: '...NUMBER...',
text: 'Hello world'
})
if response.success?
puts "Sent message: #{response.message_id}"
elsif response.failure?
raise response.error
end
..在你的控制器中的一個動作中。 假設它是您的TextsController
,並且您將其置於 create 操作下。 所以在你的config/routes.rb
,輸入如下內容:
match "/sendtext" => "texts#create"
然后,您只需點擊mydomain.com/sendtext
觸發該命令。
通過瓦拉蒂斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.