| 
									
										
										
										
											2023-01-30 16:08:00 +01:00
										 |  |  |  | Chat with your router and send commands via Telegram bot | 
					
						
							|  |  |  |  | ======================================================== | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | [⬅️ Go back to main README](../README.md) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | > ℹ️ **Info**: This script can not be used on its own but requires the base
 | 
					
						
							|  |  |  |  | > installation. See [main README](../README.md) for details.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Description | 
					
						
							|  |  |  |  | ----------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | This script makes your device poll a Telegram bot for new messages. With | 
					
						
							|  |  |  |  | these messages you can send commands to your device and make it run them. | 
					
						
							|  |  |  |  | The resulting output is send back to you. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Requirements and installation | 
					
						
							|  |  |  |  | ----------------------------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Just install the script and the module for notifications via Telegram: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     $ScriptInstallUpdate telegram-chat,mod/notification-telegram; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Then create a schedule that runs the script periodically: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /system/scheduler/add start-time=startup interval=30s name=telegram-chat on-event="/system/script/run telegram-chat;"; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | > ⚠️ **Warning**: Make sure to keep the interval in sync when installing
 | 
					
						
							|  |  |  |  | > on several devices. Differing polling intervals will result in missed
 | 
					
						
							|  |  |  |  | > messages.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Configuration | 
					
						
							|  |  |  |  | ------------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Make sure to configure | 
					
						
							|  |  |  |  | [notifications via telegram](mod/notification-telegram.md) first. The | 
					
						
							|  |  |  |  | additional configuration goes to `global-config-overlay`, these are the | 
					
						
							|  |  |  |  | parameters: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | * `TelegramChatIdsTrusted`: an array with trusted chat ids or user names | 
					
						
							|  |  |  |  | * `TelegramChatGroups`: define the groups a device should belong to | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-07 14:14:04 +02:00
										 |  |  |  | > ℹ️ **Info**: Copy relevant configuration from
 | 
					
						
							|  |  |  |  | > [`global-config`](../global-config.rsc) (the one without `-overlay`) to
 | 
					
						
							|  |  |  |  | > your local `global-config-overlay` and modify it to your specific needs.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 16:08:00 +01:00
										 |  |  |  | Usage and invocation | 
					
						
							|  |  |  |  | -------------------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | This script is capable of chatting with multiple devices. By default a | 
					
						
							|  |  |  |  | device is passive and not acting on messages. To activate it send a message | 
					
						
							|  |  |  |  | containing `! identity` (exclamation mark, optional space and system's | 
					
						
							|  |  |  |  | identity). To query all dynamic ip addresses form a device named "*MikroTik*" | 
					
						
							|  |  |  |  | send `! MikroTik`, followed by `/ip/address/print where dynamic;`. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Devices can be grouped to chat with them simultaneously. The default group | 
					
						
							|  |  |  |  | "*all*" can be activated by sending `! @all`, which will make all devices | 
					
						
							|  |  |  |  | act on your commands. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Send a single exclamation mark or non-existent identity to make all | 
					
						
							|  |  |  |  | devices passive again. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Known limitations | 
					
						
							|  |  |  |  | ----------------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ### Do not use numeric ids!
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Numeric ids are valid within a session only. Usually you can use something | 
					
						
							|  |  |  |  | like this to print all ip addresses and remove the first one: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /ip/address/print; | 
					
						
							|  |  |  |  |     /ip/address/remove 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | This will fail when sent in separate messages. Instead you should use basic | 
					
						
							|  |  |  |  | scripting capabilities. Try to print what you want to act on... | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /ip/address/print where interface=eth; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ... verify and finally remove it. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /ip/address/remove [ find where interface=eth ]; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 10:39:34 +01:00
										 |  |  |  | ### Mind command runtime
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | The command is run in background while the script waits for it - about | 
					
						
							|  |  |  |  | 20 seconds at maximum. A command exceeding that time continues to run in | 
					
						
							|  |  |  |  | background, but the output in the message is missing or truncated then. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 17:25:48 +01:00
										 |  |  |  | If you still want a response you can work around this by making your code | 
					
						
							|  |  |  |  | send information on its own. Something like this should do the job: | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     :global SendTelegram; | 
					
						
							|  |  |  |  |     :delay 30s; | 
					
						
							|  |  |  |  |     $SendTelegram "Command finished" "Your command finished..."; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 14:29:16 +01:00
										 |  |  |  | ### Output size
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | RouterOS is limited in reading file content to a size of about four | 
					
						
							|  |  |  |  | kilobytes. Reading larger files does just fail, and that is also the limit | 
					
						
							|  |  |  |  | for command output. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-31 18:11:22 +01:00
										 |  |  |  | ### Sending commands to a group
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | Adding a bot to a group allows it to send messages to that group. To allow | 
					
						
							|  |  |  |  | it to receive messages you have to make it an admin of that group! It is | 
					
						
							|  |  |  |  | fine to deny all permissions, though. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-01 09:55:29 +01:00
										 |  |  |  | Also adding an admin to a group can cause the group id to change, so check | 
					
						
							| 
									
										
										
										
											2023-02-02 09:24:19 +01:00
										 |  |  |  | that if notifications break suddenly. | 
					
						
							| 
									
										
										
										
											2023-02-01 09:55:29 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 16:08:00 +01:00
										 |  |  |  | See also | 
					
						
							|  |  |  |  | -------- | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | * [Send notifications via Telegram](mod/notification-telegram.md) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | --- | 
					
						
							|  |  |  |  | [⬅️ Go back to main README](../README.md)   | 
					
						
							|  |  |  |  | [⬆️ Go back to top](#top) |