# 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`.
## Installation
The easiest and recommended way to install `django-redis-aiogram` is from [PyPI](https://pypi.org/project/django-redis-aiogram/)
``` shell
pip install django-redis-aiogram
```
You need to add `telegram_bot` to `INSTALLED_APPS` in your projects `settings.py`.
``` python
# settings.py
INSTALLED_APPS = (
...
'telegram_bot',
...
)
```
Also, you need to specify the minimum settings:
``` python
# settings.py
TELEGRAM_BOT = {
'REDIS_URL': REDIS_URL,
'TOKEN': TELEGRAM_BOT_TOKEN,
}
```
Next, add a separate container to your docker-compose.yml.
(optional, if you want to use routers and handlers)
``` yaml
# 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:
``` python
# test.py
from aiogram import types, F
from telegram_bot import bot
# sending a message directly
bot.send_raw(chat_id=CHAT_ID, text=TEXT)
bot.send_raw('send_photo', chat_id=CHAT_ID, caption=TEXT, photo=URL)
# sending a message via redis
bot.send_redis(chat_id=CHAT_ID, text=TEXT)
bot.send_redis('send_photo', chat_id=CHAT_ID, caption=TEXT, photo=URL)
# markup example
markup = types.InlineKeyboardMarkup(inline_keyboard=[
[types.InlineKeyboardButton(
text='best project ever',
web_app=types.WebAppInfo(url='https://pypi.org/project/django-redis-aiogram')
)]
])
bot.send_raw(chat_id=CHAT_ID, text=TEXT, reply_markup=markup)
bot.send_redis(chat_id=CHAT_ID, text=TEXT, reply_markup=markup)
# if RAISE_EXCEPTION is True, you can use try-except to handle errors from send_raw
from aiogram.exceptions import TelegramBadRequest
try:
bot.send_raw(chat_id=CHAT_ID, text='**test*', parse_mode='Markdown')
except TelegramBadRequest:
print('Telegram bad request :)')
```
If you need to use handlers, create file `tg_router.py` (by default) in your app, use the following code:
``` python
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:
``` python
# settings.py
def default_kwargs(function: str) -> dict[str, Any]:
"""Default kwargs for telegram bot functions."""
prepared_dict = {
'send_message': {'parse_mode': 'HTML'},
'send_photo': {'parse_mode': 'Markdown', 'caption': '`Photo`'}
}
return prepared_dict.get(function, {})
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',
# default kwargs for telegram bot
'DEFAULT_KWARGS': default_kwargs,
# telegram bot token
'TOKEN': <TELEGRAM_BOT_TOKEN>,
# url for redis connection
'REDIS_URL': <REDIS_URL>,
# max retries for sending message
'MAX_RETRIES': 10,
# raise exception if error occurred
'RAISE_EXCEPTION': False
}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/CorneiZeR/django-redis-aiogram",
"name": "django-redis-aiogram",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "django redis aiogram docker",
"author": "Oleksii Kolosiuk",
"author_email": "kolosyuk1@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/6d/4d/a011b7996b0766151c5a32f1b201e17372147f68063c9d1a49e7f274dc29/django-redis-aiogram-1.0.8.tar.gz",
"platform": null,
"description": "# django-redis-aiogram\n\n`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`.\n\n## Installation\n\nThe easiest and recommended way to install `django-redis-aiogram` is from [PyPI](https://pypi.org/project/django-redis-aiogram/)\n\n``` shell\npip install django-redis-aiogram\n```\n\nYou need to add `telegram_bot` to `INSTALLED_APPS` in your projects `settings.py`.\n\n``` python\n# settings.py\n\nINSTALLED_APPS = (\n ...\n 'telegram_bot',\n ...\n)\n```\n\nAlso, you need to specify the minimum settings:\n``` python\n# settings.py\n\nTELEGRAM_BOT = {\n 'REDIS_URL': REDIS_URL,\n 'TOKEN': TELEGRAM_BOT_TOKEN,\n}\n```\n\nNext, add a separate container to your docker-compose.yml. \n(optional, if you want to use routers and handlers)\n\n``` yaml\n# docker-compose.yml\n\nservices:\n ...\n \n telegram_bot:\n container_name: telegram_bot\n restart: always\n command: python manage.py start_tgbot\n build:\n context: ./\n```\n\n## Example Usage\n\nTo send a message, use the following code:\n``` python\n# test.py\n\nfrom aiogram import types, F\nfrom telegram_bot import bot\n\n# sending a message directly\nbot.send_raw(chat_id=CHAT_ID, text=TEXT)\nbot.send_raw('send_photo', chat_id=CHAT_ID, caption=TEXT, photo=URL)\n\n# sending a message via redis\nbot.send_redis(chat_id=CHAT_ID, text=TEXT)\nbot.send_redis('send_photo', chat_id=CHAT_ID, caption=TEXT, photo=URL)\n\n# markup example\nmarkup = types.InlineKeyboardMarkup(inline_keyboard=[\n [types.InlineKeyboardButton(\n text='best project ever',\n web_app=types.WebAppInfo(url='https://pypi.org/project/django-redis-aiogram')\n )]\n])\n\nbot.send_raw(chat_id=CHAT_ID, text=TEXT, reply_markup=markup)\nbot.send_redis(chat_id=CHAT_ID, text=TEXT, reply_markup=markup)\n\n\n# if RAISE_EXCEPTION is True, you can use try-except to handle errors from send_raw\nfrom aiogram.exceptions import TelegramBadRequest\n\ntry:\n bot.send_raw(chat_id=CHAT_ID, text='**test*', parse_mode='Markdown')\nexcept TelegramBadRequest:\n print('Telegram bad request :)')\n```\n\nIf you need to use handlers, create file `tg_router.py` (by default) in your app, use the following code:\n\n``` python\nfrom aiogram import types, F\nfrom telegram_bot import bot\n\n\n@bot.message(F.text.startswith('/start'))\nasync def start_handler(message: types.Message) -> None:\n await message.answer('hi')\n\n\n@bot.message()\nasync def simple_handler(message: types.Message) -> None:\n await message.reply(message.text)\n```\n\nYou can use all handler types like in aiogram.\n\n## Settings\n\nYou can override settings:\n\n``` python\n# settings.py\n\ndef default_kwargs(function: str) -> dict[str, Any]:\n \"\"\"Default kwargs for telegram bot functions.\"\"\"\n prepared_dict = {\n 'send_message': {'parse_mode': 'HTML'},\n 'send_photo': {'parse_mode': 'Markdown', 'caption': '`Photo`'}\n }\n return prepared_dict.get(function, {})\n\nTELEGRAM_BOT = {\n {\n # event expiration time in redis\n 'REDIS_EXP_TIME': 5,\n # redis key for handling expired event\n 'REDIS_EXP_KEY': 'TELEGRAM_BOT_EXP',\n # redis key for collecting messages\n 'REDIS_MESSAGES_KEY': 'TELEGRAM_BOT_MESSAGE',\n # name of the module to find\n 'MODULE_NAME': 'tg_router',\n # default kwargs for telegram bot\n 'DEFAULT_KWARGS': default_kwargs,\n # telegram bot token\n 'TOKEN': <TELEGRAM_BOT_TOKEN>,\n # url for redis connection\n 'REDIS_URL': <REDIS_URL>,\n # max retries for sending message\n 'MAX_RETRIES': 10,\n # raise exception if error occurred\n 'RAISE_EXCEPTION': False\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Running aiogram in neighbor container, sending messages to telegram via redis",
"version": "1.0.8",
"project_urls": {
"Homepage": "https://github.com/CorneiZeR/django-redis-aiogram"
},
"split_keywords": [
"django",
"redis",
"aiogram",
"docker"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "be5bef1b9ad0f0baa96a7766c118da8fa68920f37b08a1e165baf6bdd78c7b6f",
"md5": "4b3b05dd8fdc27448e00447a77ee2554",
"sha256": "0e8516757bc29a3c294e877db49e98adc4d8094d914d6b402abeb527b3faba30"
},
"downloads": -1,
"filename": "django_redis_aiogram-1.0.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4b3b05dd8fdc27448e00447a77ee2554",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 9817,
"upload_time": "2024-02-20T16:34:07",
"upload_time_iso_8601": "2024-02-20T16:34:07.591357Z",
"url": "https://files.pythonhosted.org/packages/be/5b/ef1b9ad0f0baa96a7766c118da8fa68920f37b08a1e165baf6bdd78c7b6f/django_redis_aiogram-1.0.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6d4da011b7996b0766151c5a32f1b201e17372147f68063c9d1a49e7f274dc29",
"md5": "5db2492a92295ccdde331194051fcf7b",
"sha256": "10cdefa64628f50535bc88001f417a7f4b5ff19a0b0c415a269a88071c5d1126"
},
"downloads": -1,
"filename": "django-redis-aiogram-1.0.8.tar.gz",
"has_sig": false,
"md5_digest": "5db2492a92295ccdde331194051fcf7b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7904,
"upload_time": "2024-02-20T16:34:10",
"upload_time_iso_8601": "2024-02-20T16:34:10.582082Z",
"url": "https://files.pythonhosted.org/packages/6d/4d/a011b7996b0766151c5a32f1b201e17372147f68063c9d1a49e7f274dc29/django-redis-aiogram-1.0.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-20 16:34:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "CorneiZeR",
"github_project": "django-redis-aiogram",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-redis-aiogram"
}