africastalking
Advanced tools
Comparing version 0.5.5 to 0.5.6
623
lib/sms.js
@@ -10,389 +10,388 @@ 'use strict'; | ||
function SMS(options) { | ||
const _self = this; | ||
const _self = this; | ||
this.options = options; | ||
this.options = options; | ||
this._send = function (params, isBulk, isPremium) { | ||
let validationError; | ||
this._send = function (params, isBulk, isPremium) { | ||
// Validate params | ||
const _validateParams = function () { | ||
const constraints = { | ||
to: function (value, attributes, attributeName, options, constraints) { | ||
if (validate.isEmpty(value)) { | ||
return { | ||
presence: { message: 'is required' }, | ||
}; | ||
} | ||
let validationError; | ||
// Validate params | ||
const _validateParams = function () { | ||
const constraints = { | ||
to: function (value, attributes, attributeName, options, constraints) { | ||
if (validate.isEmpty(value)) { | ||
return { | ||
presence: {message: "is required"} | ||
}; | ||
} | ||
if (!validate.isArray(value) && !validate.isString(value)) { | ||
return { | ||
format: "must be a string or an array strings (phone numbers)" | ||
}; | ||
} | ||
if (validate.isString(value)) { | ||
let isInvalid = !/^\+\d{1,3}\d{3,}$/.test(value); | ||
if (isInvalid) { | ||
return { | ||
format: "must be a valid phone number" | ||
}; | ||
} | ||
} | ||
if (validate.isArray(value)) { | ||
let foundInvalid = false; | ||
value.forEach(function (phone) { | ||
foundInvalid = !/^\+\d{1,3}\d{3,}$/.test(phone); | ||
}); | ||
if (foundInvalid) { | ||
return { | ||
format: "must NOT contain invalid phone number" | ||
} | ||
} | ||
} | ||
return null; | ||
}, | ||
from: { | ||
isString: true | ||
}, | ||
message: { | ||
presence: true | ||
} | ||
if (!validate.isArray(value) && !validate.isString(value)) { | ||
return { | ||
format: 'must be a string or an array strings (phone numbers)', | ||
}; | ||
} | ||
if (isBulk) { | ||
constraints.enqueue = { | ||
inclusion: [true, false] | ||
}; | ||
if (validate.isString(value)) { | ||
let isInvalid = !/^\+\d{1,3}\d{3,}$/.test(value); | ||
if (isInvalid) { | ||
return { | ||
format: 'must be a valid phone number', | ||
}; | ||
} | ||
} | ||
if (isPremium) { | ||
constraints.keyword = { | ||
presence: true, | ||
isString: true | ||
}; | ||
constraints.linkId = { | ||
presence: false, | ||
isString: true | ||
}; | ||
constraints.retryDurationInHours = { | ||
numericality: true | ||
} | ||
if (validate.isArray(value)) { | ||
let foundInvalid = false; | ||
value.forEach(function (phone) { | ||
foundInvalid = !/^\+\d{1,3}\d{3,}$/.test(phone); | ||
}); | ||
if (foundInvalid) { | ||
return { | ||
format: 'must NOT contain invalid phone number', | ||
}; | ||
} | ||
} | ||
const error = validate(params, constraints); | ||
if (error) { | ||
let msg = ""; | ||
for (let k in error) { | ||
msg += error[k] + "; "; | ||
} | ||
validationError = new Error(msg); | ||
} | ||
return null; | ||
}, | ||
from: { | ||
isString: true, | ||
}, | ||
message: { | ||
presence: true, | ||
}, | ||
}; | ||
if (isBulk) { | ||
constraints.enqueue = { | ||
inclusion: [true, false], | ||
}; | ||
} | ||
_validateParams(); | ||
if (isPremium) { | ||
constraints.keyword = { | ||
presence: true, | ||
isString: true, | ||
}; | ||
constraints.linkId = { | ||
presence: false, | ||
isString: true, | ||
}; | ||
constraints.retryDurationInHours = { | ||
numericality: true, | ||
}; | ||
} | ||
// Multiple recipients? | ||
if (validate.isArray(params.to)) { | ||
if (params.to.length === 1) { | ||
params.to = params.to[0]; | ||
} else { | ||
params.to = params.to.join(); | ||
} | ||
const error = validate(params, constraints); | ||
if (error) { | ||
let msg = ''; | ||
for (let k in error) { | ||
msg += error[k] + '; '; | ||
} | ||
validationError = new Error(msg); | ||
} | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
_validateParams(); | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
// Multiple recipients? | ||
if (validate.isArray(params.to)) { | ||
if (params.to.length === 1) { | ||
params.to = params.to[0]; | ||
} else { | ||
params.to = params.to.join(); | ||
} | ||
} | ||
const body = { | ||
username: _self.options.username, | ||
to: params.to, | ||
message: params.message | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
if (params.from) { | ||
body.from = params.from | ||
} | ||
const body = { | ||
username: _self.options.username, | ||
to: params.to, | ||
message: params.message, | ||
}; | ||
if (isBulk) { | ||
body.bulkSMSMode = 1; | ||
if (params.enqueue) { | ||
body.enqueue = 1; | ||
} | ||
if (params.enqueue === false) { | ||
body.enqueue = 0; | ||
} | ||
} | ||
if (params.from) { | ||
body.from = params.from; | ||
} | ||
if (isPremium) { | ||
body.bulkSMSMode = 0; | ||
body.keyword = params.keyword; | ||
body.linkId = params.linkId; | ||
if (params.retryDurationInHours) { | ||
body.retryDurationInHours = params.retryDurationInHours; | ||
} | ||
} | ||
if (isBulk) { | ||
body.bulkSMSMode = 1; | ||
if (params.enqueue) { | ||
body.enqueue = 1; | ||
} | ||
if (params.enqueue === false) { | ||
body.enqueue = 0; | ||
} | ||
} | ||
const url = isBulk | ||
? Common.SMS_URL | ||
: Common.CONTENT_URL + '/messaging'; | ||
if (isPremium) { | ||
body.bulkSMSMode = 0; | ||
body.keyword = params.keyword; | ||
body.linkId = params.linkId; | ||
if (params.retryDurationInHours) { | ||
body.retryDurationInHours = params.retryDurationInHours; | ||
} | ||
} | ||
const rq = unirest.post(url); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { // API returns CREATED on success!? | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
}; | ||
const url = isBulk ? Common.SMS_URL : Common.CONTENT_URL + '/messaging'; | ||
const rq = unirest.post(url); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { | ||
// API returns CREATED on success!? | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
SMS.prototype.send = function (params) { | ||
const opts = _.cloneDeep(params); | ||
SMS.prototype.send = function (params) { | ||
const opts = _.cloneDeep(params); | ||
if (Array.isArray(opts)) { | ||
const results = opts.map((opt) => { | ||
return this._send(opt, true, false); | ||
}); | ||
return Promise.allSettled(results).then((results) => { | ||
return results.map((result) => { | ||
if (result.status === 'fulfilled') { | ||
return result.value; | ||
} else { | ||
return { | ||
SMSMessageData: { | ||
Message: result.reason, | ||
status: 'failed', | ||
}, | ||
}; | ||
} | ||
}); | ||
}); | ||
} else { | ||
return this._send(opts, true, false); | ||
} | ||
}; | ||
SMS.prototype.sendBulk = function (params) { | ||
return this.send(params); | ||
return this.send(params); | ||
}; | ||
SMS.prototype.sendPremium = function (params) { | ||
const opts = _.cloneDeep(params); | ||
return this._send(opts, false, true); | ||
const opts = _.cloneDeep(params); | ||
return this._send(opts, false, true); | ||
}; | ||
SMS.prototype.fetchMessages = function (params) { | ||
const _self = this; | ||
const _self = this; | ||
const opts = _.cloneDeep(params) || {}; | ||
opts.lastReceivedId = opts.lastReceivedId || 0; | ||
const opts = _.cloneDeep(params) || {}; | ||
opts.lastReceivedId = opts.lastReceivedId || 0; | ||
return new Promise(function (resolve, reject) { | ||
const rq = unirest.get(Common.SMS_URL); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
rq.query({ | ||
'username': _self.options.username, | ||
'lastReceivedId': opts.lastReceivedId | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
const rq = unirest.get(Common.SMS_URL); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.query({ | ||
username: _self.options.username, | ||
lastReceivedId: opts.lastReceivedId, | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
}; | ||
SMS.prototype.createSubscription = function (params) { | ||
const _self = this; | ||
const opts = _.cloneDeep(params) || {}; | ||
const _self = this; | ||
const opts = _.cloneDeep(params) || {}; | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true | ||
}, | ||
phoneNumber: { | ||
presence: true, | ||
isString: true | ||
} | ||
}; | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
phoneNumber: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
}; | ||
const validationError = validate(opts, constraints); | ||
const validationError = validate(opts, constraints); | ||
const body = { | ||
username: _self.options.username, | ||
shortCode: opts.shortCode, | ||
keyword: opts.keyword, | ||
phoneNumber: opts.phoneNumber | ||
}; | ||
const body = { | ||
username: _self.options.username, | ||
shortCode: opts.shortCode, | ||
keyword: opts.keyword, | ||
phoneNumber: opts.phoneNumber, | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/create'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { // API returns CREATED on success!? | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/create'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.send(body); | ||
rq.end(function (resp) { | ||
if (resp.status === 201) { | ||
// API returns CREATED on success!? | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
}; | ||
SMS.prototype.fetchSubscription = function (params) { | ||
const _self = this; | ||
const opts = _.cloneDeep(params) || {}; | ||
const _self = this; | ||
const opts = _.cloneDeep(params) || {}; | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true | ||
}, | ||
lastReceivedId: { | ||
numericality: true | ||
} | ||
}; | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
lastReceivedId: { | ||
numericality: true, | ||
}, | ||
}; | ||
const validationError = validate(opts, constraints); | ||
const validationError = validate(opts, constraints); | ||
opts.lastReceivedId = opts.lastReceivedId || 0; | ||
opts.lastReceivedId = opts.lastReceivedId || 0; | ||
return new Promise(function (resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
// throw validation error inside the promise chain | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
// throw validation error inside the promise chain | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
const rq = unirest.get(Common.CONTENT_URL + '/subscription'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format | ||
}); | ||
rq.query({ | ||
'username': _self.options.username, | ||
'lastReceivedId': opts.lastReceivedId, | ||
'keyword': opts.keyword, | ||
'shortCode': opts.shortCode | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
const rq = unirest.get(Common.CONTENT_URL + '/subscription'); | ||
rq.headers({ | ||
apikey: _self.options.apiKey, | ||
Accept: _self.options.format, | ||
}); | ||
rq.query({ | ||
username: _self.options.username, | ||
lastReceivedId: opts.lastReceivedId, | ||
keyword: opts.keyword, | ||
shortCode: opts.shortCode, | ||
}); | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
}); | ||
}; | ||
SMS.prototype.deleteSubscription = function (params) { | ||
const _self = this; | ||
const options = _.cloneDeep(params); | ||
let validationError; | ||
const _self = this; | ||
const options = _.cloneDeep(params); | ||
let validationError; | ||
const _validateParams = function () { | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true | ||
}, | ||
phoneNumber: { | ||
presence: true, | ||
isString: true | ||
} | ||
} | ||
const _validateParams = function () { | ||
const constraints = { | ||
shortCode: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
keyword: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
phoneNumber: { | ||
presence: true, | ||
isString: true, | ||
}, | ||
}; | ||
const error = validate(options, constraints); | ||
const error = validate(options, constraints); | ||
const makeErrorMessage = function (error) { | ||
let msg = ""; | ||
for (let k in error) { | ||
msg += error[k] + "; "; | ||
} | ||
validationError = new Error(msg); | ||
} | ||
const makeErrorMessage = function (error) { | ||
let msg = ''; | ||
for (let k in error) { | ||
msg += error[k] + '; '; | ||
} | ||
validationError = new Error(msg); | ||
}; | ||
if (error) { | ||
makeErrorMessage(error); | ||
} | ||
if (error) { | ||
makeErrorMessage(error); | ||
} | ||
}; | ||
_validateParams(); | ||
_validateParams(); | ||
return new Promise(function (resolve, reject) { | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
const { | ||
username, | ||
apiKey, | ||
format | ||
} = _self.options; | ||
const { username, apiKey, format } = _self.options; | ||
const { | ||
shortCode, | ||
keyword, | ||
phoneNumber | ||
} = options; | ||
const { shortCode, keyword, phoneNumber } = options; | ||
const body = { | ||
username, | ||
shortCode, | ||
keyword, | ||
phoneNumber | ||
} | ||
const body = { | ||
username, | ||
shortCode, | ||
keyword, | ||
phoneNumber, | ||
}; | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/delete'); | ||
const rq = unirest.post(Common.CONTENT_URL + '/subscription/delete'); | ||
rq.headers({ | ||
apiKey: apiKey, | ||
Accept: format, | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}); | ||
rq.headers({ | ||
apiKey: apiKey, | ||
Accept: format, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}); | ||
rq.send(body); | ||
rq.send(body); | ||
rq.end(function(resp) { | ||
if (resp.status === 200) { | ||
// API deleted successfully | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}) | ||
rq.end(function (resp) { | ||
if (resp.status === 200) { | ||
// API deleted successfully | ||
resolve(resp.body); | ||
} else { | ||
reject(resp.body || resp.error); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
module.exports = SMS; |
{ | ||
"name": "africastalking", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"description": "Official AfricasTalking node.js API wrapper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # Africa's Talking Node.js SDK | ||
Take a look at the [API docs here](http://docs.africastalking.com). | ||
Take a look at the [API docs here](http://developers.africastalking.com). | ||
@@ -102,5 +102,41 @@ | ||
- `send({ to, from, message, enqueue })`: Send a message | ||
- Send a message to one recipient. | ||
```javascript | ||
send({ | ||
to: '+xxxxxxxxxxxx', | ||
from: 'XYZ LTD', | ||
message: 'Hello world', | ||
enqueue: true, | ||
}); | ||
``` | ||
- `to`: Recipients phone number. `REQUIRED` | ||
- Send a message to multiple recipients. | ||
```javascript | ||
send({ | ||
to: ['+xxxxxxxxxxxx','+yyyyyyyyyyyy','+zzzzzzzzzzzz'], | ||
from: 'XYZ LTD', | ||
message: 'Hello world', | ||
enqueue: true, | ||
}); | ||
``` | ||
- Send different messages to different recipients. | ||
```javascript | ||
send([ | ||
{ | ||
to: ['+aaaaaaaaaaaa','+bbbbbbbbbbbb','+cccccccccccc'], | ||
from: 'XYZ LTD', | ||
message: 'Congratulations team! You have won it!', | ||
enqueue: true, | ||
}, | ||
{ | ||
to: '+xxxxxxxxxxxx', | ||
from: 'XYZ LTD', | ||
message: 'Congratulations coach! Your team has won!', | ||
enqueue: true, | ||
} | ||
]); | ||
``` | ||
- `to`: Recipient(s) phone number. `REQUIRED` | ||
- `from`: Shortcode or alphanumeric ID that is registered with Africa's Talking account | ||
@@ -110,3 +146,2 @@ - `message`: SMS content. `REQUIRED` | ||
- `sendPremium({ to, from, message, enqueue, keyword, linkId, retryDurationInHours })`: Send premium SMS | ||
@@ -113,0 +148,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3212
371
118468
17
1