Telegram bot CLI

Telegram bots CLI connector for use in pandora alerts.

Introduction

Version 080621.

Telegram® bots CLI connector for use in Pandora FMS alerts.

Compatibility matrix

Requirements

The requirements are as follows:

Configuration

Creation of the BOT on Telegram

From a Telegram® account, you must type /start to the user BotFather to create a  bot.

It will return a series of parameters and instructions available for the creation and manipulation of bots:

image-1626693442412.png

Use the /newbot option to create a new bot.

It will ask for the name, in this case Pandora_FMS_Test_bot , you can use any name you prefer.

Now you will be asked for a user ID for the bot, which must end in _bot. In this case, use the same one as before: Pandora_FMS_Test_bot.

It will now return information about our bot and some instructions for its administration. You must copy and save the API token, which is what we will use to authenticate ourselves as that bot.

image-1629719317095.png 

Create a group and add the bot


To create a group from Telegram, you must click on create group and select the users who will be in that group. It is important that one of those users is our bot in order to send messages to that group  specifically.

You can also simply add the bot to an existing group.

image-1626695074691.png

Obtain the group ID to send messages from the CLI

Now we need to obtain the id of the group where we added our bot. There are different ways to do this. One of the simplest is to add another bot that extracts this information very easily: GetIDs Bot.

We add it to the group we want to know the id of, and it will give a series of instructions for its use. Just by adding it, it provides the information we need (id of the group).

image-1629719396585.png

Once we have the group ID, we copy and save it.

We can remove this GetIDs Bot from the group because it has already fulfilled its purpose of giving us the ID.

Configure Pandora-Telegram CLI


To use Pandora Telegram CLI (source code), you must have Python version 3 and Pip version 3 installed for dependencies.  To install the dependencies, in the package to be downloaded, you will see the file requirements.txt. Go to its directory and run:

pip3 install -r requirements.txt

 

This will download and install the necessary dependencies.

Once the dependencies are installed, run the pandora-telegram-cli.py -h file with the Python interpreter to view 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 errors, then we are ready to use it.

General plugin parameters

# [Optional parameter]
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> ]


image-1629719533273.png

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-1629719541503.png

Use in Pandora FMS

The Telegram® plugin is fully integrated into Pandora FMS version 800.

The default script is located at: /usr/share/pandora_server/util/pandora-telegram-cli.py. However, any location can be used as long as the PFMS Server has access to it.

You must ensure that all dependencies are installed, check this, run:

python3 pandora-telegram-cli.py

You will get a response similar to this:

image-1629719592284.png

If there is an error, the dependencies must be reinstalled:

pip3 install -r requirements.txt

Telegram alert command

To verify the installation of its command, use the Management → Alerts→Commands menu and enter the search key in Filter:

pfms-telegram-integration-140.png

This command does not allow editing or deletion, so it is considered read-only and is used by the system. Therefore, you must go to the general settings to save the token obtained in the previous topic.

Managements → Settings → System settings → General setup → Alerts configuration menu:

pfms-telegram-integration-142.png

The field displays the token in plain text. Take the necessary precautions to prevent third parties from viewing it.

When finished, click the Update button to save the Telegram token in the database.

Telegram action command

Go to the Management→Alerts→ Actions menu and in Filter select Pandora Telegram in the Command field. The alert actions that use the Pandora Telegram command will be displayed:

pfms-telegram-integration-144.png

You may:

For any of the three cases, the Chat ID must always be configured, obtained as indicated in the previous topic.

Whether editing, copying and editing, or creating, the configuration process is similar:

pfms-telegram-integration-146.png

Click Create. Once you have created this alert action, it can be included in a policya template, or Module.

If additional information is required, please visit the documentation for the plugin.