From e67cce70b9335fb6a9997f32b301e0d52fadbc9d Mon Sep 17 00:00:00 2001 From: AxeKam333 Date: Wed, 12 Mar 2025 14:00:08 +0100 Subject: [PATCH] Add Apiv1 http for backwards compatibility --- server/notification-providers/smseagle.js | 199 +++++++++++++--------- src/components/notifications/SMSEagle.vue | 101 +++++++---- 2 files changed, 186 insertions(+), 114 deletions(-) diff --git a/server/notification-providers/smseagle.js b/server/notification-providers/smseagle.js index 91cd2e4bf..1e958ec3c 100644 --- a/server/notification-providers/smseagle.js +++ b/server/notification-providers/smseagle.js @@ -11,90 +11,131 @@ class SMSEagle extends NotificationProvider { const okMsg = "Sent Successfully."; try { - let config = { - headers: { - "access-token": notification.smseagleToken, - "Content-Type": "application/json", + if (notification.smseagleApiType === "smseagle-apiv1") { + let config = { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + } + }; + + let sendMethod; + let recipientType; + + if (notification.smseagleRecipientType === "smseagle-contact") { + recipientType = "contactname"; + sendMethod = "/send_tocontact"; } - }; - - let encoding = (notification.smseagleEncoding) ? "unicode" : "standard"; - let priority = (notification.smseaglePriority) ? notification.smseaglePriority : 0; - - let postData = { - text: msg, - encoding: encoding, - priority: priority - }; - - let to = notification.smseagleRecipientTo; - let contacts = notification.smseagleRecipientContact; - let groups = notification.smseagleRecipientGroup; - - if (contacts) { - contacts = contacts.split(","); - contacts = contacts.map(e => { - return Number(e) - }); - postData["contacts"] = contacts; - } - - if (groups) { - groups = groups.split(","); - groups = groups.map(e => { - return Number(e) - }); - postData["groups"] = groups; - } - - if (to) { - to = to.split(","); - postData["to"] = to; - } - - let endpoint = "/messages/sms"; - - if (notification.smseagleMsgType != "smseagle-sms") { - - let duration; - if (notification.smseagleDuration) - duration = notification.smseagleDuration - else - duration = 10; - - postData["duration"] = duration; - - if (notification.smseagleMsgType == "smseagle-ring") { - endpoint = "/calls/ring"; - } else if (notification.smseagleMsgType == "smseagle-tts") { - endpoint = "/calls/tts"; - } else if (notification.smseagleMsgType == "smseagle-tts-advanced") { - endpoint = "/calls/tts_advanced"; - postData["voice_id"] = notification.smseagleTtsModel; + if (notification.smseagleRecipientType === "smseagle-group") { + recipientType = "groupname"; + sendMethod = "/send_togroup"; } - } - - let resp = await axios.post(notification.smseagleUrl + "/api/v2" + endpoint, postData, config); - - let countAll = resp.data.length; - let countQueued = resp.data.filter(x => x.status == "queued").length; - - if (resp.status !== 200 || countQueued == 0) { - let error = ""; - if (resp.data.length > 0) { - error = `SMSEagle API returned error: ${JSON.stringify(resp.data)}`; - } else { - error = "SMSEagle API returned an unexpected response"; + if (notification.smseagleRecipientType === "smseagle-to") { + recipientType = "to"; + sendMethod = "/send_sms"; } - throw new Error(error); - } - if (countAll !== countQueued) { - let okWithErrorsMsg = "Sent " + countQueued + "/" + countAll + " Messages Successfully."; - return okWithErrorsMsg; - } + const url = new URL(notification.smseagleUrl + "/http_api" + sendMethod); - return okMsg; + url.searchParams.append('access_token', notification.smseagleToken); + url.searchParams.append(recipientType, notification.smseagleRecipient); + url.searchParams.append('message', msg); + url.searchParams.append('unicode', (notification.smseagleEncoding) ? "1" : "0"); + url.searchParams.append('highpriority', (notification.smseaglePriority) ? notification.smseaglePriority : "0"); + + let resp = await axios.get(url.toString(), config); + + if (resp.data.indexOf("OK") === -1) { + let error = `SMSEagle API returned error: ${resp.data}`; + throw new Error(error); + } + + return okMsg; + } else if (notification.smseagleApiType === "smseagle-apiv2") { + let config = { + headers: { + "access-token": notification.smseagleToken, + "Content-Type": "application/json", + } + }; + + let encoding = (notification.smseagleEncoding) ? "unicode" : "standard"; + let priority = (notification.smseaglePriority) ? notification.smseaglePriority : 0; + + let postData = { + text: msg, + encoding: encoding, + priority: priority + }; + + let to = notification.smseagleRecipientTo; + let contacts = notification.smseagleRecipientContact; + let groups = notification.smseagleRecipientGroup; + + if (contacts) { + contacts = contacts.split(","); + contacts = contacts.map(e => { + return Number(e) + }); + postData["contacts"] = contacts; + } + + if (groups) { + groups = groups.split(","); + groups = groups.map(e => { + return Number(e) + }); + postData["groups"] = groups; + } + + if (to) { + to = to.split(","); + postData["to"] = to; + } + + let endpoint = "/messages/sms"; + + if (notification.smseagleMsgType != "smseagle-sms") { + + let duration; + if (notification.smseagleDuration) + duration = notification.smseagleDuration + else + duration = 10; + + postData["duration"] = duration; + + if (notification.smseagleMsgType == "smseagle-ring") { + endpoint = "/calls/ring"; + } else if (notification.smseagleMsgType == "smseagle-tts") { + endpoint = "/calls/tts"; + } else if (notification.smseagleMsgType == "smseagle-tts-advanced") { + endpoint = "/calls/tts_advanced"; + postData["voice_id"] = notification.smseagleTtsModel; + } + } + + let resp = await axios.post(notification.smseagleUrl + "/api/v2" + endpoint, postData, config); + + let countAll = resp.data.length; + let countQueued = resp.data.filter(x => x.status == "queued").length; + + if (resp.status !== 200 || countQueued == 0) { + let error = ""; + if (resp.data.length > 0) { + error = `SMSEagle API returned error: ${JSON.stringify(resp.data)}`; + } else { + error = "SMSEagle API returned an unexpected response"; + } + throw new Error(error); + } + + if (countAll !== countQueued) { + let okWithErrorsMsg = "Sent " + countQueued + "/" + countAll + " Messages Successfully."; + return okWithErrorsMsg; + } + + return okMsg; + } } catch (error) { this.throwGeneralAxiosError(error); } diff --git a/src/components/notifications/SMSEagle.vue b/src/components/notifications/SMSEagle.vue index a4cb6d39e..39559105c 100644 --- a/src/components/notifications/SMSEagle.vue +++ b/src/components/notifications/SMSEagle.vue @@ -8,44 +8,75 @@
- -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - + +
-
- - +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
-
- - +
+
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +