aboutsummaryrefslogtreecommitdiff
path: root/libs/msg.py
blob: 9bcc52aa02e30527994e18622c038acbd8a0921c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import asyncio
from typing import AsyncGenerator, Iterable

from aiogram import Bot
from aiogram.exceptions import TelegramAPIError, TelegramRetryAfter

from models import RichText


async def publish(
    bot: Bot,
    users: Iterable[int],
    rich_text: RichText,
) -> AsyncGenerator[int]:
    for n, user_id in enumerate(users, start=1):
        for _ in range(5):
            try:
                await rich_text.send(bot, user_id)
                break
            except TelegramRetryAfter as e:
                await asyncio.sleep(e.retry_after + 1)
            except TelegramAPIError:
                await asyncio.sleep(5)

        yield n