Running aiogram in neighbor container, sending messages to telegram via redis
Project description
django-redis-aiogram
django-redis-aiogram
provides a quick way to install aiogram
in a container adjacent to django
, allowing you to use your own router and loop. Also Allows you to send messages through redis
.
Supported Python and Django releases
Current release of django-redis-aiogram
is 1.0.0, and it supports Python 3.8+ and Django 4.2+.
Installation
The easiest and recommended way to install django-redis-aiogram
is from PyPI
pip install django-redis-aiogram
You need to add telegram_bot
to INSTALLED_APPS
in your projects settings.py
.
# settings.py
INSTALLED_APPS = (
...
'telegram_bot',
...
)
Also, you need to specify the minimum settings:
# settings.py
TELEGRAM_BOT = {
'REDIS_URL': REDIS_URL,
'TOKEN': TELEGRAM_BOT_TOKEN
}
Next, add a separate container to your docker-compose.yml.
# docker-compose.yml
services:
...
telegram_bot:
container_name: telegram_bot
restart: always
command: python manage.py start_tgbot
build:
context: ./
Example Usage
To send a message, use the following code:
from telegram_bot import bot
bot.send_message_via_redis(chat_id=CHAT_ID, text=TEXT)
bot.send_message_via_redis(chat_id=CHAT_ID, caption=TEXT, photo=URL)
If you need to use handlers, create file tg_router.py
(by default) in your app, use the following code:
from aiogram import types, F
from telegram_bot import bot
@bot.message(F.text.startswith('/start'))
async def start_handler(message: types.Message) -> None:
await message.answer('hi')
@bot.message()
async def simple_handler(message: types.Message) -> None:
await message.reply(message.text)
YOu can use all handler types like in aiogram.
Settings
You can override settings:
# settings.py
TELEGRAM_BOT = {
{
# event expiration time in redis
'REDIS_EXP_TIME': 5,
# redis key for handling expired event
'REDIS_EXP_KEY': 'TELEGRAM_BOT_EXP',
# redis key for collecting messages
'REDIS_MESSAGES_KEY': 'TELEGRAM_BOT_MESSAGE',
# name of the module to find
'MODULE_NAME': 'tg_router',
# telegram bot token
'TOKEN': TELEGRAM_BOT_TOKEN,
# url for redis connection
'REDIS_URL': REDIS_URL
}
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
Built Distribution
Hashes for django-redis-aiogram-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86e2a213b09fb4c9873249a509e9a2e0c59baead7b8400a75eebfdfaee725f86 |
|
MD5 | 4cb7c6d58b10423ff2e05a717be5dbd0 |
|
BLAKE2b-256 | c301afc5e92088e8ac5f2f68d843c8d6b65fc637e168e3047242a53d2b8d1c31 |
Hashes for django_redis_aiogram-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b45cd0c066e7dc25acd2af1154fb8a0a0820c34da22d29d2aed60af8a38a87da |
|
MD5 | 3dd2cf30ac99fc22af3323d2d83197d2 |
|
BLAKE2b-256 | e2a56c1de6d7cc6c06a0e46f5d18c851f75f9a95da3cc801b75987eba771f1a5 |