javascript 如何将验证码发送到 node.js 中的手机号码?(像 nodemailer,但对于 SMS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14788125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:34:42  来源:igfitidea点击:

How can I send verfication codes to mobile numbers in node.js? (Like nodemailer, but for SMS)

javascriptnode.jssms-gateway

提问by silvesterprabu

I have sent confirmation e-mails to users by using nodemailerfor my project. It is working fine.

我已经通过为我的项目使用nodemailer向用户发送了确认电子邮件。它工作正常。

Now I am looking to send verification codes to mobile numbers from node.js, but I don't know how to do that.

现在我想从 node.js 向手机号码发送验证码,但我不知道该怎么做。

Is there any module to send verification codes to mobile numbers, like nodemailer does with e-mail addresses? Or if not, how can I do this myself?

是否有任何模块可以向手机号码发送验证码,就像 nodemailer 发送电子邮件地址一样?或者如果没有,我怎么能自己做到这一点?

I developing my project using node.js and mongodb, JavaScript and jQuery.

我使用 node.js 和 mongodb、JavaScript 和 jQuery 开发我的项目。

回答by JWK

Most carriers provide a SMS gateway to which you can send an email and have it arrive as SMS. If you want a free way of sending SMS that works with your current nodemailer implementation, this is probably your best option. Otherwise, you might want to search for paid SMS services that you can integrate with.

大多数运营商都提供 SMS 网关,您可以向该网关发送电子邮件并使其以 SMS 的形式到达。如果您想要一种与您当前的 nodemailer 实现配合使用的免费短信发送方式,这可能是您的最佳选择。否则,您可能想要搜索可以集成的付费 SMS 服务。

Here is a list of SMS gateways: http://en.wikipedia.org/wiki/List_of_SMS_gateways

这是 SMS 网关的列表:http: //en.wikipedia.org/wiki/List_of_SMS_gateways

From the linked Wikipedia page: For instance, to send to a number typically expressed in the USA as 987-555-0100, one would email 9875550100@SMS-gateway.

从链接的维基百科页面:例如,要发送到一个在美国通常表示为 987-555-0100 的号码,可以发送电子邮件至 9875550100@SMS-gateway。

回答by BSB

NodeJS package https://www.npmjs.com/package/springedgewill be easy to send sms. You can install as

NodeJS 包https://www.npmjs.com/package/springedge将很容易发送短信。您可以安装为

npm install springedge

Code example of sending sms:

发送短信的代码示例:

// send sms 

var springedge = require('springedge');

var params = {
  'apikey': '', // API Key 
  'sender': 'SEDEMO', // Sender Name 
  'to': [
    '919019xxxxxxxx'  //Moblie Number 
  ],
  'message': 'test+message'
};

springedge.messages.send(params, 5000, function (err, response) {
  if (err) {
    return console.log(err);
  }
  console.log(response);
});

回答by leggetter

To ensure you can reach a users mobile no matter their location and network it's likely you're going to have to look at a paid service such as Nexmo(who I work for) or Twilio.

为了确保无论用户的位置和网络如何,您都可以访问他们的移动设备,您可能需要考虑付费服务,例如Nexmo(我为之工作)或Twilio

With these services you can either build your own verification (2FA - Two Factor Authentication) workflow:

使用这些服务,您可以构建自己的验证(2FA - 双因素身份验证)工作流程:

  1. User enters their phone number in a form and submits to your app
  2. Your app receives the phone number
  3. You send the user an SMS with an auth code
  4. User receives the auth code
  5. User enters auth code into a form and submits to your app
  6. Your app receives the auth code and checks that auth code against the phone number (maybe using the current session phone number)
  7. If the auth code is the one you are expected then the user is validated
  1. 用户在表单中输入他们的电话号码并提交给您的应用
  2. 您的应用收到电话号码
  3. 您向用户发送带有授权码的 SMS
  4. 用户收到验证码
  5. 用户在表单中输入验证码并提交给您的应用
  6. 您的应用收到验证码并根据电话号码检查该验证码(可能使用当前会话电话号码)
  7. 如果验证码是您所期望的,则用户已通过验证

Or you can use their 2FA authentication products (Nexmo - Verifyor Twilio - Authythat should help simplify this workflow.

或者您可以使用他们的 2FA 身份验证产品(Nexmo - Verify或 Twilio - Authy,它们应该有助于简化此工作流程。

Using Nexmo verify the code would be:

使用 Nexmo 验证代码将是:

Send Verification Request

发送验证请求

var Nexmo = require('nexmo');
var nexmo = new Nexmo({apiKey: API_KEY, apiSecret: API_SECRET});

var verifyRequestId = null; // use in the check process

nexmo.verify.request({number: TO_NUMBER, brand: APP_NAME}, function(err, result) {
  if(err) { console.error(err); }
  else {
    verifyRequestId = result. request_id;
  }
});

Check Authentication Code

检查验证码

nexmo.verify.control({request_id: verifyRequestId, cmd: 'cancel'}, function(err, result) {
  if(err) { console.error(err); }
  else {
    console.log(result);
  }
});

回答by user9343019

For a testing you should use this API https://www.fast2sms.comhowever you can use it as a business too. Before running the below code make sure you have generated valid API Authorization Key and for that you should register for free.

对于测试,您应该使用此 API https://www.fast2sms.com,但是您也可以将其用作业务。在运行以下代码之前,请确保您已生成有效的 API 授权密钥,为此您应该免费注册。

Now place receiver's mobile number for bulk message to and you can use it as a verification and OTP purpose too.

现在将接收者的手机号码用于批量消息,您也可以将其用作验证和 OTP 目的。

Install npm module in your project, where you want to implement an SMS system.

在你想要实现短信系统的项目中安装 npm 模块。

npm install unirest

Code for GET method:

GET 方法的代码:

var unirest = require("unirest");

var req = unirest("GET", "https://www.fast2sms.com/dev/bulk");

req.query({
  "authorization": "YOUR_API_KEY",
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.headers({
  "cache-control": "no-cache"
});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});

Code for POST method:

POST 方法的代码:

var unirest = require("unirest");

var req = unirest("POST", "https://www.fast2sms.com/dev/bulk");

req.headers({
  "authorization": "YOUR_API_KEY"
});

req.form({
  "sender_id": "FSTSMS",
  "message": "This is a test message",
  "language": "english",
  "route": "p",
  "numbers": "9999999999,8888888888,7777777777",
});

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  console.log(res.body);
});