Skip to content

Instantly share code, notes, and snippets.

@jnankin
Last active December 20, 2015 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnankin/6089984 to your computer and use it in GitHub Desktop.
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…
-- 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
@smarx
Copy link

smarx commented Jul 26, 2013

This looks great! Let me point you at a couple ways you can simplify the code:

  1. alert.sms(text) and alert.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.)
  2. require 'twilio' makes it easier to use Twilio from Webscript. See, e.g., twilio.call in https://www.webscript.io/examples/flatterist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment