Manual:The Dude v6/Dude Telegram Example

From MikroTik Wiki
Revision as of 10:50, 9 January 2017 by Pauls (talk | contribs)
Jump to navigation Jump to search

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"

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 messages will appear
  • &text=ExampleMessage - ExampleMessage is message string, which will be sent to your bot

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


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"

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:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpA/sendMessage\?chat_id=312605050&text=ExampleMessage"

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:AAFh8645FeAOgUbcOgUVIhtclbmzpwwIXpA/sendMessage\?chat_id=312605050&text=Time: [Time];
 Device: [Device.FirstAddress]; Status: [Service.Status]"


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.