Skip to main content

Connect Telegram Bot

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

connect-telegram-bot-1.0.1.tar.gz (23.1 kB view hashes)

Uploaded Source

Built Distribution

connect_telegram_bot-1.0.1-py3-none-any.whl (14.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page