Created
August 27, 2016 08:27
-
-
Save jnv/27666983e88fb271e1528b5e538b2f2a to your computer and use it in GitHub Desktop.
SMS forwarder for broken Arduino Uno WiFi Ciao library
This file contains hidden or 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
'use strict' | |
const http = require('http') | |
const server = http.createServer().listen(process.env.PORT || 3000) | |
const url = require('url') | |
const TWILIO_SID = '' | |
const TWILIO_TOKEN = '' | |
const TEL_FROM = '' | |
const TEL_TO = '' | |
const client = require('twilio')(TWILIO_SID, TWILIO_TOKEN); | |
function makeMessage(humidity, temp) { | |
let msg | |
if (humidity === '1') { | |
msg = "Nezehli si vlasy!" | |
} else { | |
msg = "Vyzehli si vlasy jestli chces." | |
} | |
msg += ` Teplota je ${temp} C` | |
return msg | |
} | |
function sendSms(message, callback) { | |
client.sendMessage({ | |
to: TEL_TO, | |
from: TEL_FROM, | |
body: message | |
}, callback) | |
} | |
server.on('request', (req, res) => { | |
console.log(req.method, req.url) | |
const queryData = url.parse(req.url, true).query | |
if (!queryData || !queryData.h || !queryData.t) { | |
res.writeHead(400, {"Content-Type": "text/plain"}) | |
res.write('Invalid query parameters') | |
console.error(queryData) | |
} else { | |
res.writeHead(200, {"Content-Type": "text/plain"}) | |
const msg = makeMessage(queryData.h, queryData.t) | |
sendSms(msg, (err, data) => { | |
if (err) { | |
console.error('Error', err) | |
} else { | |
console.log('SMS response', data) | |
} | |
}) | |
} | |
res.end() | |
}) |
This file contains hidden or 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
{ | |
"name": "@jnv/fwd", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"twilio": "^2.9.2" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"engines": { | |
"node": "4.x" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment