From 9acdecc0d570c08170b7814556f57979d2a981ae Mon Sep 17 00:00:00 2001 From: vspedr Date: Fri, 24 Feb 2017 13:00:46 -0300 Subject: [PATCH] initial commit --- .eslintrc | 33 ++++++++++++++++++ .gitignore | 60 ++++++++++++++++++++++++++++++++ README.md | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 44 +++++++++++++++++++++++ package.json | 26 ++++++++++++++ 5 files changed, 261 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..d38a37f --- /dev/null +++ b/.eslintrc @@ -0,0 +1,33 @@ +{ + "extends": "airbnb-base", + "parserOptions": { + "sourceType": "module", + }, + "rules": { + "strict": 0, + "no-param-reassign": [ + "error", + { "props": false } + ], + "func-names": 0, + "comma-dangle": 0, + "prefer-template": 0, + "camelcase": 0, + "no-underscore-dangle": 0, + "consistent-return": 0, + "no-bitwise": ["error", { "allow": ["~"] }] + }, + "globals": { + "describe": true, + "it": true, + "after": true, + "afterEach": true, + "before": true, + "beforeEach": true, + "should": true + }, + "env": { + "node": true, + "mocha": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d74bce --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + +output.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9a39ab --- /dev/null +++ b/README.md @@ -0,0 +1,98 @@ +# movile-messaging + +Node.js wrapper for [Movile's SMS Messaging API](http://doc-messaging.movile.com/sms-v1.html). +You will need your own `UserName` and `AuthenticationToken` to make API calls. +Note that most optional parameters are missing in this module, I'm working on it. PR's are welcome as well 😉 + +## Usage example: +``` +var express = require('express'); +var router = express.Router(); +var Movile = require('movile-messaging'); + +/* GET home page. */ +router.get('/', function(req, res, next) { + let sms = new Movile('YOUR_USER_NAME', 'YOUR_AUTH_TOKEN'); + console.log(sms.getStatus('9cb87d36-79af-11e5-89f3-1b0591cdf807')); + res.render('index', { title: 'Movile Messaging Example' }); +}); + +module.exports = router; +``` + +## Methods: +### send(destination, messageText) +Send SMS message to a single endpoint. +`destination`: Phone number with country code and area code. Example: `'5519998765432'` +`messageText`: The message string to be sent. If it's too long, it will be split into multiple messages. + +Example: +``` +sms.send('5519998765432', 'Your text here'); +``` + +Expected return: +``` +{ + "id":"9cb87d36-79af-11e5-89f3-1b0591cdf807" +} +``` + + +### sendBulk(numbers, messageText) +Send the same SMS message to many endpoints at once. +`numbers`: Array of phone numbers, just like `destination` in the `send` method. +`messageText`: The message string to be sent. If it's too long, it will be split into multiple messages. + +Example: +``` +sms.sendBulk(['5519988887777', '5535989890000'], 'Your text here'); +``` + +Expected return: +``` +{ + "id":"317b925a-79b0-11e5-82d3-9fb06ba220b3", + "messages":[ + { + "id":"715773da-79b0-11e5-afc8-dfdd0dedf87a" + }, + { + "id":"717fb4bc-79b0-11e5-819e-57198aac792e" + } + ] +} +``` + + +### getStatus(id) +Check the delivery status of a single message. +`id`: ID of a message, obtained when it is sent. + +Example: +``` +sms.getStatus('8f5af680-973e-11e4-ad43-4ee58e9a13a6'); +``` +Expected return: +``` +{ + "id":"8f5af680-973e-11e4-ad43-4ee58e9a13a6", + "carrierId":5, + "carrierName":"TIM", + "destination":"5519900001111", + "sentStatusCode":2, + "sentStatus":"SENT_SUCCESS", + "sentStatusAt":1420732929252, + "sentStatusDate":"2015-01-08T16:02:09Z", + "deliveredStatusCode":4, + "deliveredStatus":"DELIVERED_SUCCESS", + "deliveredAt":1420732954000, + "deliveredDate":"2015-01-08T16:02:34Z", + "campaignId":1234 +} +``` + +* Note that phone numbers from `OI` carrier will not return `DELIVERED_SUCCESS` status even if the SMS was successfully received. +* Delivery status data is only retained in Movile's backend for a few days, so you may want to store this data somewhere else. + +Special thanks to [@mCodex](https://github.com/mCodex/) diff --git a/index.js b/index.js new file mode 100644 index 0000000..c1936ea --- /dev/null +++ b/index.js @@ -0,0 +1,44 @@ +'use strict'; + +const axios = require('axios'); + +module.exports = class MovileMessaging { + constructor(UserName = null, AuthenticationToken = null) { + this.instance = axios.create({ + baseURL: 'https://api-messaging.movile.com/v1', + timeout: 5000, + headers: { + UserName, + AuthenticationToken, + 'Content-Type': 'application/json' + } + }); + } + + send(destination = null, messageText = '') { + this.instance.post('/send-sms', { + destination, + messageText + }) + .then(response => response.data) + .catch(error => console.error(error)); + } + + sendBulk(numbers = [], messageText = '') { + const messages = numbers.map(number => ({ destination: number })); + this.instance.post('/send-bulk-sms', { + messages, + defaultValues: { + messageText + } + }) + .then(response => response.data) + .catch(error => console.error(error)); + } + + getStatus(id = '') { + this.instance.get('/sms-status?id=' + id) + .then(response => response.data) + .catch(error => console.error(error)); + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..0d836ec --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "movile-messaging", + "version": "0.1.0", + "description": "Simple node.js wrapper for Movile's Messaging API", + "repository": "vspedr/movile-messaging", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "movile", + "messaging", + "send", + "sms" + ], + "author": "vspedr", + "license": "ISC", + "dependencies": { + "axios": "^0.15.3" + }, + "devDependencies": { + "eslint": "^3.16.0", + "eslint-config-airbnb-base": "^11.1.0", + "eslint-plugin-import": "^2.2.0" + } +}