python_bot_v2/handlers/sendalarm.py

33 lines
1.3 KiB
Python
Raw Permalink Normal View History

import traceback
2022-12-11 20:48:14 +10:00
from create_bot import dp, bot
2022-12-11 20:48:14 +10:00
from aiogram import types, Dispatcher
from sqdb import User, session
2022-12-11 20:48:14 +10:00
@dp.message_handler(commands=['sendall'])
async def sendall(message: types.Message):
print('команда прошла')
2022-12-11 20:48:14 +10:00
if message.chat.type == 'private':
if message.from_user.id == 226765676:
text = message.text[9:]
try:
chats_id = session.query(User.chat_id).distinct()
2022-12-11 20:48:14 +10:00
for row in chats_id:
try:
await bot.send_message(row[0], text)
except Exception as e:
print('вероятно бота нет в чате, на всякий случай трейсбек:\n' +
"\n".join(traceback.format_tb(e.__traceback__)))
2022-12-11 20:48:14 +10:00
await bot.send_message(message.from_user.id, 'Сообщение успешно отправлено во все чаты')
except Exception as e:
2022-12-11 20:48:14 +10:00
session.rollback()
print('error sendall, traceback:\n' +
"\n".join(traceback.format_tb(e.__traceback__)))
finally:
session.close_all()
2022-12-11 20:48:14 +10:00
# def register_handlers_test(dp: Dispatcher):
# dp.register_message_handler(send_welcome)