-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.lua
149 lines (122 loc) · 4.4 KB
/
send.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
require "luci.sys"
require "luci.jsonc"
require "nixio"
local config = require "root.sms.config"
local sclient = require("ssl.https")
nixio.openlog("sms-sender")
nixio.syslog("info", "Child ready")
local callerId = arg[1]
local userId = arg[2]
local text = arg[3]
local callback = arg[4]
local channelId = arg[5]
local channelText = ""
local footerText = " - makerspacelt.slack.com"
local bodyText = " wants to talk with you"
nixio.syslog("info", "callerId: " .. callerId)
nixio.syslog("info", "toUserId: " .. userId)
nixio.syslog("info", "callback: " .. callback)
nixio.syslog("info", "channelId: " .. channelId)
function exit()
nixio.closelog()
os.exit()
end
function sendSms(phone, text)
-- return luci.sys.call("echo -n '" .. text .. "' | gnokii --sendsms " .. phone)
return luci.sys.call("/root/sms/send.sh '" .. phone .. "' '" .. text .."'")
end
function respond(url, msg)
local request_body = luci.jsonc.stringify({text = msg})
nixio.syslog("info", "Responding with body: " .. request_body)
local res, code = sclient.request{
url = url,
method = "POST",
headers = {
["Content-Type"] = "application/json";
["Content-Length"] = #request_body;
},
source = ltn12.source.string(request_body),
}
nixio.syslog("info", "Response sent - Code: " .. tostring(code) .. " Res: " .. tostring(res))
end
function getUser(user)
local response_body = {}
local request_body = "user=" .. user
local res, code, response_headers = sclient.request{
url = "https://slack.com/api/users.info",
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
["Authorization"] = "Bearer " .. config.slack.oauth;
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
nixio.syslog("info", "User " .. user .. " requested from slack, status: " .. code)
if code == 200 and type(response_body) == "table" then
return luci.jsonc.parse(table.concat(response_body))
else
return nil
end
end
function getChannel(channel)
local response_body = {}
local request_body = "channel=" .. channel
local res, code, response_headers = sclient.request{
url = "https://slack.com/api/conversations.info",
method = "POST",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded";
["Content-Length"] = #request_body;
["Authorization"] = "Bearer " .. config.slack.oauth;
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body),
}
nixio.syslog("info", "Channel " .. channel .. " requested from slack, status: " .. code)
if code == 200 and type(response_body) == "table" then
return luci.jsonc.parse(table.concat(response_body))
else
return nil
end
end
local callerObj = getUser(callerId)
local userObj = getUser(userId)
local channelObj = getChannel(channelId)
if not callerObj.ok then
nixio.syslog("err", "Invalid caller object")
respond(callback, "ERROR!: Who are you?")
exit()
end
nixio.syslog("info", "Caller: " .. callerObj.user.profile.display_name_normalized)
if not userObj.ok or userObj.user.profile.phone == nil or userObj.user.profile.phone == '' then
nixio.syslog("err", "Invalid user/no number trying to inform user")
respond(callback, "ERROR!: User has no phone/invalid user")
exit()
end
nixio.syslog("info", "User: " .. userObj.user.profile.display_name_normalized)
if channelObj.ok then
if channelObj.channel.is_im then
channelText = " in private!"
else
channelText = " in #" .. channelObj.channel.name_normalized
end
end
phone = tostring(userObj.user.profile.phone):gsub("[^0-9+]", "")
text = "@" .. callerObj.user.profile.display_name_normalized .. bodyText .. channelText .. footerText
nixio.syslog("info", "Sending sms to: " .. phone .. " text: " .. text)
local status = sendSms(phone, text)
if status ~= 0 then
nixio.syslog("err", "SMS sendig failed informing user...")
respond(callback, "ERROR: Someone didn't pay for phone, sms sending failed (" .. tostring(status) .. ")")
else
nixio.syslog("info", "SMS sendig success informing user and adding throttle info...")
local localTime = os.time(os.date("!*t"))
local throttleFile = "/tmp/" .. callerId .. userId
local throttleHandle = io.open(throttleFile, "w+")
throttleHandle:write(localTime)
io.close(throttleHandle)
respond(callback, "SMS delivered")
end
exit()