Telegram bot CLI

Telegram bots CLI connector for use in pandora alerts.

Introduction

Ver. 080621

Telegram bots CLI connector for use in pandora alerts.

Compatibility matrix

Python3 for source code
Linux x64 for compiled
Does not work on Windows systems

Requirements

The requirements are as follows:

Configuration

Creating a BOT in Telegram

From a Telegram account you must type /start to the user BotFather (https://telegram.me/botfather) to create a bot.

It will return a series of parameters and instructions that we can do for the creation and manipulation of bots.

image-1629719208995.png

We will use the /newbot option with which we will create a new bot.
It will ask us the name we want to give to our bot, in my case I have selected Pandora_FMS_Test_bot , but you can put the one you prefer.

Now it will ask us for a user id for the bot, this has to end in _bot in this case I will use the same one. Pandora_FMS_Test_bot.

Now it will return us the information of our bot and some instructions for its administration, we will copy and save the API token that is the one with which we will authenticate us as that bot.

image-1629719275374.png

Create a group and add the bot


To create a group from Telegram we simply click on create group and select the users that will be in that group. It is important that one of those users is our bot in order to be able to send messages to that specific group. We can also simply add the bot to an existing group.

image-1629719345270.png

Get the id of the group to send messages from the CLI

Now we need to obtain the id of the group in which we have added our bot, there are different ways to do it, but one of the easiest is to add another bot that extracts this information very easily GetIDs Bot (https://t.me/getidsbot).
We add it to the group we want to know the id and it will give us a series of instructions for its use. But just by adding it, it will give us the information we need, which is the group id.

image-1629719388833.png

Once we have the id of the group we copy and save it, we can delete this bot from the group if we want, it has already fulfilled the purpose of giving us the id.
group if we want, it has already fulfilled the objective of giving us the id.

Configuring Pandora-Telegram CLI

To use the Pandora Telegram CLI (source code) you must have installed python3 and pip3 for the dependencies.
To install the dependencies, in the package you download you will see the requirements.txt file, we place it in its directory and simply execute it:

pip3 install -r requirements.txt

This will download and install the necessary dependencies.
Once the dependencies are installed we run the pandora-telegram-cli.py -h file with the Python interpreter to see its help:

python3 pandora-telegram-cli.py -h
usage: pandora-telegram-cli.py [-h] -m MESSAGE -t TOKEN -c CHAT_ID
Bot telegram cli
optional arguments:
-h, --help show this help message and exit
-m MESSAGE, --message MESSAGE
Message to be send
-t TOKEN, --token TOKEN
Bot token
-c CHAT_ID, --chat_id CHAT_ID
chat id to send messages

If we see the help without any error we are ready to use it.

General plugin parameters

python pandora-telegram-cli.py -t <bot_token> -c <chat_id> -m <MESSAGE> [ --api_conf <API_CONF> ] [ --module_graph <MODULE_GRAPH> ] [ --tmp_dir <TMP_DIR> ]

If the credentials are correct, it will return in .json format the information of the message we have just sent, which we will be able to see received in our Telegram group.

image-1629719533273.png

image-1629719541503.png

Use in Pandora FMS

The first thing we should do is to upload the script to an accessible path in the machine where we have deployed the Pandora FMS server, in my case I will place it in/usr/share/pandora_server/util/pandora-telegram-cli.py. However, you can use any location as long as the pandora server has access to it.

Also, we must make sure that we have the dependencies installed for the plugin execution or that we have the compiled version in its default. To check it we simply execute the plugin without any parameter, which should give us an answer similar to:

python3 pandora-telegram-cli.py

image-1629719592284.png

In case you have any error, go to the plugin configuration section of this guide where it is described how to install the dependencies.

Having the plugin in the server and working correctly, now we have to declare it in the Pandora FMS web console.

Once we access as administrator (or with alert management permissions) we will go to the Alerts > Commands section.

image-1629719612705.png

Click on create new command.

image-1629719630194.png

Here we will define the execution of the script and the parameters that we will use making use of the PandoraFMS macros.
My definition will be:

image-1629719652071.png

Here we define a field for each parameter that the plugin will have. If we don't want to use any of the optional parameters we can simply leave them blank, or not define them at all.

Now we can go to configure the action using our command. We go to Alerts > Actions.

image-1629719669692.png

Click on Create.

image-1629719686807.png

We select the command we want to use, in this case the one we have just created for the Telegram bot.

Here we can define and modify the fields corresponding to the fields that we have defined, I have left it as default configuration in the command, so we can use it with what we have defined, but we can change it at the level of specific actions if we want to change the data that is sent for each action and change the trigger text and the recovery text.

Having the action configured we can simply go to a module or policy and apply this action with the corresponding template.
For more information about alerts configuration go to PandoraFMS official documentation:

image-1629719725081.png