Manual:The Dude v6/Dude Telegram Example: Difference between revisions

From MikroTik Wiki
Jump to navigation Jump to search
Telegram Example
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Overview ==
== Overview ==


Here is quick example how to instantly send messages from your RouterOS (Dude) to Telegram bot.
Here is quick example how to instantly send messages from your RouterOS (Dude) server to Telegram bot.


====== 1. Create Telegram BOT. ======  
====== 1. Create Telegram bot. ======  
To receive messages from the Dude server / RouterOS, you will need to create your Telegram BOT, more information can be found on this page:
To receive messages from the Dude server / RouterOS, you will need to create your Telegram bot, more information can be found on this page:


https://core.telegram.org/api
https://core.telegram.org/api


When the bot will be created, you will receive token which can be used for HTTP API
When the bot will be created, you will receive token which can be used for HTTPs API


Token Example:
Token sample:
309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB
309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB


Line 18: Line 18:
====== 2. Send messages from RouterOS/Dude . ======  
====== 2. Send messages from RouterOS/Dude . ======  


Telegram supports both - GET and POST HTTP method. Since /tool fetch is able to use HTTP GET method you can use this tool to send instant messages to Telegram
Telegram supports both - GET and POST HTTP method. Since /tool fetch is able to use HTTPs GET method you can use this tool to send instant messages to Telegram API.


To successfully send message to your bot, you will need to know the bot token. Simple send message looks like this:
To successfully send message to your bot, you will need to know the bot token.
Simple send message looks like this:


<pre>
<pre>
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage"
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage" keep-result=no
</pre>
</pre>


Line 31: Line 32:
* 309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB - '''Your Telegram bot token'''
* 309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB - '''Your Telegram bot token'''
* /sendMessage - '''Command type'''
* /sendMessage - '''Command type'''
* \?chat_id=312605050 - '''312605050 is the chat where messages will appear'''
* \?chat_id=312605050 - '''312605050 is the chat where the messages will appear'''
* &text=ExampleMessage - '''ExampleMessage is message string, which will be sent to your bot'''
* &text=ExampleMessage - '''ExampleMessage is message string, which will be sent to your bot'''


{{Note | Note that "?" need to be escaped with backlash}}
{{Note | Note that "?" need to be escaped with backslash}}


The variables in the URL you will need to change:
The variables in the URL you will need to change:


/tool fetch url="https://api.telegram.org/bot'''309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB'''/sendMessage\?chat_id='''312605050'''&text='''ExampleMessage'''"
/tool fetch url="https://api.telegram.org/bot'''309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB'''/sendMessage\?chat_id='''312605050'''&text='''ExampleMessage'''" keep-result=no


Most likely you will use the same bot and chat, this means each time you will need to change only message text:
Most likely you will use the same bot and chat, this means each time you will need to change only message text:


<pre>
<pre>
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpA/sendMessage\?chat_id=312605050&text=ExampleMessage"
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage" keep-result=no
</pre>
</pre>


Line 52: Line 53:
You can use Dude to execute commands in your RouterOS, this means you can use same tool fetch to send messages from the Dude server.
You can use Dude to execute commands in your RouterOS, this means you can use same tool fetch to send messages from the Dude server.


Dude notidication type which allows to execute commands in RouterOS is called '''execute on server'''. More about Dude notifications can be found here:
Dude notification type which allows to execute commands in RouterOS is called '''execute on server'''. More about Dude notifications can be found here:
http://wiki.mikrotik.com/wiki/Manual:The_Dude_v6/Notifications
http://wiki.mikrotik.com/wiki/Manual:The_Dude_v6/Notifications


Line 60: Line 61:
2. Add tool fetch command in the '''Command''' field
2. Add tool fetch command in the '''Command''' field


It's also possible to add Dude variables in the command:
 
[[File:Dude_Notifiation.png]]
 
 
'''It's also possible to use Dude variables in the command:'''


<pre>
<pre>
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpA/sendMessage\?chat_id=312605050&text=Time: [Time]; Device: [Device.FirstAddress]; Status: [Service.Status]"
/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=Time: [Time];
Device: [Device.FirstAddress]; Status: [Service.Status]" keep-result=no
</pre>
</pre>
'''This code will result in telegram notification messages:'''
[[File:Telegram.png]]


== Test fetch command ==
== Test fetch command ==


{{Note | The fastest way to check if everything works correctly is to use "Test" button, just note that with "Test" button Dude variables will not work, that's why for testing purposes use simple text.}}
{{Note | The fastest way to check if everything works correctly is to use "Test" button, just note that with "Test" button Dude variables will not work, that's why for testing purposes use simple text.}}
[[Category:Dude]]

Latest revision as of 08:42, 21 February 2017

Overview

Here is quick example how to instantly send messages from your RouterOS (Dude) server to Telegram bot.

1. Create Telegram bot.

To receive messages from the Dude server / RouterOS, you will need to create your Telegram bot, more information can be found on this page:

https://core.telegram.org/api

When the bot will be created, you will receive token which can be used for HTTPs API

Token sample: 309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

2. Send messages from RouterOS/Dude .

Telegram supports both - GET and POST HTTP method. Since /tool fetch is able to use HTTPs GET method you can use this tool to send instant messages to Telegram API.

To successfully send message to your bot, you will need to know the bot token. Simple send message looks like this:

/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage" keep-result=no 

Where the HTTPs URL GET query string is created from:

  • https://api.telegram.org/bot - The base of URL
  • 309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB - Your Telegram bot token
  • /sendMessage - Command type
  • \?chat_id=312605050 - 312605050 is the chat where the messages will appear
  • &text=ExampleMessage - ExampleMessage is message string, which will be sent to your bot

Note: Note that "?" need to be escaped with backslash


The variables in the URL you will need to change:

/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage" keep-result=no

Most likely you will use the same bot and chat, this means each time you will need to change only message text:

/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=ExampleMessage" keep-result=no 

If the Telegram bot is configured correctly, you should instantly see the message, when the tool fetch command is executed in RouterOS/Dude.

Dude notification example using Telegram bot

You can use Dude to execute commands in your RouterOS, this means you can use same tool fetch to send messages from the Dude server.

Dude notification type which allows to execute commands in RouterOS is called execute on server. More about Dude notifications can be found here: http://wiki.mikrotik.com/wiki/Manual:The_Dude_v6/Notifications

To create new notification, that will send messages to Telegram bot follow these steps:

1. Create new notification, with notification type "execute on server" 2. Add tool fetch command in the Command field



It's also possible to use Dude variables in the command:

/tool fetch url="https://api.telegram.org/bot309683994:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpAB/sendMessage\?chat_id=312605050&text=Time: [Time];
 Device: [Device.FirstAddress]; Status: [Service.Status]" keep-result=no 


This code will result in telegram notification messages:


Test fetch command

Note: The fastest way to check if everything works correctly is to use "Test" button, just note that with "Test" button Dude variables will not work, that's why for testing purposes use simple text.