2factor 中文文档教程
2factor-node
使用 2factor.in API 在印度发送交易和 OTP 短信的 NodeJS 模块
Installation
要安装库,请使用
$ npm i 2factor --save
要安装库并将其保存为 package.json 中的依赖项,请使用
$ npm i 2factor --save
New in this version (1.0.6)
现在支持开放模板/交易短信。 将添加对 OTP 消息的支持,但我没有 现在没有任何积分,买不起。
Usage
该模块导出一个返回新的 TwoFactor 实例的构造函数。 构造函数期望将 api key
传递给它。
const TwoFactor = new (require('2factor'))(<your api key>)
注意 - 您不应将 API 密钥存储在您的代码或您将提交的文本文件中 你的存储库。 理想情况下,您希望使用如下所示
let APIKEY = process.env.my_api_key || ''
if (APIKEY === '') {
throw new Error('Missing 2Factor api key in environment')
}
const TwoFactor = new(require('2factor'))(APIKEY)
To get your balance -
的环境变量 - balance()
方法采用单个字符串参数来表示您要检索的余额类型。 要获取所有余额,请将类型指定为 ALL
或省略参数。
TwoFactor.balance().then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send an sms otp -
只需使用电话号码和包含 otp 和模板字段。 它将返回一个使用 sessionId 解析的 Promise
或拒绝并说明失败原因。
TwoFactor.sendOTP(<phone number>, {otp: <otp code>, template: <template_name>}).then((sessionId) => {
console.log(sessionId)
}, (error) => {
console.log(error)
})
To verify an sms otp -
使用 sendOTP
函数返回的 sessionId 和 otp 调用 verifyOTP()
你想检查。 如果 otp 是正确的,它将通过成功消息解析 它将拒绝 2Factor.in 的回复
TwoFactor.verifyOTP(sessionId, otp).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send a template SMS to a single user
TwoFactor.sendTemplate('123456789', 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send a template SMS to a multiple users
TwoFactor.sendTemplate(['123456789','987654321'], 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send an transactional / open template SMS -
请确保为您的帐户启用了开放模板短信。 如果不是,请为模板创建一个新请求并指定您希望用于的发件人 ID 发送打开模板短信,并在描述中写上您要启用打开 模板发送即。 动态内容。 如果您发送给单个用户,您可以将电话号码作为第一个参数。
TwoFactor.sendTransactional(["1234567890", "2103456789"], "Your message", 'YOUR SENDER ID')
.then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
Author
Hanut Singh Gusain hanutsingh@gmail.com [http://www.hanutsingh.in]
2factor-node
NodeJS module for using the 2factor.in APIs for sending transactional and OTP sms in india
Installation
To install the library use
$ npm i 2factor --save
To install the library and save it as a dependency in package.json use
$ npm i 2factor --save
New in this version (1.0.6)
Open template / transactional SMS are now supported. Will add support for OTP messages but I don't have any credits for it right now and can't afford to buy them.
Usage
The module exports a constructor that returns new TwoFactor instances. The constructor expects the api key
to be passed to it.
const TwoFactor = new (require('2factor'))(<your api key>)
Note - You shouldn't store your API key in your code or in text files you will commit to your repositories. Ideally, you want to use environment variables as given below -
let APIKEY = process.env.my_api_key || ''
if (APIKEY === '') {
throw new Error('Missing 2Factor api key in environment')
}
const TwoFactor = new(require('2factor'))(APIKEY)
To get your balance -
The balance()
method takes a single string parameter for the type of balance you want to retrieve. To get all balances, specify type as ALL
or omit the parameter.
TwoFactor.balance().then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send an sms otp -
Simply call the sendOTP
function with a phone number and options object containing the otp and template fields. It will return a Promise
that resolves with the sessionId or reject with the reason for failure.
TwoFactor.sendOTP(<phone number>, {otp: <otp code>, template: <template_name>}).then((sessionId) => {
console.log(sessionId)
}, (error) => {
console.log(error)
})
To verify an sms otp -
Call verifyOTP()
with the sessionId returned from the sendOTP
function and the otp you want to check. If the otp was correct, it will resolve with a success message else it will reject with the response from 2Factor.in
TwoFactor.verifyOTP(sessionId, otp).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send a template SMS to a single user
TwoFactor.sendTemplate('123456789', 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send a template SMS to a multiple users
TwoFactor.sendTemplate(['123456789','987654321'], 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
To send an transactional / open template SMS -
Please ensure that open template SMS is enabled for your account. If it is not, create a new request for a template and specify the senderid you wish to use for sending the Open Template SMS and write in the description that you want to enable open template sending ie. dynamic content. If you are sending to a single user, you can just put the phone number as the first parameter.
TwoFactor.sendTransactional(["1234567890", "2103456789"], "Your message", 'YOUR SENDER ID')
.then((response) => {
console.log(response)
}, (error) => {
console.log(error)
})
Author
Hanut Singh Gusain hanutsingh@gmail.com [http://www.hanutsingh.in]