Last active
April 22, 2018 00:24
-
-
Save zomglings/304a372d4873ac705ea287ac2ce2097c to your computer and use it in GitHub Desktop.
Alert using SMS on file creation
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 path = require('path'); | |
var {poll} = require('@nkashy1/pollster'); | |
var {spawnPredicate} = require('@nkashy1/pollster/predicates'); | |
var twilio = require('twilio'); | |
// START fs config | |
var rawFilepath = process.env.WATCH_FILE; | |
if (!rawFilepath) { | |
throw new Error('WATCH_FILE (undefined) should be the path that you want to poll for file existence'); | |
} | |
var filepath = path.normalize(rawFilepath); | |
// END fs config | |
// START twilio config | |
var twilioAccountSid = process.env.TWILIO_ACCOUNT_SID; | |
var twilioAuthToken = process.env.TWILIO_AUTH_TOKEN; | |
var twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER; | |
var notificationPhoneNumber = process.env.NOTIFICATION_PHONE_NUMBER; | |
if (!twilioAccountSid || !twilioAuthToken || !twilioPhoneNumber || !notificationPhoneNumber) { | |
let message = ( | |
'The following environment variables should be defined:\n' + | |
`TWILIO_ACCOUNT_SID: ${!!twilioAccountSid}\n` + | |
`TWILIO_AUTH_TOKEN: ${!!twilioAuthToken}\n` + | |
`TWILIO_PHONE_NUMBER: ${!!twilioPhoneNumber}` + | |
`NOTIFICATION_PHONE_NUMBER: ${notificationPhoneNumber}` | |
); | |
throw new Error(message); | |
} | |
var twilioClient = new twilio(twilioAccountSid, twilioAuthToken); | |
// END twilio config | |
function smsSignaller(twilioClient, twilioPhoneNumber, notificationPhoneNumber, message) { | |
function sendSignal(err) { | |
if (!!err) { | |
console.error(err); | |
process.exit(1); | |
} | |
return twilioClient.messages.create({ | |
body: message, | |
to: notificationPhoneNumber, | |
from: twilioPhoneNumber | |
}, function(err, message) { | |
if (!!err) { | |
console.error(err); | |
process.exit(1); | |
} | |
console.log( | |
`Message sent: ${message.sid}`); | |
process.exit(0); | |
}); | |
} | |
return sendSignal; | |
} | |
console.log(`Watching for file at ${filepath}...`); | |
poll( | |
spawnPredicate( | |
'cat', | |
[filepath], | |
{}, | |
100), | |
1000, | |
Infinity, | |
smsSignaller( | |
twilioClient, | |
twilioPhoneNumber, | |
notificationPhoneNumber, | |
`File created: ${filepath}`) | |
); |
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
{ | |
"name": "@nkashy1/file-sms-signaller", | |
"version": "0.0.1", | |
"description": "Send an SMS on file creation", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "nkashy1", | |
"license": "Apache-2.0", | |
"dependencies": { | |
"@nkashy1/pollster": "0.0.2", | |
"twilio": "^3.15.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment