forked from arcturial/clickatell-node
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from clickatell/platform-version
Platform version
- Loading branch information
Showing
21 changed files
with
236 additions
and
1,297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules/ | ||
stage.js | ||
stage.js | ||
.DS_Store | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2017 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,114 @@ | ||
Clickatell NodeJS Library | ||
========================================= | ||
# A simple NODEJS REST & HTTP interaction with Clickatell platform API | ||
|
||
You can see our other libraries and more documentation at the [Clickatell APIs and Libraries Project](http://clickatell.github.io/). | ||
Inside the test.js file is an example implementation of the REST and HTTP API. | ||
|
||
------------------------------------ | ||
|
||
|
||
Master: [![Build Status](https://secure.travis-ci.org/arcturial/clickatell-node.png?branch=master)](http://travis-ci.org/arcturial/clickatell) | ||
|
||
This library allows easy access to connecting the [Clickatell's](http://www.clickatell.com) different messenging API's. | ||
Simply require the clickatell-platform package and use one of the methods to send. | ||
Add the message you want to send, and add the cell number you're sending to, and the API key. | ||
|
||
``` | ||
var clickatell = require("clickatell-platform"); | ||
1. Installation | ||
------------------ | ||
//clickatell.sendMessageRest("Hello testing message", ["27XXXXX-NUMBER"], "APIKEY-HERE"); | ||
This library is managed by the **Node Package Manager** | ||
clickatell.sendMessageHttp("Hello testing message", ["27XXXXX-NUMBER"], "APIKEY-HERE"); | ||
`npm install clickatell-node` | ||
2. Usage | ||
------------------ | ||
``` | ||
|
||
All calls are asynchronous and the parameters follows the nodeJS convention of specifying any errors as the first parameter and the | ||
response as the second. | ||
### Run the code | ||
|
||
```javascript | ||
Simply create a file called test.js and add the code above. | ||
Then trigger the sending by running "node test.js" in your terminal. | ||
|
||
var clickatell = require('clickatell-node').http(user, pass, api_id); | ||
// var clickatell = require('clickatell-node').rest(token); | ||
Remember to add the number you are sending to and your API Key to be able to send successfully. | ||
|
||
clickatell.sendMessage(["00000000000"], "My Message", {}, function (err, messages) { | ||
``` | ||
node test.js | ||
``` | ||
|
||
for (var key in messages) { | ||
var message = messages[key]; | ||
### Handling API callbacks | ||
|
||
console.log(message); | ||
Create a file called server.js and paste the code below into it and save it. | ||
|
||
// Message response format: | ||
// message.id (false if error) | ||
// message.destination | ||
// message.error (false if no error) | ||
// message.code (false if no error) | ||
} | ||
It has a express post method pointing to yourdomain.com/sms which you will use on your platform api to send the callback | ||
posts to, to be able to read callback infromation. | ||
|
||
}); | ||
Simply run the code by typing "node server.js" and it will start to run on port 80, make sure it works by just going to yourdomain.com | ||
|
||
``` | ||
node server.js" | ||
``` | ||
|
||
``` | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
3. Supported API calls | ||
------------------ | ||
The available calls should be defined as the following. Whenever you write a new adapter (API type) you should also try to stick | ||
to this interface. | ||
const app = express() | ||
```javascript | ||
sendMessage(to, message, extra, callback); | ||
const http = require('http') | ||
const port = 80 | ||
getBalance(callback); | ||
const server = http.createServer(app) | ||
stopMessage(apiMsgId, callback); | ||
queryMessage(apiMsgId, callback); | ||
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | ||
extended: true | ||
})); | ||
app.use( bodyParser.json() ); // to support JSON-encoded bodies | ||
routeCoverage(msisdn, callback); | ||
app.use(express.json()); // to support JSON-encoded bodies | ||
app.use(express.urlencoded()); // to support URL-encoded bodies | ||
getMessageCharge(apiMsgId, callback); | ||
server.listen(port, (err) => { | ||
if (err) { | ||
return console.log('something bad happened', err) | ||
} | ||
``` | ||
console.log(`server is listening on ${port}`); | ||
The callback uses the standard way of handling response and will be invoked with the following parameters: | ||
```javascript | ||
app.get('/', function (req, res) { | ||
res.send('It's working') | ||
}) | ||
sendMessage(["0000000000"], "My Message", {}, function (err, messages) { | ||
app.post('/sms', function (req, res) { | ||
const body = req.body | ||
console.log(body); | ||
}); | ||
``` | ||
res.set('Content-Type', 'text/plain') | ||
res.send(`You sent: ${body}`) | ||
}) | ||
4. SendMessage parameters that are not supported | ||
--------------- | ||
The `sendMessage` calls supports a third parameter called `extra`. This parameter can be used to specify any values in the [Clickatell documentation](http://www.clickatell.com) that the library does not support as part of the public interface. | ||
}) | ||
``` | ||
|
||
5. Testing | ||
--------------- | ||
Below is data that you will get back on the callback, once you send a sms. | ||
|
||
DELIVERED_TO_GATEWAY : | ||
* integrationName | ||
* messageId | ||
* requestId | ||
* clientMessageId | ||
* to | ||
* from | ||
* statusCode | ||
* status | ||
* statusDescription | ||
* timestamp | ||
|
||
RECEIVED_BY_RECIPIENT : | ||
* integrationName | ||
* messageId | ||
* requestId | ||
* clientMessageId | ||
* to | ||
* from | ||
* statusCode | ||
* status | ||
* statusDescription | ||
* timestamp | ||
|
||
To run the library test suite just execute `npm test` from the library root. Please make sure all tests are passing before pushing back any changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const Request = require('request'); | ||
|
||
// Constructor | ||
function Http(content, to, apiKey) { | ||
// always initialize all instance properties | ||
this.content = content; // message you are sending | ||
this.to = to; // phone number | ||
this.apiKey = apiKey; | ||
|
||
} | ||
// class methods | ||
Http.prototype.sendMessage = function(content, to, apiKey) { | ||
|
||
// replace spaces in message with plus sign to work correctly with http call | ||
var contentReplaced = encodeURIComponent(content.split(' ').join('+')); | ||
|
||
var options = { | ||
method: 'GET', | ||
url:'https://platform.clickatell.com/messages/http/send?apiKey='+ apiKey +'&to='+ to.join(",") +'&content='+ contentReplaced | ||
} | ||
|
||
Request(options, function (error, response, body) { | ||
|
||
console.log('error:', error); // Print the error if one occurred | ||
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | ||
console.log('body:', body); // Print the HTML for the Google homepage. | ||
}); | ||
|
||
}; | ||
|
||
// export the class | ||
module.exports = Http; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const Request = require('request'); | ||
|
||
// Constructor | ||
function Rest(content, to, apiKey) { | ||
// always initialize all instance properties | ||
this.content = content; // message you are sending | ||
this.to = to; // phone number | ||
this.apiKey = apiKey; | ||
|
||
} | ||
// class methods | ||
Rest.prototype.sendMessage = function(content, to, apiKey) { | ||
// set options array | ||
content = JSON.stringify(content); | ||
to = JSON.stringify(to); | ||
var options = { | ||
method: 'POST', | ||
url:'https://platform.clickatell.com/messages', | ||
headers: | ||
{ | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': apiKey | ||
}, | ||
body: '{"content": ' + content + ', "to": ' + to +'}' | ||
} | ||
|
||
// send the request | ||
Request(options, function (error, response, body) { | ||
console.log('error:', error); // Print the error if one occurred | ||
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | ||
console.log('body:', body); // Print the HTML for the Google homepage. | ||
}); | ||
}; | ||
// export the class | ||
module.exports = Rest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
|
||
|
||
const app = express() | ||
|
||
|
||
const http = require('http') | ||
const port = 80 | ||
|
||
const server = http.createServer(app) | ||
|
||
|
||
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | ||
extended: true | ||
})); | ||
app.use( bodyParser.json() ); // to support JSON-encoded bodies | ||
|
||
app.use(express.json()); // to support JSON-encoded bodies | ||
app.use(express.urlencoded()); // to support URL-encoded bodies | ||
|
||
server.listen(port, (err) => { | ||
if (err) { | ||
return console.log('something bad happened', err) | ||
} | ||
|
||
console.log(`server is listening on ${port}`); | ||
|
||
|
||
app.get('/', function (req, res) { | ||
res.send('This is an example route.') | ||
}) | ||
|
||
app.post('/sms', function (req, res) { | ||
const body = req.body | ||
console.log(body); | ||
|
||
|
||
res.set('Content-Type', 'text/plain') | ||
res.send(`You sent: ${body}`) | ||
}) | ||
|
||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var Http = require("./api/http"); | ||
var Rest = require('./api/rest'); | ||
|
||
module.exports = { | ||
sendMessageHttp: function (content, to, apiKey) { | ||
var http = new Http(); | ||
var sendMessageHttp = http.sendMessage(content, to, apiKey); | ||
return sendMessageHttp; | ||
}, | ||
sendMessageRest: function (content, to, apiKey) { | ||
var rest = new Rest(); | ||
var sendMessageRest = rest.sendMessage(content, to, apiKey) | ||
return sendMessageRest; | ||
} | ||
// more methods coming | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,18 @@ | ||
{ | ||
"name": "clickatell-node", | ||
"version": "0.0.2", | ||
"description": "Library for Clickatell SMS gateway APIs", | ||
"author": "Chris Brand <chris@cainsvault.com> (http://www.cainsvault.com/)", | ||
"main": "./src/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/arcturial/clickatell-node.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/arcturial/clickatell-node/issues" | ||
}, | ||
"keywords": [ | ||
"clickatell", | ||
"api", | ||
"sms" | ||
], | ||
"license": "GNU", | ||
"dependencies": { | ||
"merge": "1.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"nock": "*" | ||
}, | ||
"scripts": { | ||
"test": "node node_modules/mocha/bin/mocha --recursive" | ||
} | ||
} | ||
"name": "clickatell-platform", | ||
"author": "Morne Zeelie <morne.zeelie@clickatell.com>", | ||
"description": "A simple NodeJs REST & HTTP interaction with Clickatell platform API", | ||
"main": "index.js", | ||
"version": "2.0.10", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/clickatell/clickatell-node.git" | ||
}, | ||
"bugs": "https://github.com/clickatell/clickatell-node/issues", | ||
"dependencies": { | ||
"express": "^4.16.1", | ||
"body-parser": "^1.18.2", | ||
"request": "^2.79.0" | ||
} | ||
} |
Oops, something went wrong.