Skip to content

Added transactional SMS flow #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: v2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ npm-debug.log
*.sublime-project
*.sublime-workspace
.vscode
out/
out/
.devcontainer
test.js
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
'use-strict';

const msg91OTP = require('./src/msg91OTP');
const msg91SMS = require('./src/msg91SMS');

module.exports = {
msg91OTP,
msg91OTP,msg91SMS,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions src/errorUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
61 changes: 61 additions & 0 deletions src/msg91SMS.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 22 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
}

export { Msg91Sms as msg91SMS };