Ad Widget

Collapse

Doesn't work BOLD in telegram media type

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mshkondin
    Junior Member
    • Apr 2023
    • 7

    #1

    Doesn't work BOLD in telegram media type


    Alerts - Media type - Telegram

    Problem started at {EVENT.TIME} on {EVENT.DATE}
    Problem name: {EVENT.NAME}
    \*Host: {HOST.NAME}\*
    Severity: {EVENT.SEVERITY}
    Operational data: {EVENT.OPDATA}
    Original problem ID: {EVENT.ID}
    {TRIGGER.URL} zz0.fe159j1h2wzz


    with ParseMode=markdownv2
    \*Host: {HOST.NAME}\* doesn't make bold text in triggers

    work only in test
  • Answer selected by mshkondin at 02-05-2023, 07:52.
    mshkondin
    Junior Member
    • Apr 2023
    • 7

    Well.. Solution:
    use \* your bold text \*
    for markdown and markdownv2 to get this
    Click image for larger version

Name:	image.png
Views:	2959
Size:	17.6 KB
ID:	463842

    Comment

    • cyber
      Senior Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • Dec 2006
      • 4807

      #2
      Never used it, but looking at https://core.telegram.org/bots/api#markdownv2-style
      it says to format it like
      Code:
      *bold \*text*​
      so in your case it should be
      Code:
      *bold \*Host: {HOST.NAME}\*​
      I think...
      You example is not markdownv2 but just markdown...

      Comment

      • mshkondin
        Junior Member
        • Apr 2023
        • 7

        #3
        Click image for larger version

Name:	image.png
Views:	2938
Size:	21.9 KB
ID:	463797

        when use *bold \*Host: {HOST.NAME}\*​​

        Comment

        • mshkondin
          Junior Member
          • Apr 2023
          • 7

          #4
          Click image for larger version

