Added CallMeBot messaging integration (#4688)
Ylian Saint-Hilaire committed
Oct 28, 2022 at 17:44 UTC
0b1bf704aafe9029d9fe8de3fbe9ce8137dd3b9a
3 files changed
+81
-16
meshmessaging.js
+48
-2
@@ -53,18 +53,28 @@
53
}
54
}
55
}
56
+
57
+// For CallMeBot
58
+// For Signal Messenger: https://www.callmebot.com/blog/free-api-signal-send-messages/
59
+{
60
+ "messaging": {
61
+ "callmebot": true
62
+ }
63
+}
64
+
65
*/
66
67
// Construct a messaging server object
68
module.exports.CreateServer = function (parent) {
69
var obj = {};
70
obj.parent = parent;
62
- obj.providers = 0; // 1 = Telegram, 2 = Signal, 4 = Discord, 8 = XMPP
71
+ obj.providers = 0; // 1 = Telegram, 2 = Signal, 4 = Discord, 8 = XMPP, 16 = CallMeBot
72
obj.telegramClient = null;
73
obj.discordClient = null;
74
obj.discordUrl = null;
75
obj.xmppClient = null;
76
var xmppXml = null;
77
+ obj.callMeBotClient = null;
78
79
// Telegram client setup
80
if (parent.config.messaging.telegram) {
@@ -154,7 +164,7 @@ module.exports.CreateServer = function (parent) {
164
165
// XMPP client setup
166
if (parent.config.messaging.xmpp) {
157
- // Validate Discord configuration values
167
+ // Validate XMPP configuration values
168
var xmppOK = true;
169
if (typeof parent.config.messaging.xmpp.service != 'string') { console.log('Invalid or missing XMPP service.'); xmppOK = false; }
170
@@ -176,6 +186,12 @@ module.exports.CreateServer = function (parent) {
186
}
187
}
188
189
+ // CallMeBot client setup
190
+ if (parent.config.messaging.callmebot) {
191
+ obj.callMeBotClient = true;
192
+ obj.providers += 16; // Enable CallMeBot messaging
193
+ }
194
+
195
// Send a direct message to a specific userid
196
async function discordSendMsg(userId, message) {
197
const user = await obj.discordClient.users.fetch(userId).catch(function () { return null; });
@@ -221,12 +237,42 @@ module.exports.CreateServer = function (parent) {
237
} else if ((to.startsWith('xmpp:')) && (obj.xmppClient != null)) { // XMPP
238
parent.debug('email', 'Sending XMPP message to: ' + to.substring(5) + ': ' + msg);
239
sendXmppMessage(to, msg, func);
240
+ } else if ((to.startsWith('callmebot:')) && (obj.callMeBotClient != null)) { // CallMeBot
241
+ parent.debug('email', 'Sending CallMeBot message to: ' + to.substring(10) + ': ' + msg);
242
+ console.log('Sending CallMeBot message to: ' + to.substring(10) + ': ' + msg);
243
+ var toData = to.substring(10).split('|');
244
+ if ((toData[0] == 'signal') && (toData.length == 3)) {
245
+ var url = 'https://api.callmebot.com/signal/send.php?phone=' + encodeURIComponent(toData[1]) + '&apikey=' + encodeURIComponent(toData[2]) + '&text=' + encodeURIComponent(msg);
246
+ require('https').get(url, function (r) { if (func != null) { func(r.statusCode == 200); } });
247
+ } else if ((toData[0] == 'whatsapp') && (toData.length == 3)) {
248
+ var url = 'https://api.callmebot.com/whatsapp.php?phone=' + encodeURIComponent(toData[1]) + '&apikey=' + encodeURIComponent(toData[2]) + '&text=' + encodeURIComponent(msg);
249
+ require('https').get(url, function (r) { if (func != null) { func(r.statusCode == 200); } });
250
+ } else if ((toData[0] == 'facebook') && (toData.length == 2)) {
251
+ var url = 'https://api.callmebot.com/facebook/send.php?apikey=' + encodeURIComponent(toData[1]) + '&text=' + encodeURIComponent(msg);
252
+ require('https').get(url, function (r) { if (func != null) { func(r.statusCode == 200); } });
253
+ }
254
} else {
255
// No providers found
256
func(false, "No messaging providers found for this message.");
257
}
258
}
259
260
+ // Convert a CallMeBot URL into a handle
261
+ obj.callmebotUrlToHandle = function (xurl) {
262
+ var url = null;
263
+ try { url = require('url').parse(xurl); } catch (ex) { return; }
264
+ if ((url == null) || (url.host != 'api.callmebot.com') || (url.protocol != 'https:') || (url.query == null)) return;
265
+ var urlArgs = {}, urlArgs2 = url.query.split('&');
266
+ for (var i in urlArgs2) { var j = urlArgs2[i].indexOf('='); if (j > 0) { urlArgs[urlArgs2[i].substring(0, j)] = urlArgs2[i].substring(j + 1); } }
267
+ if ((urlArgs['phone'] != null) && (urlArgs['phone'].indexOf('|') >= 0)) return;
268
+ if ((urlArgs['apikey'] != null) && (urlArgs['apikey'].indexOf('|') >= 0)) return;
269
+ // Signal Messenger, Whatapp and Facebook
270
+ if (url.path.startsWith('/signal') && (urlArgs['phone'] != null) && (urlArgs['apikey'] != null)) { return 'callmebot:signal|' + urlArgs['phone'] + '|' + urlArgs['apikey']; }
271
+ if (url.path.startsWith('/whatsapp') && (urlArgs['phone'] != null) && (urlArgs['apikey'] != null)) { return 'callmebot:whatsapp|' + urlArgs['phone'] + '|' + urlArgs['apikey']; }
272
+ if (url.path.startsWith('/facebook') && (urlArgs['apikey'] != null)) { return 'callmebot:facebook|' + urlArgs['apikey']; }
273
+ return null;
274
+ }
275
+
276
// Get the correct SMS template
277
function getTemplate(templateNumber, domain, lang) {
278
parent.debug('email', 'Getting SMS template #' + templateNumber + ', lang: ' + lang);
meshuser.js
+7
-2
@@ -1390,7 +1390,11 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1390
if (command.resetNextLogin === true) { chguser.passchange = -1; }
1391
if ((command.consent != null) && (typeof command.consent == 'number')) { if (command.consent == 0) { delete chguser.consent; } else { chguser.consent = command.consent; } change = 1; }
1392
if ((command.phone != null) && (typeof command.phone == 'string') && ((command.phone == '') || isPhoneNumber(command.phone))) { if (command.phone == '') { delete chguser.phone; } else { chguser.phone = command.phone; } change = 1; }
1393
- if ((command.msghandle != null) && (typeof command.msghandle == 'string')) { if (command.msghandle == '') { delete chguser.msghandle; } else { chguser.msghandle = command.msghandle; } change = 1; }
1393
+ if ((command.msghandle != null) && (typeof command.msghandle == 'string')) {
1394
+ if (command.msghandle.startsWith('callmebot:https://')) { const h = parent.parent.msgserver.callmebotUrlToHandle(command.msghandle.substring(10)); if (h) { command.msghandle = h; } else { command.msghandle = ''; } }
1395
+ if (command.msghandle == '') { delete chguser.msghandle; } else { chguser.msghandle = command.msghandle; }
1396
+ change = 1;
1397
+ }
1398
if ((command.flags != null) && (typeof command.flags == 'number')) {
1399
// Flags: 1 = Account Image, 2 = Session Recording
1400
if ((command.flags == 0) && (chguser.flags != null)) { delete chguser.flags; change = 1; } else { if (command.flags !== chguser.flags) { chguser.flags = command.flags; change = 1; } }
@@ -6689,13 +6693,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6693
6694
if ((user.siteadmin != 0xFFFFFFFF) && ((user.siteadmin & 1024) != 0)) return; // If this account is settings locked, return here.
6695
if (parent.parent.msgserver == null) return;
6692
- if (common.validateString(command.handle, 1, 64) == false) return; // Check handle length
6696
+ if (common.validateString(command.handle, 1, 1024) == false) return; // Check handle length
6697
6698
// Setup the handle for the right messaging service
6699
var handle = null;
6700
if ((command.service == 1) && ((parent.parent.msgserver.providers & 1) != 0)) { handle = 'telegram:@' + command.handle; }
6701
if ((command.service == 4) && ((parent.parent.msgserver.providers & 4) != 0)) { handle = 'discord:' + command.handle; }
6702
if ((command.service == 8) && ((parent.parent.msgserver.providers & 8) != 0)) { handle = 'xmpp:' + command.handle; }
6703
+ if ((command.service == 16) && ((parent.parent.msgserver.providers & 16) != 0)) { handle = parent.parent.msgserver.callmebotUrlToHandle(command.handle); }
6704
if (handle == null) return;
6705
6706
// Send a verification message
views/default.handlebars
+26
-12
@@ -11982,15 +11982,16 @@
11982
x = '<table style=width:100%><tr><td style=width:56px;vertical-align:top><img src="images/messaging40.png" style=padding:8px>';
11983
x += '<td>' + "Enter your messaging service and handle. Once verified, this server can send you login verification and other notifications." + '<br /><br />';
11984
var y = '<select id=d2serviceselect style=width:160px;margin-left:8px onchange=account_manageMessagingValidate()>';
11985
- if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
11986
- if ((serverinfo.userMsgProviders & 2) != 0) { y += '<option value=2>' + "Signal Messenger" + '</option>'; }
11987
- if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
11988
- if ((serverinfo.userMsgProviders & 8) != 0) { y += '<option value=8>' + "XMPP" + '</option>'; }
11985
+ if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
11986
+ if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
11987
+ if ((serverinfo.userMsgProviders & 8) != 0) { y += '<option value=8>' + "XMPP" + '</option>'; }
11988
+ if ((serverinfo.userMsgProviders & 16) != 0) { y += '<option value=16>' + "CallMeBot" + '</option>'; }
11989
y += '</select>';
11990
x += '<table><tr><td>' + "Service" + '<td>' + y;
11991
- x += '<tr><td>' + "Handle" + '<td><input maxlength=64 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=account_manageMessagingValidate() onkeypress="if (event.key==\'Enter\') account_manageMessagingValidate(1)">';
11991
+ x += '<tr><td>' + "Handle" + '<td><input maxlength=1024 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=account_manageMessagingValidate() onkeypress="if (event.key==\'Enter\') account_manageMessagingValidate(1)">';
11992
x += '</table>';
11993
- if (serverinfo.discordUrl) { x += '<div id=d2discordurl style=display:none><br /><a href=' + serverinfo.discordUrl + ' target="_discord">' + "Join this Discord server to receive notifications." + '</a></div>' }
11993
+ if (serverinfo.discordUrl) { x += '<div id=d2discordurl style=display:none><br /><a href=' + serverinfo.discordUrl + ' target="_discord">' + "Join this Discord server to receive notifications." + '</a></div>'; }
11994
+ x += '<div id=d2callmebotinfo style=display:none><br /><a href=https://www.callmebot.com/blog/free-api-signal-send-messages/ target="_callmebot">' + "Signal" + '</a>, <a href=https://www.callmebot.com/blog/free-api-whatsapp-messages/ target="_callmebot">' + "Whatsapp" + '</a>, <a href=https://www.callmebot.com/blog/free-api-facebook-messenger/ target="_callmebot">' + "Facebook" + '</a></div>';
11995
setDialogMode(2, "Messaging Notifications", 3, account_manageMessagingAdd, x, 'verifyMessaging');
11996
Q('d2handleinput').focus();
11997
account_manageMessagingValidate();
@@ -11999,8 +12000,10 @@
12000
12001
function account_manageMessagingValidate(x) {
12002
if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); }
12003
+ QV('d2callmebotinfo', Q('d2serviceselect').value == 16);
12004
if (Q('d2serviceselect').value == 4) { Q('d2handleinput')['placeholder'] = "Username:0000"; }
12005
else if (Q('d2serviceselect').value == 8) { Q('d2handleinput')['placeholder'] = "username@server.com"; }
12006
+ else if (Q('d2serviceselect').value == 16) { Q('d2handleinput')['placeholder'] = "https://api.callmebot.com/..."; }
12007
else { Q('d2handleinput')['placeholder'] = "Username"; }
12008
var ok = (Q('d2handleinput').value.length > 0); QE('idx_dlgOkButton', ok); if ((x == 1) && ok) { dialogclose(1); }
12009
}
@@ -15860,21 +15863,29 @@
15863
var x = '<table style=width:100%><tr><td style=width:56px;vertical-align:top><img src="images/messaging40.png" style=padding:8px>';
15864
x += '<td style=width:100%>' + "Messaging account for this user.";
15865
var y = '<select id=d2serviceselect style=width:160px;margin-left:8px onchange=p30editMessagingValidate()><option value=0>' + "None" + '</option>';
15863
- if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
15864
- if ((serverinfo.userMsgProviders & 2) != 0) { y += '<option value=2>' + "Signal Messenger" + '</option>'; }
15865
- if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
15866
- if ((serverinfo.userMsgProviders & 8) != 0) { y += '<option value=8>' + "XMPP" + '</option>'; }
15866
+ if ((serverinfo.userMsgProviders & 1) != 0) { y += '<option value=1>' + "Telegram" + '</option>'; }
15867
+ if ((serverinfo.userMsgProviders & 4) != 0) { y += '<option value=4>' + "Discord" + '</option>'; }
15868
+ if ((serverinfo.userMsgProviders & 8) != 0) { y += '<option value=8>' + "XMPP" + '</option>'; }
15869
+ if ((serverinfo.userMsgProviders & 16) != 0) { y += '<option value=16>' + "CallMeBot" + '</option>'; }
15870
y += '</select>';
15871
x += '<table style=margin-top:12px><tr><td>' + "Service" + '<td>' + y;
15869
- x += '<tr><td>' + "Handle" + '<td><input maxlength=64 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=p30editMessagingValidate() onkeypress="if (event.key==\'Enter\') p30editMessagingValidate(1)">';
15872
+ x += '<tr><td>' + "Handle" + '<td><input maxlength=1024 style=width:160px;margin-left:8px id=d2handleinput onKeyUp=p30editMessagingValidate() onkeypress="if (event.key==\'Enter\') p30editMessagingValidate(1)">';
15873
x += '</table>';
15874
if (serverinfo.discordUrl) { x += '<div id=d2discordurl style=display:none><br /><a href=' + serverinfo.discordUrl + ' target="_discord">' + "Join this Discord server to receive notifications." + '</a></div>' }
15875
+ x += '<div id=d2callmebotinfo style=display:none><br /><a href=https://www.callmebot.com/blog/free-api-signal-send-messages/ target="_callmebot">' + "Signal" + '</a>, <a href=https://www.callmebot.com/blog/free-api-whatsapp-messages/ target="_callmebot">' + "Whatsapp" + '</a>, <a href=https://www.callmebot.com/blog/free-api-facebook-messenger/ target="_callmebot">' + "Facebook" + '</a></div>';
15876
setDialogMode(2, "Messaging Notifications", 3, p30editMessagingEx, x, 'verifyMessaging');
15877
Q('d2handleinput').focus();
15878
if (userinfo.msghandle) {
15879
if (userinfo.msghandle.startsWith('telegram:') && ((serverinfo.userMsgProviders & 1) != 0)) { Q('d2serviceselect').value = 1; Q('d2handleinput').value = userinfo.msghandle.substring(10); }
15880
if (userinfo.msghandle.startsWith('discord:') && ((serverinfo.userMsgProviders & 4) != 0)) { Q('d2serviceselect').value = 4; Q('d2handleinput').value = userinfo.msghandle.substring(8); }
15877
- if (userinfo.msghandle.startsWith('xmpp:') && ((serverinfo.userMsgProviders & 8) != 0)) { Q('d2serviceselect').value = 4; Q('d2handleinput').value = userinfo.msghandle.substring(5); }
15881
+ if (userinfo.msghandle.startsWith('xmpp:') && ((serverinfo.userMsgProviders & 8) != 0)) { Q('d2serviceselect').value = 8; Q('d2handleinput').value = userinfo.msghandle.substring(5); }
15882
+ if (userinfo.msghandle.startsWith('callmebot:') && ((serverinfo.userMsgProviders & 16) != 0)) {
15883
+ Q('d2serviceselect').value = 16;
15884
+ var toData = userinfo.msghandle.substring(10).split('|');
15885
+ if ((toData[0] == 'signal') && (toData.length == 3)) { Q('d2handleinput').value = 'https://api.callmebot.com/signal/send.php?phone=' + decodeURIComponent(toData[1]) + '&apikey=' + decodeURIComponent(toData[2]); }
15886
+ if ((toData[0] == 'whatsapp') && (toData.length == 3)) { Q('d2handleinput').value = 'https://api.callmebot.com/whatsapp.php?phone=' + decodeURIComponent(toData[1]) + '&apikey=' + decodeURIComponent(toData[2]); }
15887
+ if ((toData[0] == 'facebook') && (toData.length == 2)) { Q('d2handleinput').value = 'https://api.callmebot.com/facebook/send.php?apikey=' + decodeURIComponent(toData[1]); }
15888
+ }
15889
}
15890
p30editMessagingValidate();
15891
}
@@ -15882,9 +15893,11 @@
15893
function p30editMessagingValidate(x) {
15894
QE('d2handleinput', Q('d2serviceselect').value != 0);
15895
if (serverinfo.discordUrl) { QV('d2discordurl', Q('d2serviceselect').value == 4); }
15896
+ QV('d2callmebotinfo', Q('d2serviceselect').value == 16);
15897
if (Q('d2serviceselect').value == 0) { Q('d2handleinput')['placeholder'] = ''; }
15898
else if (Q('d2serviceselect').value == 4) { Q('d2handleinput')['placeholder'] = "Username:0000"; }
15899
else if (Q('d2serviceselect').value == 8) { Q('d2handleinput')['placeholder'] = "username@server.com"; }
15900
+ else if (Q('d2serviceselect').value == 16) { Q('d2handleinput')['placeholder'] = "https://api.callmebot.com/..."; }
15901
else { Q('d2handleinput')['placeholder'] = "Username"; }
15902
if (x == 1) { dialogclose(1); }
15903
}
@@ -15896,6 +15909,7 @@
15909
else if (Q('d2serviceselect').value == 1) { handle = 'telegram:@' + Q('d2handleinput').value; }
15910
else if (Q('d2serviceselect').value == 4) { handle = 'discord:' + Q('d2handleinput').value; }
15911
else if (Q('d2serviceselect').value == 8) { handle = 'xmpp:' + Q('d2handleinput').value; }
15912
+ else if (Q('d2serviceselect').value == 16) { handle = 'callmebot:' + Q('d2handleinput').value; }
15913
if (handle != null) { meshserver.send({ action: 'edituser', id: currentUser._id, msghandle: handle }); }
15914
}
15915