Skip to content

Commit

Permalink
Send sms (#107)
Browse files Browse the repository at this point in the history
* Feat: Send sms using Twilio

- add test for sms
- update test phone number

* - test

* - remove empty line

* - move sms-notify-recipient.test to contract
  • Loading branch information
andreychuk authored and kostia-official committed May 24, 2017
1 parent f0abe69 commit 33f5785
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
},
"amqp": {
"url": "amqp://localhost"
},
"sms": {
"fromNumber": "OWNER_PHONE_NUMBER",
"accountSid":"TWILIO_ACCOUNT_SID",
"authToken":"TWILIO_AUTH_TOKEN"
}
}
5 changes: 5 additions & 0 deletions config/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@
},
"amqp": {
"url": "AMQP_URL"
},
"sms": {
"fromNumber": "OWNER_PHONE_NUMBER",
"accountSid":"TWILIO_ACCOUNT_SID",
"authToken":"TWILIO_AUTH_TOKEN"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"promdash": "1.1.0",
"raven": "0.12.1",
"simple-express-logger": "2.0.0",
"smart-config": "0.8.0"
"smart-config": "0.8.0",
"twilio": "3.0.0"
},
"devDependencies": {
"babel-eslint": "6.0.2",
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/sms/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const smsStrategy = require('./sms-strategy');

module.exports = smsStrategy;
24 changes: 24 additions & 0 deletions src/helpers/sms/sms-strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const Twilio = require('twilio');
const httpError = require('http-errors');
const smsConfig = require('smart-config').get('sms');

module.exports = (config = smsConfig) => {
return { send: sendSms(config) };
};

function sendSms(config) {
const client = new Twilio(config.accountSid, config.authToken);
return async (message, recipientNumber) => {
try {
await client.messages.create({
body: message,
to: recipientNumber,
from: config.fromNumber
});
return { status: 'OK' };
} catch (err) {
throw httpError(err.status, err.message);
}
};
}

3 changes: 3 additions & 0 deletions src/services/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const emailNotifyAddress = require('./routes/email-notify-address');
const emailNotifyRecipient = require('./routes/email-notify-recipient');
const emailNotifyRecipientBatch = require('./routes/email-notify-recipient-batch');
const batchNotification = require('./routes/batch-notification');
const smsNotifyRecipient = require('./routes/sms-notify-recipient');

const validate = require('../../hooks/validate');
const batchDataValidation = require('./validations/batch-notify-data');
Expand All @@ -30,6 +31,8 @@ module.exports = function () {

app.service('/provider/email/recipient', { create: emailNotifyRecipient });

app.service('/provider/sms/recipient', { create: smsNotifyRecipient });

app
.service('/notification/batch', { create: batchNotification })
.before({ create: validate({ validation: batchDataValidation }) });
Expand Down
5 changes: 5 additions & 0 deletions src/services/provider/routes/sms-notify-recipient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const smsClient = require('../../../helpers/sms');

module.exports = ({ body, recipientNumber }) => {
return smsClient().send(body, recipientNumber);
};
13 changes: 13 additions & 0 deletions test/contract/sms-notify-recipient.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require('../test-env');

const message = {
body: 'testing message :). vanya.',
recipientNumber: '+13346498383'
};

describe('Send SMS via TWILIO', () => {
it('should receive status code 201', () => request
.post('/provider/sms/recipient')
.send(message)
.expect(201));
});

0 comments on commit 33f5785

Please sign in to comment.