-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pushover_Transmitter.lua
152 lines (121 loc) · 4.56 KB
/
Pushover_Transmitter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
--[[
Pushover - Transmitter
Created by K. Rhodus
Version History:
1.0 - Initial Build
]]--
-----------------------------------------------------------------------------------------------------------------------------------------
-- Script Setup --
----------------------------------------------------------------------------------------------------------------------------------------
json = require("rapidjson")
Controls.priority.Choices = {-2,-1,0,1,2}
Controls.sound.Choices = {"","pushover","bike","bugle","cashregister","classical","cosmic","falling","gamelan","incoming","intermission","magic","mechanical","pianobar","siren","spacealarm","tugboat","alien","climb","persistent","echo","updown","vibrate","none","custom"}
Controls.Status.Value = 0
-----------------------------------------------------------------------------------------------------------------------------------------
-- Register / Validate Device --
----------------------------------------------------------------------------------------------------------------------------------------
Controls.Validate.EventHandler = function()
local message = {}
if Controls.device.String ~= "" then
message["device"]= Controls.device.String
end
if Controls.Token.String ~= "" then
message["token"]= Controls.Token.String
else
Controls.Status.Value = 1
Controls.Status.String = "Missing Application Token"
end
if Controls.UserAPI.String ~= "" then
message["user"]= Controls.UserAPI.String
else
Controls.Status.Value = 1
Controls.Status.String = "Missing User API Key"
end
print(json.encode(message))
if #Controls.Token.String >1 and # Controls.UserAPI.String >1 then
HttpClient.Upload {
Url = "https://api.pushover.net/1/users/validate.json",
Method = "POST",
Data = json.encode(message),
Headers = {
["Content-Type"] = "application/json",
},
EventHandler = ValidateReturn -- The function to call upon response
}
end
end
function ValidateReturn(tbl, code, d, e)
print( string.format("Response Code: %i\t\tErrors: %s\rData: %s",code, e or "None", d))
local response = json.decode(d)
if response.status == 1 then
Controls.ValidUser.Boolean = true
else
Controls.ValidUser.Boolean = false
end
Controls.AvailableDevices.String = table.concat(response.devices, ",")
end
-----------------------------------------------------------------------------------------------------------------------------------------
-- Send Message --
-----------------------------------------------------------------------------------------------------------------------------------------
Controls.Send.EventHandler = function()
local message = {}
if Controls.device.String ~= "" then
message["device"]= Controls.device.String
end
if Controls.priority.String ~= "" then
message["priority"]= Controls.priority.String
end
if Controls.sound.String ~= "" then
if Controls.sound.String == "custom" then
message["sound"]= Controls.customSoundName.String
else
message["sound"]= Controls.sound.String
end
end
if Controls.title.String ~= "" then
message["title"]= Controls.title.String
end
if Controls.ttl.String ~= "" then
message["ttl"]= Controls.ttl.String
end
if Controls.url.String ~= "" then
message["url"]= Controls.url.String
end
if Controls.urlName.String ~= "" then
message["url_title"]= Controls.urlName.String
end
if Controls.message.String ~= "" then
message["message"]= Controls.message.String
else
Controls.Status.Value = 1
Controls.Status.String = "Message has no Body"
end
if Controls.Token.String ~= "" then
message["token"]= Controls.Token.String
else
Controls.Status.Value = 1
Controls.Status.String = "Missing Application Token"
end
if Controls.UserAPI.String ~= "" then
message["user"]= Controls.UserAPI.String
else
Controls.Status.Value = 1
Controls.Status.String = "Missing User API Key"
end
print(json.encode(message))
if #Controls.Token.String >1 and # Controls.UserAPI.String >1 then
HttpClient.Upload {
Url = "https://api.pushover.net/1/messages.json",
Method = "POST",
Data = json.encode(message),
Headers = {
["Content-Type"] = "application/json",
},
EventHandler = MessageReturn -- The function to call upon response
}
end
end
function MessageReturn(tbl, code, d, e)
print( string.format("Response Code: %i\t\tErrors: %s\rData: %s",code, e or "None", d))
end