-
Notifications
You must be signed in to change notification settings - Fork 4
/
broadcaster.py
34 lines (30 loc) · 1.14 KB
/
broadcaster.py
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
26
27
28
29
30
31
32
33
34
import asyncio
import logging
from aiogram.utils import exceptions
from loader import bot
async def send_message(user_id: int, text: str, disable_notification: bool = False) -> bool:
"""
Safe messages sender
:param user_id:
:param text:
:param disable_notification:
:return:
"""
try:
await bot.send_message(user_id, text, disable_notification=disable_notification)
except exceptions.BotBlocked:
logging.error(f"Target [ID:{user_id}]: blocked by user")
except exceptions.ChatNotFound:
logging.error(f"Target [ID:{user_id}]: invalid user ID")
except exceptions.RetryAfter as e:
logging.error(f"Target [ID:{user_id}]: Flood limit is exceeded. Sleep {e.timeout} seconds.")
await asyncio.sleep(e.timeout)
return await send_message(user_id, text) # Recursive call
except exceptions.UserDeactivated:
logging.error(f"Target [ID:{user_id}]: user is deactivated")
except exceptions.TelegramAPIError:
logging.exception(f"Target [ID:{user_id}]: failed")
else:
logging.info(f"Target [ID:{user_id}]: success")
return True
return False