Connect Telegram Bot
Project description
Connect Telegram Bot
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for connect-telegram-bot-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8fe9ea31aeb2ce5182c484c6d73f8e0d3c25b345c61f4f4efc667cdc898ade1 |
|
MD5 | 80e9ac496b737558b8d829ac25258086 |
|
BLAKE2b-256 | 2b9cbbbf98beb769b81b3473fb2e01ed8a9e8b38c0df05b58bb32c5a618d481f |
Hashes for connect_telegram_bot-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cf3d8882c9b73a7f3622f01333adf82d70a5e6fa7037f8f099dd1bf159cf9ca |
|
MD5 | d6a07a2df4c1892e13464f211e56c480 |
|
BLAKE2b-256 | 1b798be5b906bb098a9b8b1110fda78852f37c12625f956d9c96ef887712376c |