[英]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.