Community discussions

MikroTik App
 
elico
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Mon Nov 07, 2016 3:23 am

Sending telegram bot message

Tue Mar 23, 2021 11:46 pm

An example for sending a group or a single private message using a telegram bot.
:do { 
	:local telegramBotToken "xxxxxxxxx:secondpartOfTheToken";
	:local destination "Number";
	:local requestUrl "https://api.telegram.org/bot$telegramBotToken/sendMessage";
	:local httpData "{\"chat_id\": \"$destination\", \"text\": \"This is a test from fetch\"}";
	/tool fetch url=$requestUrl http-data=$httpData http-header-field=content-type:application/json http-method=post  keep-result=no
} on-error={
	/log info "finished with error";
}
/log info "finished";
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19103
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Sending telegram bot message

Wed Mar 24, 2021 12:02 am

Another example
/tool fetch "https://api.telegram.org/bot<yourtokencode>/sendMessage\?chat_id=<botcode>&text=Router"
Note: Any spaces in the text portion of the URL, the message you wish to send should contain no gaps.
Use the '+' symbol for spaces!!

You can add time for example.
:local sub1 ([/system clock get time])
:local sub2 ([/system clock get date])
/tool fetch "https://api.telegram.org/bot<yourtokencode>/sendMessage\?chat_id=<botcode>&text=Router+went+offline+at+@sub1+on+$sub2"
QUESTION: How do I also use keep-result=no in the script, as is done in the first example? I tried it after the word fetch and at the end after the last entry, neither worked and both prevented the script from working.
Last edited by anav on Wed Mar 24, 2021 1:50 am, edited 3 times in total.
 
gotsprings
Forum Guru
Forum Guru
Posts: 2102
Joined: Mon May 14, 2012 9:30 pm

Re: Sending telegram bot message

Wed Mar 24, 2021 12:48 am

As I stated before... A message from an offline router...
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19103
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Sending telegram bot message

Wed Mar 24, 2021 1:51 am

This script works well for me for internal interfaces at the moment and for system/cpu temperature and if you go to my other thread, trying to do so for UPS log entry!
viewtopic.php?f=9&t=173565&p=849993#p849993

The one thing I would like to add from the first example to my script is this
How do I also use keep-result=no in the script, as is done in the first example? I tried it after the word fetch and at the end after the last entry sub2" - neither worked and both prevented the script from working.
 
accarda
Member Candidate
Member Candidate
Posts: 208
Joined: Fri Apr 05, 2019 4:06 pm
Location: Italy

Re: Sending telegram bot message

Wed Mar 24, 2021 6:10 am

How do I also use keep-result=no in the script, as is done in the first example? I tried it after the word fetch and at the end after the last entry sub2" - neither worked and both prevented the script from working.
I use this statement to send Telegrams with that option included:
:local botId "7217xxxxxxxx"
:local chatId "yyyyyyyy"
:local site [/system identity get value-name=name]

/tool fetch url="https://api.telegram.org/bot$botId/sendMessage?chat_id=$chatId&text=MikroTik $site info:%0AIPSec phase2 error" keep-result=no
So the url is quoted and after that I use the option keep-result and it simply works
 
elico
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Mon Nov 07, 2016 3:23 am

Re: Sending telegram bot message

Mon Apr 05, 2021 10:39 am

This script works well for me for internal interfaces at the moment and for system/cpu temperature and if you go to my other thread, trying to do so for UPS log entry!
viewtopic.php?f=9&t=173565&p=849993#p849993

The one thing I would like to add from the first example to my script is this
How do I also use keep-result=no in the script, as is done in the first example? I tried it after the word fetch and at the end after the last entry sub2" - neither worked and both prevented the script from working.
paste here the whole script.
I cannot say anything without understanding what you are doing.
 
User avatar
flankymanga
just joined
Posts: 1
Joined: Fri Apr 16, 2021 2:48 pm
Location: Slovakia

Re: Sending telegram bot message

Fri Apr 16, 2021 3:04 pm

Hello

I am a new user here. So please forgive me if im posting in the wrong topic / section but:
I have a set of servers behind my Mikrotik RB2011UiAS-2HnD-IN. On top of that i have UPS with USB connection.
I would like to connect the UPS to the Mikrotik and when there is battery critical (low) event then the RouterOs will send pakets to all the servers informing them to shutdown gracefully and when power is back on mains, send them wake-on-lan magic packets.

I do know about Mikrotik UPS package however its functionality is pretty limited.
Any suggestions / help is more than welcome.
Also i do not know about scripting in RouterOS - maybe some good articles / tutorials would also help a lot.
(I am a programmer by profession).

Many thanks in advance for any guidance provided!

Who is online

Users browsing this forum: alexantao and 26 guests