Created
April 8, 2025 18:41
-
-
Save kaellego/891d9a973b619d37d1f0e147d684cf9d to your computer and use it in GitHub Desktop.
Webhook WAHA WhatsApp for zabbix notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Whatsapp = { | |
| url: null, | |
| token: null, | |
| to: null, | |
| message: null, | |
| proxy: null, | |
| parse_mode: null, | |
| start_typing: null, | |
| escapeMarkup: function (str, mode) { | |
| switch (mode) { | |
| case 'markdown': | |
| return str.replace(/([_*\[`])/g, '\\$&'); | |
| case 'markdownv2': | |
| return str.replace(/([_*\[\]()~`>#+\-=|{}.!])/g, '\\$&'); | |
| case 'html': | |
| return str.replace(/<(\s|[^a-z\/])/g, '<$1'); | |
| default: | |
| return str; | |
| } | |
| }, | |
| sleep: function (ms) { | |
| var start = new Date().getTime(); | |
| while (new Date().getTime() < start + ms); | |
| }, | |
| sendMessage: function () { | |
| var params = { | |
| chatId: Whatsapp.to, | |
| reply_to: null, | |
| text: Whatsapp.message, | |
| linkPreview: true, | |
| linkPreviewHighQuality: false, | |
| session: Whatsapp.session | |
| }, | |
| response, | |
| res, | |
| request = new HttpRequest(); | |
| request.addHeader('accept: */*'); | |
| request.addHeader('accept: application/json'); | |
| request.addHeader('Content-Type: application/json'); | |
| request.addHeader('X-Api-Key: ' + Whatsapp.token); | |
| if (Whatsapp.parse_mode !== null) { | |
| params['parse_mode'] = Whatsapp.parse_mode; | |
| } | |
| if (Whatsapp.proxy) { | |
| request.setProxy(Whatsapp.proxy); | |
| } | |
| try { | |
| if (Whatsapp.startTyping) { | |
| var param = { | |
| chatId: Whatsapp.to, | |
| session: Whatsapp.session | |
| }; | |
| Whatsapp.sleep(Math.random() * 3000); | |
| res = request.post(Whatsapp.url + '/api/startTyping', JSON.stringify(param)); | |
| Zabbix.log(4, '[Whatsapp Webhook] startTyping: ' + res); | |
| } | |
| Zabbix.log(4, '[Whatsapp Webhook] URL: ' + Whatsapp.url); | |
| Zabbix.log(4, '[Whatsapp Webhook] params: ' + JSON.stringify(params)); | |
| response = request.post(Whatsapp.url + '/api/sendText', JSON.stringify(params)); | |
| Zabbix.log(4, '[Whatsapp Webhook] HTTP response: ' + response); | |
| if (Whatsapp.startTyping) { | |
| var val = { | |
| chatId: Whatsapp.to, | |
| session: Whatsapp.session | |
| }; | |
| Whatsapp.sleep(Math.random() * 3000); | |
| res = request.post(Whatsapp.url + '/api/stopTyping', JSON.stringify(val)); | |
| Zabbix.log(4, '[Whatsapp Webhook] stopTyping: ' + res); | |
| } | |
| } catch (error) { | |
| //Zabbix.log(4, 'Error sending message:', error); | |
| throw 'Error sending message: ' + error.message; | |
| } | |
| } | |
| }; | |
| try { | |
| var params = JSON.parse(value); | |
| if (typeof params.Token === 'undefined') { | |
| throw 'Incorrect value is given for parameter "Token": parameter is missing'; | |
| } | |
| Whatsapp.token = params.Token; | |
| if (params.HTTPProxy) { | |
| Whatsapp.proxy = params.HTTPProxy; | |
| } | |
| params.ParseMode = params.ParseMode.toLowerCase(); | |
| if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) { | |
| Whatsapp.parse_mode = params.ParseMode; | |
| } | |
| Whatsapp.session = params.Session; | |
| Whatsapp.to = params.To; | |
| Whatsapp.message = params.Subject + '\n' + params.Message; | |
| Whatsapp.startTyping = params.Starttyping; | |
| Whatsapp.url = params.Url; | |
| if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) { | |
| Whatsapp.message = Whatsapp.escapeMarkup(Whatsapp.message, params.ParseMode); | |
| } | |
| Whatsapp.sendMessage(); | |
| return 'OK'; | |
| } catch (error) { | |
| Zabbix.log(4, '[Whatsapp Webhook] notification failed: ' + error); | |
| throw 'Sending failed: ' + error + '.'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment