comparison mod_voipms/mod_voipms.lua @ 6341:fa976338928c

mod_voipms: Chunk messages larger than 160 characters as per the API
author Chaz Kettleson <chaz@pyr3x.com>
date Sun, 12 Oct 2025 17:47:24 -0400
parents 8f2de6652930
children
comparison
equal deleted inserted replaced
6340:a1d883aa64f6 6341:fa976338928c
30 return num 30 return num
31 end 31 end
32 32
33 local function extract_query_key(event) 33 local function extract_query_key(event)
34 return (event.request.url.query or ""):match("key=([^&]+)") 34 return (event.request.url.query or ""):match("key=([^&]+)")
35 end
36
37 local function send_message(query, stanza)
38 local query_str = http.formencode(query)
39
40 http.request(rest_endpoint .. "?" .. query_str, {
41 method = "GET";
42 }, function(response_body, code)
43 if code == 200 then
44 local resp, err = json.decode(response_body)
45 if not resp or resp.status ~= "success" then
46 local err_msg = string.format("Failed to send %s: %s", query.method, err or (resp and resp.status) or "unknown")
47 module:log("error", "%s", err_msg)
48 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
49 module:send(err_reply)
50 else
51 module:log("debug", "Sent %s from %s to %s", query.method, query.did, query.dst)
52 end
53 else
54 local err_msg = string.format("HTTP error sending %s: code %s", method, tostring(code))
55 module:log("error", "%s", err_msg)
56 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
57 module:send(err_reply)
58 end
59 end)
35 end 60 end
36 61
37 module:provides("http", { 62 module:provides("http", {
38 route = { 63 route = {
39 ["POST"] = function(event) 64 ["POST"] = function(event)
94 to = target_jid, 119 to = target_jid,
95 type = "chat" 120 type = "chat"
96 }):tag("body"):text(message_text):up() 121 }):tag("body"):text(message_text):up()
97 122
98 module:send(text_message) 123 module:send(text_message)
99 module:log("info", "Delivered text SMS from %s to %s", normalized_from, target_jid) 124 module:log("debug", "Delivered text SMS from %s to %s", normalized_from, target_jid)
100 end 125 end
101 126
102 if payload.media and #payload.media > 0 then 127 if payload.media and #payload.media > 0 then
103 for _, media_item in ipairs(payload.media) do 128 for _, media_item in ipairs(payload.media) do
104 if media_item.url then 129 if media_item.url then
111 :tag("x", { xmlns = "jabber:x:oob" }) 136 :tag("x", { xmlns = "jabber:x:oob" })
112 :tag("url"):text(media_item.url):up() 137 :tag("url"):text(media_item.url):up()
113 :up() 138 :up()
114 139
115 module:send(media_message) 140 module:send(media_message)
116 module:log("info", "Delivered media URL from %s to %s: %s", normalized_from, target_jid, media_item.url) 141 module:log("debug", "Delivered media URL from %s to %s: %s", normalized_from, target_jid, media_item.url)
117 end 142 end
118 end 143 end
119 end 144 end
120 145
121 return { status_code = 204 } 146 return { status_code = 204 }
166 191
167 if method == "sendMMS" then 192 if method == "sendMMS" then
168 for i, url in ipairs(media_urls) do 193 for i, url in ipairs(media_urls) do
169 query["media_url[" .. (i - 1) .. "]"] = url 194 query["media_url[" .. (i - 1) .. "]"] = url
170 end 195 end
171 end 196
172 197 send_message(query, stanza)
173 local query_str = http.formencode(query) 198 else
174 199 for i = 1, #body, 160 do
175 http.request(rest_endpoint .. "?" .. query_str, { 200 local chunked_query = {
176 method = "GET"; 201 api_username = query.api_username,
177 }, function(response_body, code) 202 api_password = query.api_password,
178 if code == 200 then 203 method = method,
179 local resp, err = json.decode(response_body) 204 did = from_number,
180 if not resp or resp.status ~= "success" then 205 dst = dst_number,
181 local err_msg = string.format("Failed to send %s: %s", method, err or (resp and resp.status) or "unknown") 206 message = body:sub(i, i + 159)
182 module:log("error", "%s", err_msg) 207 }
183 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg) 208
184 module:send(err_reply) 209 send_message(chunked_query, stanza)
185 else 210 end
186 module:log("info", "Sent %s from %s to %s", method, from_number, dst_number) 211 end
187 end
188 else
189 local err_msg = string.format("HTTP error sending %s: code %s", method, tostring(code))
190 module:log("error", "%s", err_msg)
191 local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
192 module:send(err_reply)
193 end
194 end)
195 212
196 return true 213 return true
197 end) 214 end)