我们正在创建一个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"one_answers"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
,命令应该启动。