Well, I hadn’t seen this thread, and I apparently reimplemented the wheel. If anyone’s curious, here’s my version:
[ul][li]Is a pure lua version[/li]
[li]Supports multiline text[/li]
[li]Supports message priority[/li]
[li]Supports rate limiting (for some reason, each trigger causes my scene’s luup code to be run three times).[/li]
[li]Everything’s easily configured via variables at the top.[/li][/ul]
[code]local apikey = “0000000000000000000000000000000000000000”
local application_name = “Front door”
local event_text = “Door opened”
– default description is like:
– 04:59:23 PM, Jan 25, 2013
local description = os.date(“%I:%M:%S %p, %b %d, %Y”)
– Priority is an integer in the range of [-2 … 2].
– -2 is very low priority, 0 is normal, and +2 is emergency
local priority = 0
– limit notifications to only once every this many seconds:
local limit_notifications = 10
local class_name = “front_door”
require ‘socket.http’
require ‘ltn12’
– Convert text into a URL-safe form. See RFC 1738 for unsafe characters.
local function transmorgrify_text(str)
– There’s probably a much better way to do this, but lua seems to be
– poorly documented, as the authors seem to want you to buy the book …
local s = string.gsub(str, “%%”, “%%25”)
s = string.gsub(s, “\n”, “%%0a”)
s = string.gsub(s, “\r”, “”) – go away
s = string.gsub(s, “\t”, “%%09”)
s = string.gsub(s, " ", “%%20”)
s = string.gsub(s, “!”, “%%21”)
s = string.gsub(s, “"”, “%%22”)
s = string.gsub(s, “#”, “%%23”)
s = string.gsub(s, “\$”, “%%24”) – not necessary?
s = string.gsub(s, “&”, “%%26”)
s = string.gsub(s, “'”, “%%27”)
s = string.gsub(s, “[(]”, “%%28”) – not necessary?
s = string.gsub(s, “[)]”, “%%29”) – not necessary?
s = string.gsub(s, “[*]”, “%%2a”) – not necessary?
s = string.gsub(s, “[+]”, “%%2b”) – not necessary?
s = string.gsub(s, “,”, “%%2c”) – not necessary?
s = string.gsub(s, “:”, “%%3a”)
s = string.gsub(s, “;”, “%%3b”)
s = string.gsub(s, “<”, “%%3c”)
s = string.gsub(s, “=”, “%%3d”)
s = string.gsub(s, “>”, “%%3e”)
s = string.gsub(s, “[?]”, “%%3f”)
s = string.gsub(s, “@”, “%%40”)
s = string.gsub(s, “[[]”, “%%5b”)
s = string.gsub(s, “\”, “%%5c”)
s = string.gsub(s, “]”, “%%5d”)
s = string.gsub(s, “[\^]”, “%%5e”)
s = string.gsub(s, “`”, “%%60”)
s = string.gsub(s, “{”, “%%7b”)
s = string.gsub(s, “|”, “%%7c”)
s = string.gsub(s, “}”, “%%7d”)
s = string.gsub(s, “~”, “%%7e”)
return s
end
local function prowl_notify(apikey, application_name, event_text, rate_limit)
if rate_limit then
local current_time = os.time()
if prowl_last_notify_time then
if prowl_last_notify_time[class_name] then
if prowl_last_notify_time[class_name] >
(os.time() - rate_limit) then
return nil
end
end
else
prowl_last_notify_time = {}
end
prowl_last_notify_time[class_name] = current_time
end
local response_body = {}
local request_body = “apikey=” … apikey …
“&application=” … transmorgrify_text(application_name) …
“&event=” … transmorgrify_text(event_text) …
“&description=” … transmorgrify_text(description) …
“&priority=” … tostring(priority)
socket.http.request{
url = “https://api.prowlapp.com/publicapi/add”,
method = “POST”,
headers = {
[“Content-Type”] = “application/x-www-form-urlencoded”,
[“Content-Length”] = string.len(request_body)
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}
return response_body
end
local response_body
response_body = prowl_notify(apikey, application_name, event_text,
limit_notifications)
– if response_body then
– table.foreach(response_body,print)
– end
– response_body = prowl_notify(apikey, application_name, event_text,
– limit_notifications)
– if response_body then
– table.foreach(response_body,print)
– end
return true
[/code]