Guest User

Untitled

a guest
Nov 16th, 2020
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Telegram = {
  2.     token: null,
  3.     to: null,
  4.     message: null,
  5.     proxy: null,
  6.     parse_mode: null,
  7.  
  8.     sendMessage: function() {
  9.         var params = {
  10.             chat_id: Telegram.to,
  11.             text: Telegram.message,
  12.             disable_web_page_preview: true,
  13.             disable_notification: false
  14.         },
  15.         data,
  16.         response,
  17.         request = new CurlHttpRequest(),
  18.         url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';
  19.  
  20.         if (Telegram.parse_mode !== null) {
  21.             params['parse_mode'] = Telegram.parse_mode;
  22.         }
  23.  
  24.         if (Telegram.proxy) {
  25.             request.SetProxy(Telegram.proxy);
  26.         }
  27.  
  28.         request.AddHeader('Content-Type: application/json');
  29.         data = JSON.stringify(params);
  30.  
  31.         // Remove replace() function if you want to see the exposed token in the log file.
  32.         Zabbix.Log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
  33.         Zabbix.Log(4, '[Telegram Webhook] params: ' + data);
  34.         response = request.Post(url, data);
  35.         Zabbix.Log(4, '[Telegram Webhook] HTTP code: ' + request.Status());
  36.  
  37.         try {
  38.             response = JSON.parse(response);
  39.         }
  40.         catch (error) {
  41.             response = null;
  42.         }
  43.  
  44.         if (request.Status() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
  45.             if (typeof response.description === 'string') {
  46.                 throw response.description;
  47.             }
  48.             else {
  49.                 throw 'Unknown error. Check debug log for more information.'
  50.             }
  51.         }
  52.     }
  53. }
  54.  
  55. try {
  56.     var params = JSON.parse(value);
  57.  
  58.     if (typeof params.Token === 'undefined') {
  59.         throw 'Incorrect value is given for parameter "Token": parameter is missing';
  60.     }
  61.  
  62.     Telegram.token = params.Token;
  63.  
  64.     if (params.HTTPProxy) {
  65.         Telegram.proxy = params.HTTPProxy;
  66.     }
  67.  
  68.     if (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {
  69.         Telegram.parse_mode = params.ParseMode;
  70.     }
  71.  
  72.     Telegram.to = params.To;
  73.     Telegram.message = params.Subject + '\n' + params.Message;
  74.     Telegram.sendMessage();
  75.  
  76.     return 'OK';
  77. }
  78. catch (error) {
  79.     Zabbix.Log(4, '[Telegram Webhook] notification failed: ' + error);
  80.     throw 'Sending failed: ' + error + '.';
  81. }
RAW Paste Data