Name:	image.png
Views:	2963
Size:	20.2 KB
ID:	463799

          Scritp by default:
          var Telegram = {
          token: null,
          to: null,
          message: null,
          proxy: null,
          parse_mode: null,

          escapeMarkup: function (str, mode) {
          switch (mode) {
          case 'markdown':
          return str.replace(/([_*\[`])/g, '\\$&');

          case 'markdownv2':
          return str.replace(/([_*\[\]()~`>#+\-=|{}.!])/g, '\\$&');

          default:
          return str;
          }
          },

          sendMessage: function () {
          var params = {
          chat_id: Telegram.to,
          text: Telegram.message,
          disable_web_page_preview: true,
          disable_notification: false
          },
          data,
          response,
          request = new HttpRequest(),
          url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';

          if (Telegram.parse_mode !== null) {
          params['parse_mode'] = Telegram.parse_mode;
          }

          if (Telegram.proxy) {
          request.setProxy(Telegram.proxy);
          }

          request.addHeader('Content-Type: application/json');
          data = JSON.stringify(params);

          // Remove replace() function if you want to see the exposed token in the log file.
          Zabbix.log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
          Zabbix.log(4, '[Telegram Webhook] params: ' + data);
          response = request.post(url, data);
          Zabbix.log(4, '[Telegram Webhook] HTTP code: ' + request.getStatus());

          try {
          response = JSON.parse(response);
          }
          catch (error) {
          response = null;
          }

          if (request.getStatus() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
          if (typeof response.description === 'string') {
          throw response.description;
          }
          else {
          throw 'Unknown error. Check debug log for more information.';
          }
          }
          }
          };

          try {
          var params = JSON.parse(value);

          if (typeof params.Token === 'undefined') {
          throw 'Incorrect value is given for parameter "Token": parameter is missing';
          }

          Telegram.token = params.Token;

          if (params.HTTPProxy) {
          Telegram.proxy = params.HTTPProxy;
          }

          params.ParseMode = params.ParseMode.toLowerCase();

          if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
          Telegram.parse_mode = params.ParseMode;
          }

          Telegram.to = params.To;
          Telegram.message = params.Subject + '\n' + params.Message;

          if (['markdown', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
          Telegram.message = Telegram.escapeMarkup(Telegram.message, params.ParseMode);
          }

          Telegram.sendMessage();

          return 'OK';
          }
          catch (error) {
          Zabbix.log(4, '[Telegram Webhook] notification failed: ' + error);
          throw 'Sending failed: ' + error + '.';
          }​

          Comment

          • mshkondin
            Junior Member
            • Apr 2023
            • 7

            #5
            Well.. Solution:
            use \* your bold text \*
            for markdown and markdownv2 to get this
            Click image for larger version

Name:	image.png
Views:	2959
Size:	17.6 KB
ID:	463842

            Comment

            • rodrigolinux
              Junior Member
              • Jun 2022
              • 5

              #6
              With the help of GPT, he corrected part of the code, I tested it and it worked. Example of how to use Bold, use a *, and to break a line, use \n.

              Set ParseMode to markdon:

              Click image for larger version

Name:	image.png
Views:	2025
Size:	7.4 KB
ID:	486198

              Save your current Media Type script as a backup, and paste this script:


              var Telegram = {
              token: null,
              to: null,
              message: null,
              proxy: null,
              parse_mode: null,

              escapeMarkup: function (str, mode) {
              switch (mode) {
              case 'markdown':
              // Escaping necessary characters except for asterisks (*) used for bold in markdown
              return str.replace(/([_`\[\]])/g, '\\$&');

              case 'markdownv2':
              return str.replace(/([_\[\]()~`>#+\-=|{}.!])/g, '\\$&');

              case 'html':
              return str.replace(/<(\s|[^a-z\/])/g, '&lt;$1');

              default:
              return str;
              }
              },

              sendMessage: function () {
              var params = {
              chat_id: Telegram.to,
              text: Telegram.message,
              disable_web_page_preview: true,
              disable_notification: false
              },
              data,
              response,
              request = new HttpRequest(),
              url = 'https://api.telegram.org/bot' + Telegram.token + '/sendMessage';

              if (Telegram.parse_mode !== null) {
              params['parse_mode'] = Telegram.parse_mode;
              }

              if (Telegram.proxy) {
              request.setProxy(Telegram.proxy);
              }

              request.addHeader('Content-Type: application/json');
              data = JSON.stringify(params);

              // Remove replace() function if you want to see the exposed token in the log file.
              Zabbix.log(4, '[Telegram Webhook] URL: ' + url.replace(Telegram.token, '<TOKEN>'));
              Zabbix.log(4, '[Telegram Webhook] params: ' + data);
              response = request.post(url, data);
              Zabbix.log(4, '[Telegram Webhook] HTTP code: ' + request.getStatus());

              try {
              response = JSON.parse(response);
              }
              catch (error) {
              response = null;
              }

              if (request.getStatus() !== 200 || typeof response.ok !== 'boolean' || response.ok !== true) {
              if (typeof response.description === 'string') {
              throw response.description;
              }
              else {
              throw 'Unknown error. Check debug log for more information.';
              }
              }
              }
              };

              try {
              var params = JSON.parse(value);

              if (typeof params.Token === 'undefined') {
              throw 'Incorrect value is given for parameter "Token": parameter is missing';
              }

              Telegram.token = params.Token;

              if (params.HTTPProxy) {
              Telegram.proxy = params.HTTPProxy;
              }

              params.ParseMode = params.ParseMode.toLowerCase();

              if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
              Telegram.parse_mode = params.ParseMode;
              }

              Telegram.to = params.To;
              Telegram.message = params.Subject + '\n' + params.Message;

              // Replace \n with actual newlines
              Telegram.message = Telegram.message.replace(/\\n/g, '\n');

              if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
              Telegram.message = Telegram.escapeMarkup(Telegram.message, params.ParseMode);
              }

              Telegram.sendMessage();

              return 'OK';
              }
              catch (error) {
              Zabbix.log(4, '[Telegram Webhook] notification failed: ' + error);
              throw 'Sending failed: ' + error + '.';
              }​

              Comment

              • iav
                Junior Member
                • Apr 2014
                • 10

                #7
                Have same problem.
                Your solution allows bold style, but not italic.
                use for "markdown" case
                Code:
                return str.replace(/([`\[\]])/g, '\\$&');
                to make possible usage "_aaa_" to get italic text style.
                bold+italic style (like _*text*_) not works. But it's better then nothing.

                Comment

                • PapaTramp
                  Junior Member
                  • Apr 2023
                  • 3

                  #8
                  Solution not work in 7.2.
                  Text in Telegram looks like thet: *bold \* Problem: Network Generic Device: Interface 0/10(port 10: .34.33): Link down \*
                  Template in media type is: *bold \* Problem: {EVENT.NAME} \*
                  The \* TEXT \* dont works too.

                  Comment

                  Working...