Skip to content

This is an additional implementation compared to the telebot module. This module is designed for quick initialization, authorization and rendering of various buttons/widgets for telegram bots.

License

Notifications You must be signed in to change notification settings

obervinov/telegram-package

Repository files navigation

Telegram Package

Release CodeQL Tests and checks

GitHub release (latest SemVer) GitHub last commit GitHub Release Date GitHub issues GitHub repo size

About this project

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.

GitHub Actions

Name Version
GitHub Actions Templates v1.0.13

Supported functions

  • 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

Data structure in Vault

The structure of storing the bot token in the Vault

# token data
 % vault kv get ${mount_point}/configuration/telegram
========= Secret Path =========
configuration/data/telegram

====== Data ======
Key         Value
---         -----
token       123456qwerty

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.

Installing with Poetry

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 = "v1.2.0" }

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
EOF

poetry install

Usage example

Environment variables

Name Description Default value
TELEGRAM_BOT_NAME Telegram bot name None
  1. 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()

About

This is an additional implementation compared to the telebot module. This module is designed for quick initialization, authorization and rendering of various buttons/widgets for telegram bots.

Topics

Resources

License

Security policy

Stars

Watchers

Forks