diff options
Diffstat (limited to 'libs/msg.py')
| -rw-r--r-- | libs/msg.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/libs/msg.py b/libs/msg.py new file mode 100644 index 0000000..9bcc52a --- /dev/null +++ b/libs/msg.py | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | import asyncio | ||
| 2 | from typing import AsyncGenerator, Iterable | ||
| 3 | |||
| 4 | from aiogram import Bot | ||
| 5 | from aiogram.exceptions import TelegramAPIError, TelegramRetryAfter | ||
| 6 | |||
| 7 | from models import RichText | ||
| 8 | |||
| 9 | |||
| 10 | async def publish( | ||
| 11 | bot: Bot, | ||
| 12 | users: Iterable[int], | ||
| 13 | rich_text: RichText, | ||
| 14 | ) -> AsyncGenerator[int]: | ||
| 15 | for n, user_id in enumerate(users, start=1): | ||
| 16 | for _ in range(5): | ||
| 17 | try: | ||
| 18 | await rich_text.send(bot, user_id) | ||
| 19 | break | ||
| 20 | except TelegramRetryAfter as e: | ||
| 21 | await asyncio.sleep(e.retry_after + 1) | ||
| 22 | except TelegramAPIError: | ||
| 23 | await asyncio.sleep(5) | ||
| 24 | |||
| 25 | yield n | ||
