Dude notofication to Telegram

These code is not workimg. Which ROS and Dude you use?

Please do not post spam/advertising links, as this board already supports images without generating any profit.

hi guyz, u can use my dude script here :

Boa tarde, ire ser seu salvador
Use
/tool fetch http-method=post
url="https://api.telegram.org/botSEUTOKEN/sendMessage"
http-data="chat_id=SEUCHATID&text=Dispositivo:%20[Device.Name]%20Status:%20[Service.Status]%20IP:%20[Device.AddressesColumn]%20Hora:%20[Time]"
keep-result=no

Blza cara! Tudo OK! Funcionou perfeitamente.

The problem was at the Bot-API and the space. After changing Bot-API and changing space to + symbol, the Dude started sending the notifications. Thanks for all to help.

I am having the same problem. Even if I put it as a scrip in Mikrotik, I am receiving the following error. I also tried getting assistance from Telegram, but no answers yet.

image

Good Morning, could you perhaps send me a sample of your scrip, because I have checked everything, but cant seems to get it fix

Instead of manually replacing spaces with %20 or _ or other manual % replacements, if your RouterOS is new enough you should use the :convert method as described in the thread linked above:

Hello there. /tool fetch url="https://api.telegram.org/botAPI/sendMessage\?chat_id=chat-id&text=Устройство:[Device.Name];+Статус:+[Service.Status];+IP:+[Device.AddressesColumn];+Время:+[Time]" keep-result=no

@CGGXANNX
With all due respect :slightly_smiling_face:, the approach here:
Is the Telegram script failing? - #8 by dahuafschmied
seems to me "easier" (in 7.21+).
Note: it is actually easier, but WRONG.

Maybe we should draw some lines:
< see post below by rextended >:

Anyone knows what x and y are?

Nah, http-percent-encoding is a hack that doesn't work properly, let me show you an example. We'll use this helper page that echos back the parameters it receives from your request with arbitrary parameters in the URL, example:

https://echo.free.beeceptor.com/sample?message=abc&user=john&id=123

will include this in the output JSON (at the bottom)

"parsedQueryParams": {
    "message": "abc",
    "user": "john",
    "id": "123"
}

Now let's do some test where our message to be sent will contain this string: "This is a string with whitespaces &special=characters 3 + 2 = ?" and we expect that the other end receive the exact content in the message parameter.

First attempt without special handling (like what the OP did with telegram)

{
    :local messageText "This is a string with whitespaces &special=characters 3 + 2 = ?";
    :local theURL "https://echo.free.beeceptor.com/sample?message=$messageText&user=john&id=123";
    /tool fetch url=$theURL output=user proplist=data;
}

As expected, the request is rejected with error 400.

Now let's set http-percent-encoding=yes

{
    :local messageText "This is a string with whitespaces &special=characters 3 + 2 = ?";
    :local theURL "https://echo.free.beeceptor.com/sample?message=$messageText&user=john&id=123";
    /tool fetch url=$theURL output=user proplist=data http-percent-encoding=yes;
}

We scroll to the right to see the end of the JSON output and... uh oh! The server received FOUR request parameters, the message parameter only contains a portion of the original message, while part of that messages became an extra special parameter. Obviously, our message could not be sent intact to the remote host!

Now the proper way, with proper URL encoding of the message text:

{
    :local messageText "This is a string with whitespaces &special=characters 3 + 2 = ?";
    :set messageText [:convert $messageText to=url]
    :local theURL "https://echo.free.beeceptor.com/sample?message=$messageText&user=john&id=123";
    /tool fetch url=$theURL output=user proplist=data;
}

The result is perfect, the server got exactly 3 parameters, with the message parameter retaining its original intended content.

Obviously we don't need to waste time finding out when we can use http-percent-encoding=yes because that is totally broken, and is a security risk!

Because if we cannot control the content of the message (for example it comes from some log entry with text value set by outside party) then additional URL parameters can be inserted by the outside party to your fetch URL parameters, something like &action=delete (like how the special parameter was inserted to the query in the example above).

So...

  1. RouterOS v6 -> Use URLencode by Rextended
  2. RouterOS v7 < 7.16.2 -> UPGRADE ROUTEROS
  3. RouterOS v7 >= 7.16.2 and < 7.20 -> Use URLencode by Rextended
  4. RouterOS v7 >= 7.20 use [:convert $messageText to=url]

ASCIItoCP1252toURLencode: ✂ Rextended Fragments of Snippets - #34 by rextended

EDIT: Added link

So we must

SHOUT

this:

DO NOT USE http-percent-encoding=yes

as a /tool/fetch parameter (available since 7.21) as it is:
a. a hack that doesn't work properly
b. totally broken
c. a security risk

Just DON'T

[TimeAndDate] doesn't work because it embeds a space.

Modify the notification in dude like this:

:local rawTime "[TimeAndDate]"; :local safeTime ""; :for i from=0 to=([:len $rawTime] - 1) do={ :local char [:pick $rawTime $i ($i + 1)]; :if ($char = " ") do={ :set safeTime ($safeTime . "_") } else={ :set safeTime ($safeTime . $char) } }; /tool fetch http-method=get url="https://telegram.org/sendMessage?chat_id=&text=Alerta+Dude:+El+servicio+[Probe.Name]+en+[Device.Name]+esta+[Service.Status]+Fecha:+ $safeTime]" keep-result=no

change the space character using this %20

Had this happen once too. I ended up creating a new bot token and adding it back, and it started working again. Might be worth a try if you haven't already.