Skip to main content

Django Telegram Bot

Project description

Django Telegram Bot

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

Introduction

Django 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

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

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

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

$ pip install telegram-django

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):
    def __init__(self, logger, model_datetime_property, suffix):
        super().__init__(logger, model_datetime_property, suffix)
        self.model = MyAppModel

    @property
    def custom_commands(self):
        return [
            'custom_command1',
        ]
        
    @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()


The method custom_commands must return a list of defined django commands which can be executed by this conversation handler. These commands are standard django commands which are normally executed via python manage.py $command. The method saved_filters must return a list of defined custom filters. The filter's body must be implemented in the same class using the convention get_$filter_name, like in the example above: for count filter the get_count method is implemented.

Add the following sections to your settings.py:

Define application in INSTALLED_APPS

    INSTALLED_APPS = [
        ...
        'django_telegram.bot',
        ...
    ]

Add section TELEGRAM_BOT for bot configuration

TELEGRAM_BOT = {
    'CONVERSATIONS': [
        'myapp.package1.package2.MyAppConversation',
    ],
    'TOKEN': '',
    'COMMANDS_SUFFIX': None,
    'HISTORY_LOOKUP_MODEL_PROPERTY': ''
}

Add section django_telegram_bot to logger configuration

LOGGING = {
...
    'loggers': {
...
        'django_telegram_bot': {
            'handlers': ['console'],
            'level': 'INFO',
        },
...
    },
...
}

Settings description:

Variable Description
TOKEN Telegram Token for the bot. Please refer to https://core.telegram.org/bots on how to create a bot.
CONVERSATIONS List of FQDNs for classes which implement and provide conversation instances
HISTORY_LOOKUP_MODEL_PROPERTY Property of the django model of DateTime type which is used to do history lookups
COMMANDS_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 as None.

Running The Bot

python manage.py start_bot

Testing

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

License

Django 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

telegram-django-1.0.4.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_django-1.0.4-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file telegram-django-1.0.4.tar.gz.

File metadata

  • Download URL: telegram-django-1.0.4.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.4 Linux/5.4.0-1043-azure

File hashes

Hashes for telegram-django-1.0.4.tar.gz
Algorithm Hash digest
SHA256 d86c0fecafa0d53262ab2364e72c9fa042c7929fd19182f02e3dd04cc89b234f
MD5 05abc8dd6b1854f9ae99c79157e2e8aa
BLAKE2b-256 7c65dd35936ec15dc151b80e71d7a61159d86d6be48ebae7e86f385a3a08aec4

See more details on using hashes here.

File details

Details for the file telegram_django-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: telegram_django-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.9.4 Linux/5.4.0-1043-azure

File hashes

Hashes for telegram_django-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bba9b909fa9b943f7e3ad7b694ddb25061b7a6a8067cd5e416a72e25e734cea6
MD5 671bf4841cc7d4f26cf57e1fb94ec59e
BLAKE2b-256 3571d3421a371eab6e936f3d5d9d92d625779c09b53fba8ef9f229161fd8e749

See more details on using hashes here.

Supported by

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