changeset 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 a1d883aa64f6
children 4c98f4340bf1
files mod_voipms/mod_voipms.lua
diffstat 1 files changed, 41 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/mod_voipms/mod_voipms.lua	Sun Oct 12 16:13:42 2025 -0400
+++ b/mod_voipms/mod_voipms.lua	Sun Oct 12 17:47:24 2025 -0400
@@ -34,6 +34,31 @@
     return (event.request.url.query or ""):match("key=([^&]+)")
 end
 
+local function send_message(query, stanza)
+    local query_str = http.formencode(query)
+
+    http.request(rest_endpoint .. "?" .. query_str, {
+        method = "GET";
+    }, function(response_body, code)
+        if code == 200 then
+            local resp, err = json.decode(response_body)
+            if not resp or resp.status ~= "success" then
+                local err_msg = string.format("Failed to send %s: %s", query.method, err or (resp and resp.status) or "unknown")
+                module:log("error", "%s", err_msg)
+                local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
+                module:send(err_reply)
+            else
+                module:log("debug", "Sent %s from %s to %s", query.method, query.did, query.dst)
+            end
+        else
+            local err_msg = string.format("HTTP error sending %s: code %s", method, tostring(code))
+            module:log("error", "%s", err_msg)
+            local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
+            module:send(err_reply)
+        end
+    end)
+end
+
 module:provides("http", {
     route = {
         ["POST"] = function(event)
@@ -96,7 +121,7 @@
                 }):tag("body"):text(message_text):up()
 
                 module:send(text_message)
-                module:log("info", "Delivered text SMS from %s to %s", normalized_from, target_jid)
+                module:log("debug", "Delivered text SMS from %s to %s", normalized_from, target_jid)
             end
 
             if payload.media and #payload.media > 0 then
@@ -113,7 +138,7 @@
                         :up()
 
                         module:send(media_message)
-                        module:log("info", "Delivered media URL from %s to %s: %s", normalized_from, target_jid, media_item.url)
+                        module:log("debug", "Delivered media URL from %s to %s: %s", normalized_from, target_jid, media_item.url)
                     end
                 end
             end
@@ -168,30 +193,22 @@
         for i, url in ipairs(media_urls) do
             query["media_url[" .. (i - 1) .. "]"] = url
         end
-    end
-
-    local query_str = http.formencode(query)
 
-    http.request(rest_endpoint .. "?" .. query_str, {
-        method = "GET";
-    }, function(response_body, code)
-        if code == 200 then
-            local resp, err = json.decode(response_body)
-            if not resp or resp.status ~= "success" then
-                local err_msg = string.format("Failed to send %s: %s", method, err or (resp and resp.status) or "unknown")
-                module:log("error", "%s", err_msg)
-                local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
-                module:send(err_reply)
-            else
-                module:log("info", "Sent %s from %s to %s", method, from_number, dst_number)
-            end
-        else
-            local err_msg = string.format("HTTP error sending %s: code %s", method, tostring(code))
-            module:log("error", "%s", err_msg)
-            local err_reply = st.error_reply(stanza, "cancel", "remote-server-error", err_msg)
-            module:send(err_reply)
+	send_message(query, stanza)
+    else
+        for i = 1, #body, 160 do
+            local chunked_query = {
+                api_username = query.api_username,
+                api_password = query.api_password,
+                method = method,
+                did = from_number,
+                dst = dst_number,
+                message = body:sub(i, i + 159)
+            }
+
+            send_message(chunked_query, stanza)
         end
-    end)
+    end
 
     return true
 end)