Skip to main content

Connect Telegram Bot

pyversions PyPi Status Build Connect Telegram Bot Quality Gate Status Coverage Maintainability Rating

Introduction

Connect Telegram Bot is the base class which helps to build custom commands for the django models. It allows getting historical data from django models, as well as develop custom pre-defined filters and execute custom django management commands (i.e. those which are executed through python manage.py $command)

Install

Connect Telegram Bot requires python 3.8 or later and has the following dependencies:

  • python-telegram-bot >=13.3
  • django>=2.2.19

Connect Telegram Bot can be installed from pypi.org using pip:

$ pip install connect-telegram-bot

Running The Bot

Define your command class extending the base

For example:


from myapp.models import MyAppModel

from telegram_bot.telegram_conversation import TelegramConversation


class MyAppConversation(TelegramConversation):
    COMMANDS_DIR = '/app/myapp/management/commands'

    def __init__(self, logger, suffix):
        super().__init__(logger, suffix)
        self.model = MyAppModel

    @property
    def saved_filters(self):
        return [
            'count',
        ]

    def get_count(self, update):
        amount = self._get_initial_queryset().count()
        self._reply(update, amount)
        self._default_query_context()


Create a directory in your project where these conversation implementations will be placed. Connect Telegram Bot requires multiple environment variables to be setup before executing:

Variable Description
TELEGRAM_BOT_TOKEN Telegram Token for the bot. Please refer to https://core.telegram.org/bots on how to create a bot.
DJANGO_SETTINGS_MODULE Django settings package for current project.
TELEGRAM_BOT_HANDLERS Directory where custom conversations (handlers) were placed, i.e. where we would put our MyAppConversation from above
TELEGRAM_BOT_COMMAND_SUFFIX In case of having multiple instances of the bot (with the same commands) we want to add some suffix to the commands, so that only specific bot is getting the command, so command becomes myappconversation_${SUFFIX}. If there is no need to have multiple instances of the same bot in the chat -- just leave this undefined.

Running The Bot

export TELEGRAM_BOT_TOKEN='....'; export DJANGO_SETTINGS_MODULE='settings.common'; export TELEGRAM_BOT_HANDLERS='/app/telegram_bot/conversation_handlers/'; export TELEGRAM_BOT_COMMAND_SUFFIX='suf1'; python telegram_bot/bot.py

Testing

  • Create virtualenv
  • Install project dependencies
python -m pip install --upgrade pip
pip install poetry
poetry update
  • Run tests
poetry run pytest

License

Connect Telegram Bot is released under the Apache License Version 2.0.

Metadata

Release files for connect-telegram-bot 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for connect-telegram-bot 1.0.1
File Size Uploaded
connect-telegram-bot-1.0.1.tar.gz 23.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for connect-telegram-bot 1.0.1
File Interpreter ABI Platform
connect_telegram_bot-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 37.5 kB

Release files / connect-telegram-bot-1.0.1.tar.gz

Download URL connect-telegram-bot-1.0.1.tar.gz
Size 23.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b8fe9ea31aeb2ce5182c484c6d73f8e0d3c25b345c61f4f4efc667cdc898ade1
BLAKE2b-256 checksum
How to use checksums
2b9cbbbf98beb769b81b3473fb2e01ed8a9e8b38c0df05b58bb32c5a618d481f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1041-azure

Release files / connect_telegram_bot-1.0.1-py3-none-any.whl

Download URL connect_telegram_bot-1.0.1-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cf3d8882c9b73a7f3622f01333adf82d70a5e6fa7037f8f099dd1bf159cf9ca
BLAKE2b-256 checksum
How to use checksums
1b798be5b906bb098a9b8b1110fda78852f37c12625f956d9c96ef887712376c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.2 Linux/5.4.0-1041-azure

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page