Rails – 如何使用nexmo api通过SMS发送确认代码

我们正在创建一个用于实现Nexmo API的Rails应用程序。 在注册过程中,我们需要使用Nexmo API从服务器端通过SMS向用户发送确认代码。 但我们对此并不了解。 我们只想为印度实现此function。

我使用过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 

我已将“密钥”和“秘密”的凭据用于“#”,并替换为我的电话号码。 但它不起作用。 即使在未将SMS发送到其他号码之后,也正在执行成功块。

任何人都可以指导我们实施吗?

好吧,看起来你想要使用为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 ,你把它放在创建动作之下。 所以在你的config/routes.rb ,输入如下内容:

 match "/sendtext" => "texts#create" 

然后,你只需点击mydomain.com/sendtext ,命令就会触发。

通过varatis

Interesting Posts