Last active
January 5, 2018 13:33
-
-
Save roboflank/bac122c54f73f3b648ff to your computer and use it in GitHub Desktop.
ClickSend Arduino
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
var SerialPort = require("serialport").SerialPort, | |
express = require('express'), | |
bodyParser = require('body-parser'); | |
// initialize a sample request function | |
//This request will be posted once the server is started | |
var request = require('request'); | |
request({ | |
method: 'POST', | |
url: 'https://rest.clicksend.com/v3/sms/send', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Basic YXBpLXVzZXJuYW1lOmFwaS1wYXNzd29yZA==' | |
}, | |
body: "{ \"messages\": [ { \"source\": \"php\", \"from\": \"sendmobile\", \"body\": \"Jelly liquorice marshmallow candy carrot cake 4Eyffjs1vL.\", \"to\": \"+61411111111\", \"schedule\": 1436874701, \"custom_string\": \"this is a test\" }, { \"source\": \"php\", \"from\": \"sendlist\", \"body\": \"Chocolate bar icing icing oat cake carrot cake jelly cotton MWEvciEPIr.\", \"list_id\": 428, \"schedule\": 1436876011, \"custom_string\": \"this is a test\" } ]}" | |
}, function (error, response, body) { | |
console.log('Status:', response.statusCode); | |
console.log('Headers:', JSON.stringify(response.headers)); | |
console.log('Response:', body); | |
}); | |
//add any package provided by the service provider | |
//initialize serialport | |
var serialPort = new SerialPort("/dev/tty.usbmodem1421", { | |
baudrate: 9600 | |
}); | |
//init express | |
var app = express(); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.post('/sms', function(req, res){ | |
serialPort.write(req.body.Body, function(err, results) { | |
if (err) { | |
console.log('err ' + err); | |
} | |
console.log('results ' + results); | |
}); | |
var resp = new sms.Response(); | |
resp.message(function() { | |
this.body('Sent sms successfully'); | |
}); | |
res.type('text/xml'); | |
res.send(resp.toString()); | |
}); | |
//route to take care of the sms request from service provider | |
app.post('/sms', function(req, res){ | |
serialPort.write(req.body.Body, function(err, results) { | |
if (err) { | |
console.log('err ' + err); | |
} | |
console.log('results ' + results); | |
}); | |
var resp = new sms.Response(); | |
resp.message(function() { | |
this.body('SMS received succesfully!'); | |
}); | |
res.type('text/xml'); | |
res.send(resp.toString()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Send and receive sms via Rest via clicksend.