python_bot/handlers/sendalarm.py

33 lines
1.3 KiB
Python

import traceback
from create_bot import dp, bot
from aiogram import types, Dispatcher
from sqdb import User, session
@dp.message_handler(commands=['sendall'])
async def sendall(message: types.Message):
print('команда прошла')
if message.chat.type == 'private':
if message.from_user.id == 226765676:
text = message.text[9:]
try:
chats_id = session.query(User.chat_id).distinct()
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__)))
await bot.send_message(message.from_user.id, 'Сообщение успешно отправлено во все чаты')
except Exception as e:
session.rollback()
print('error sendall, traceback:\n' +
"\n".join(traceback.format_tb(e.__traceback__)))
finally:
session.close_all()
# def register_handlers_test(dp: Dispatcher):
# dp.register_message_handler(send_welcome)