Rails-如何使用nexmo API通过SMS发送确认代码 [英] Rails - How to send confirmation code via SMS using nexmo api

查看:261
本文介绍了Rails-如何使用nexmo API通过SMS发送确认代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在创建用于实现Nexmo API的Rails应用程序.在注册过程中,我们需要使用Nexmo API通过SMS从服务器端向用户发送确认代码. 但是我们对此一无所知.我们只想在印度实施此功能.

We are creating a Rails application for implement the Nexmo API. In registration process, we need to send confirmation code through SMS to the user from server-side using Nexmo API. But we don't have any idea about this. We wanted to implement this feature for India only.

我使用了 https://github.com/dotpromo/nexmos gem,我的代码是:

I have used https://github.com/dotpromo/nexmos gem and my code is:

# 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发送给其他号码,成功块也正在执行.

I have used my credentials for "key" and "Secret" in-place of "#" and replace from and to with my phone numbers. but it doesn't works. Even after not delivering the SMS to other numbers, the success block is being executed.

任何人都可以指导我们实现这一目标吗?.

Can anybody guide us to implement this?.

推荐答案

看起来您想使用基本上,您需要做的是以下示例代码:

Essentially what you'd do is put this sample code:

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中,输入以下内容:

..inside an action in your controller. Let's say it's your TextsController, and you put it under the create action. So in your config/routes.rb, put something like:

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

然后,您只需按mydomain.com/sendtext,该命令就会触发.

Then, you just hit mydomain.com/sendtext and the command should fire.

通过瓦拉蒂斯

这篇关于Rails-如何使用nexmo API通过SMS发送确认代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