This is an additional implementation compared to the telebot module.
This module is designed for quick initialization, authorization, rendering various buttons/widgets and sending stylized messages for telegram-bots.
- Creating a connection to the telegram api and initializing the objects necessary for the bot to function (parser, format, types, etc)
- Generating an inline keyboard button with a matrix of the specified size from the passed elements
- Starting the bot polling process
- Sending or editing a stylized message to a user
- Deleting a message from chat
- Interception of invoked exceptions
The structure of storing the bot token in the Vault
Configuration path: ${mount_point}/configuration/telegram
{
"token": "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}
The policy
required by the module when interacting with Vault.
An example of a policy with all the necessary rights and a description can be found here.
tee -a pyproject.toml <<EOF
[tool.poetry]
name = myproject"
version = "1.0.0"
description = ""
[tool.poetry.dependencies]
python = "^3.10"
telegram = { git = "https://github.com/obervinov/telegram-package.git", tag = "v2.0.0" }
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
EOF
poetry install
Name | Description | Default value |
---|---|---|
TELEGRAM_BOT_NAME |
Telegram bot name | None |
- Create messages template file
tee -a configs/messages.json <<EOF
{
"templates": {
"test_message": {
"text": "Hi, <b>{0}</b>! {1}\nAccess for your account - allowed {2}",
"args": ["username", ":raised_hand:", ":unlocked:"]
}
}
}
-
Simple usage
# import module from telegram import TelegramBot # init class objects telegram = TelegramBot(vault_client) bot = telegram.telegram_bot # decorator @bot.message_handler(commands=['start']) def start_message(message): telegram.send_styled_message( message.chat.id, messages_template={ 'alias': 'hello_message', 'kwargs': { 'username': message.from_user.first_name } } ) # run bot pulling telegram.launch_bot()
-
With inline keyboard
# import module from telegram import TelegramBot # init class objects telegram_bot = TelegramBot(vault_client).telegram_bot # decorator @telegram_bot.message_handler(commands=['start']) def start_message(message): markup = telegram_bot.telegram.create_inline_markup( [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' ], 4 ) telegram_bot.send_message( message.chat.id, f"\U0001F4C5 Select month for the creating report", reply_markup=markup ) # run bot pulling telegram.launch_bot()
Name | Version |
---|---|
GitHub Actions Templates | v1.2.8 |