From 14d313a13e569aa4ecf6cc72efc2804f3fd1f12a Mon Sep 17 00:00:00 2001 From: Mridul Date: Sat, 15 Jan 2022 00:51:05 +0000 Subject: [PATCH 1/2] Added Transactional SMS flow --- .gitignore | 4 ++- index.js | 3 ++- src/errorUtility.js | 2 ++ src/msg91SMS.js | 61 +++++++++++++++++++++++++++++++++++++++++++++ types/sms.d.ts | 21 ++++++++++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/msg91SMS.js create mode 100644 types/sms.d.ts diff --git a/.gitignore b/.gitignore index 62020fd..a7e349e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ npm-debug.log *.sublime-project *.sublime-workspace .vscode -out/ \ No newline at end of file +out/ +.devcontainer +test.js \ No newline at end of file diff --git a/index.js b/index.js index 3031063..6a599ca 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ 'use-strict'; const msg91OTP = require('./src/msg91OTP'); +const msg91SMS = require('./src/msg91SMS'); module.exports = { - msg91OTP, + msg91OTP,msg91SMS, }; diff --git a/src/errorUtility.js b/src/errorUtility.js index 1f8037d..1e7ae27 100644 --- a/src/errorUtility.js +++ b/src/errorUtility.js @@ -7,6 +7,8 @@ const errorCodes = {}; requestError('BAD_REQUEST_DATA', '%s', 400); requestError('NO_AUTH_KEY', 'You need to pass MSG91 auth key.', 400); requestError('NO_TEMPLATE_ID', 'You need to pass MSG91 valid template ID.', 400); +requestError('NO_FLOW_ID', 'You need to pass MSG91 valid flow ID.', 400); +requestError('NO_SENDER_ID', 'You need to pass MSG91 valid sender ID.', 400); function requestError(code, message, statusCode = 400, error_data = {}, Base = Error) { if (!code) throw new Error('Backend error code must not be empty'); diff --git a/src/msg91SMS.js b/src/msg91SMS.js new file mode 100644 index 0000000..0d2c814 --- /dev/null +++ b/src/msg91SMS.js @@ -0,0 +1,61 @@ +'use strict'; + +const makeRequest = require('./requestUtility'); +const { errorCodes } = require('./errorUtility'); +class Msg91Sms { + /** + * Creates a new SendSMS instance + * @param {string} authKey Authentication key. + * @param {string} FlowId of message to send. + */ + constructor(args = {}) { + if (!args.authKey) throw new errorCodes['NO_AUTH_KEY'](); + /* + * if (!args.flowId) throw new errorCodes['NO_FLOW_ID'](); + * if (!args.senderId) throw new errorCodes['NO_SENDER_ID'](); + */ + this.authKey = args.authKey; + + this.baseUrl = args.baseUrl || 'https://api.msg91.com/api'; + this.apiVersion = args.apiVersion || 'v5'; + + this.APIs = { + sendSms: { url: `${this.baseUrl}/${this.apiVersion}/flow`, method: 'POST' }, + }; + + this.send = this.send.bind(this); + // this.makeRequest = this.makeRequest.bind(this); + } + + /** + * Send Otp to given mobile number + * @param {string} contactNumber receiver's mobile number along with country code + * @param {object, optional} args + * @param {object, optional} custom_vars custom varaiables for DLT templates + * Return promise + */ + async send(contactNumber, args = {}, custom_vars = {}) { + const smsParams = { + mobiles: contactNumber, + flow_id: args.flow_id, + sender: args.sender, + }; + for (const [key, value] of Object.entries(custom_vars)) + smsParams[key]=value; + + const options = { + url: this.APIs['sendSms'].url, + method: this.APIs['sendSms'].method, + params: '', + headers: { + 'authkey': this.authKey, + 'Content-Type': 'application/json', + }, + postData: JSON.stringify(smsParams), + }; + return await makeRequest(options); + } + +} + +module.exports = Msg91Sms; diff --git a/types/sms.d.ts b/types/sms.d.ts new file mode 100644 index 0000000..9504d17 --- /dev/null +++ b/types/sms.d.ts @@ -0,0 +1,21 @@ +interface IMsg91SmsConstructorArgs { + authKey: string; + templateId: string; + baseUrl?: string; + apiVersion?: string; + otpExpiry?: number; + otpLength?: number; +} + +interface IMsg91SendSmsArgs { + flow_id: string; + sender: string; +} + +declare class Msg91Sms { + constructor(args: IMsg91SmsConstructorArgs); + + send(contactNumber: string | number, args?: IMsg91SendSmsArgs, custom_vars?: any): Promise; +} + +export { Msg91Sms as msg91SMS }; \ No newline at end of file From 65e496f51fba9b6594f08cae032da0169d05bea4 Mon Sep 17 00:00:00 2001 From: Mridul Date: Sat, 15 Jan 2022 06:42:33 +0530 Subject: [PATCH 2/2] Updated changelog and version --- CHANGELOG.md | 3 +++ package.json | 2 +- types/index.d.ts | 22 ++++++++++++++++++++++ types/sms.d.ts | 21 --------------------- 4 files changed, 26 insertions(+), 22 deletions(-) delete mode 100644 types/sms.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb1cb7..81246b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,6 @@ - followed some basic eslint rules - Modified Readme with aysnc/await examples - Error handling + +#### 2.0.3 (Jan 15, 2022) +- Added support for Transactional SMS flow \ No newline at end of file diff --git a/package.json b/package.json index e55db6c..1a43e60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "msg91-lib", - "version": "2.0.2", + "version": "2.0.3", "description": "Unofficial msg91 library to send OTP & messages", "main": "index.js", "scripts": { diff --git a/types/index.d.ts b/types/index.d.ts index 55cf151..bd1365d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -27,3 +27,25 @@ declare class Msg91Otp { } export { Msg91Otp as msg91OTP }; + +interface IMsg91SmsConstructorArgs { + authKey: string; + templateId: string; + baseUrl?: string; + apiVersion?: string; + otpExpiry?: number; + otpLength?: number; +} + +interface IMsg91SendSmsArgs { + flow_id: string; + sender: string; +} + +declare class Msg91Sms { + constructor(args: IMsg91SmsConstructorArgs); + + send(contactNumber: string | number, args?: IMsg91SendSmsArgs, custom_vars?: any): Promise; +} + +export { Msg91Sms as msg91SMS }; diff --git a/types/sms.d.ts b/types/sms.d.ts deleted file mode 100644 index 9504d17..0000000 --- a/types/sms.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -interface IMsg91SmsConstructorArgs { - authKey: string; - templateId: string; - baseUrl?: string; - apiVersion?: string; - otpExpiry?: number; - otpLength?: number; -} - -interface IMsg91SendSmsArgs { - flow_id: string; - sender: string; -} - -declare class Msg91Sms { - constructor(args: IMsg91SmsConstructorArgs); - - send(contactNumber: string | number, args?: IMsg91SendSmsArgs, custom_vars?: any): Promise; -} - -export { Msg91Sms as msg91SMS }; \ No newline at end of file