aboutsummaryrefslogtreecommitdiff
path: root/libs/msg.py
diff options
context:
space:
mode:
Diffstat (limited to 'libs/msg.py')
-rw-r--r--libs/msg.py25
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 @@
1import asyncio
2from typing import AsyncGenerator, Iterable
3
4from aiogram import Bot
5from aiogram.exceptions import TelegramAPIError, TelegramRetryAfter
6
7from models import RichText
8
9
10async 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