Last active
December 20, 2015 06:59
-
-
Save jnankin/6089984 to your computer and use it in GitHub Desktop.
A simple webscript.io script in lua that you can curl to alert you via text, sms, or phone when something completes. Eample curls:
curl http://jnankin.webscript.io/alertme?target=YOUR_EMAIL&body=YOUR_BODY&subject=YOUR_OPTIONAL_SUBJECT
curl http://jnankin.webscript.io/alertme?target=sms:YOUR_PHONE&body=YOUR_BODY
curl http://jnankin.webscript.io/a…
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
-- CONFIGURATION | |
-- mailgun | |
MAILGUN_APIKEY='' | |
MAILGUN_SUBDOMAIN='' | |
-- twilio | |
TWILIO_ACCOUNTSID = '' | |
TWILIO_AUTHTOKEN = '' | |
TWILIO_FROM_NUMBER = '' | |
WEBSCRIPTS_SUBDOMAIN = 'http://jnankin.webscript.io' | |
-- THE CODE | |
-- see if this is a twilio call request | |
if request.query.makeCall then | |
return 200, string.format("<Response><Say>%s</Say><Hangup/></Response>", request.query.body) | |
end | |
-- get the target | |
target = request.query.target | |
if not target then | |
return 403, "No target specfied. Must be email address, sms:phoneNumber, or tel:phoneNumber" | |
end | |
if not request.query.body then | |
return 403, "You must specify a body to send" | |
end | |
body = request.query.body | |
if not request.query.subject then | |
subject = body | |
else | |
subject = request.query.subject | |
end | |
function string.starts(String,Start) | |
return string.sub(String,1,string.len(Start))==Start | |
end | |
if string.starts(target, "sms:") then | |
http.request { | |
url = string.format("https://api.twilio.com/2010-04-01/Accounts/%s/SMS/Messages.json", TWILIO_ACCOUNTSID), | |
method = "POST", | |
auth={TWILIO_ACCOUNTSID, TWILIO_AUTHTOKEN}, | |
data = { | |
Body=body, | |
To=string.sub(target,5), | |
From=TWILIO_FROM_NUMBER, | |
} | |
} | |
return 200, "texting..." | |
elseif string.starts(target, "tel:") then | |
http.request { | |
url = string.format("https://api.twilio.com/2010-04-01/Accounts/%s/Calls.json", TWILIO_ACCOUNTSID), | |
method = "POST", | |
auth={TWILIO_ACCOUNTSID, TWILIO_AUTHTOKEN}, | |
data = { | |
Url=string.format("%s%s?makeCall=1&body=%s", WEBSCRIPTS_SUBDOMAIN, request.path, body), | |
To=string.sub(target,5), | |
From=TWILIO_FROM_NUMBER, | |
} | |
} | |
return 200, "calling..." | |
else | |
--it must be an email address | |
http.request { | |
url = string.format("https://api.mailgun.net/v2/%s/messages", MAILGUN_SUBDOMAIN), | |
method = "POST", | |
auth={'api', MAILGUN_APIKEY}, | |
data = { | |
from = target, | |
to = target, | |
subject = subject, | |
text = body | |
} | |
} | |
return 200, "emailed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks great! Let me point you at a couple ways you can simplify the code:
alert.sms(text)
andalert.email(text)
can take care of the texting and emailing to the script owner. See https://www.webscript.io/documentation#alerts. (This only works for paid accounts, though.)require 'twilio'
makes it easier to use Twilio from Webscript. See, e.g.,twilio.call
in https://www.webscript.io/examples/flatterist.