mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
# Conflicts: # README.md # README.rst # aiogram/__init__.py # aiogram/bot/bot.py # aiogram/contrib/fsm_storage/redis.py # aiogram/contrib/middlewares/logging.py # aiogram/dispatcher/dispatcher.py # aiogram/dispatcher/filters/__init__.py # aiogram/dispatcher/filters/builtin.py # aiogram/dispatcher/filters/filters.py # aiogram/dispatcher/filters/state.py # aiogram/dispatcher/handler.py # aiogram/dispatcher/webhook.py # aiogram/types/base.py # aiogram/types/chat.py # aiogram/types/chat_member.py # aiogram/types/input_media.py # aiogram/types/message.py # aiogram/utils/callback_data.py # aiogram/utils/deprecated.py # aiogram/utils/exceptions.py # aiogram/utils/executor.py # aiogram/utils/helper.py # aiogram/utils/json.py # aiogram/utils/mixins.py # aiogram/utils/payload.py # dev_requirements.txt # docs/source/index.rst # examples/callback_data_factory.py # examples/check_user_language.py # examples/echo_bot.py # examples/finite_state_machine_example.py # examples/i18n_example.py # examples/inline_bot.py # examples/media_group.py # examples/middleware_and_antiflood.py # examples/payments.py # examples/proxy_and_emojize.py # examples/regexp_commands_filter_example.py # examples/throtling_example.py # examples/webhook_example.py # examples/webhook_example_2.py # setup.py # tests/test_bot.py # tests/test_token.py # tests/types/dataset.py
74 lines
2 KiB
Python
74 lines
2 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from aiogram import Bot, Dispatcher, types
|
|
from aiogram.utils import exceptions, executor
|
|
|
|
API_TOKEN = "BOT TOKEN HERE"
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
log = logging.getLogger("broadcast")
|
|
|
|
bot = Bot(token=API_TOKEN, parse_mode=types.ParseMode.HTML)
|
|
dp = Dispatcher(bot)
|
|
|
|
|
|
def get_users():
|
|
"""
|
|
Return users list
|
|
|
|
In this example returns some random ID's
|
|
"""
|
|
yield from (61043901, 78238238, 78378343, 98765431, 12345678)
|
|
|
|
|
|
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:
|
|
log.error(f"Target [ID:{user_id}]: blocked by user")
|
|
except exceptions.ChatNotFound:
|
|
log.error(f"Target [ID:{user_id}]: invalid user ID")
|
|
except exceptions.RetryAfter as e:
|
|
log.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:
|
|
log.error(f"Target [ID:{user_id}]: user is deactivated")
|
|
except exceptions.TelegramAPIError:
|
|
log.exception(f"Target [ID:{user_id}]: failed")
|
|
else:
|
|
log.info(f"Target [ID:{user_id}]: success")
|
|
return True
|
|
return False
|
|
|
|
|
|
async def broadcaster() -> int:
|
|
"""
|
|
Simple broadcaster
|
|
|
|
:return: Count of messages
|
|
"""
|
|
count = 0
|
|
try:
|
|
for user_id in get_users():
|
|
if await send_message(user_id, "<b>Hello!</b>"):
|
|
count += 1
|
|
await asyncio.sleep(0.05) # 20 messages per second (Limit: 30 messages per second)
|
|
finally:
|
|
log.info(f"{count} messages successful sent.")
|
|
|
|
return count
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Execute broadcaster
|
|
executor.start(dp, broadcaster())
|