chat_moderator_bot/handlers/message.py

253 lines
6.4 KiB
Python
Raw Permalink Normal View History

2024-07-17 20:35:45 +00:00
import json
from aiogram import Router, F
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery, Message
from templates import commands as tcommands
from templates import message as tmessage
from utils.db import Postgres
from utils.defs import delete_msg
router = Router()
@router.callback_query(F.data == 'message')
async def message_btn(call: CallbackQuery, state: FSMContext):
"""
Ловит кнопку 💬 Стоп сообщение
:param call: CallbackQuery
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
p = Postgres()
message_data = await p.get_data(
table_name='message'
)
preview_msg = await tmessage.send_preview(
message_data=message_data[0],
chat_id=call.from_user.id
)
last_msg = await call.message.answer(
text=tmessage.actions_text.format(
include='Да' if message_data[0]['included'] else 'Нет'
),
reply_markup=tmessage.actions_ikb(
included=message_data[0]['included']
)
)
await delete_msg(
msg=state_data.get('last_msg')
)
await state.update_data(
preview_msg=preview_msg,
last_msg=last_msg
)
@router.callback_query(F.data == 'edit_message')
async def edit_message_btn(call: CallbackQuery, state: FSMContext):
"""
Ловит кнопку 📝 Редактировать
:param call: CallbackQuery
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
await state_data['last_msg'].edit_text(
text=tmessage.send_message_text,
reply_markup=tmessage.send_message_ikb()
)
await state.set_state(
state=tmessage.SendState.message
)
await delete_msg(
msg=state_data.get('preview_msg')
)
await state.update_data(
edited_message_data={
'text': '',
'buttons': [],
'media': ''
}
)
@router.message(tmessage.SendState.message, F.content_type.in_({'text', 'photo'}))
async def get_edit_message(msg: Message, state: FSMContext):
"""
Ловит новое сообщение
:param msg: Message
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
await state_data['last_msg'].edit_text(
text=tmessage.send_buttons_text,
reply_markup=tmessage.send_buttons_ikb()
)
await msg.delete()
await state.set_state(
state=tmessage.SendState.buttons
)
if msg.photo:
state_data['edited_message_data']['text'] = msg.caption
state_data['edited_message_data']['media'] = msg.photo[-1].file_id
else:
state_data['edited_message_data']['text'] = msg.text
state_data['edited_message_data']['media'] = None
await state.update_data(
edited_message_data=state_data['edited_message_data']
)
@router.message(tmessage.SendState.buttons, F.text)
async def get_edit_buttons(msg: Message, state: FSMContext):
"""
Ловит новое url кнопки к сообщению
:param msg: Message
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
buttons = tmessage.build_url_ikb(msg.text)
await msg.delete()
if not buttons:
await state_data['last_msg'].edit_text(
text=tmessage.incorrect_data_text + '\n\n' + tmessage.send_buttons_text,
reply_markup=tmessage.send_buttons_ikb()
)
return
await delete_msg(
msg=state_data.get('last_msg')
)
state_data['edited_message_data']['buttons'] = buttons
preview_msg = await tmessage.send_preview(
message_data=state_data['edited_message_data'],
chat_id=msg.from_user.id
)
last_msg = await msg.answer(
text=tmessage.check_data_text,
reply_markup=tmessage.check_data_ikb()
)
await state.update_data(
edited_message_data=state_data['edited_message_data'],
preview_msg=preview_msg,
last_msg=last_msg
)
await state.set_state(
state=None
)
@router.callback_query(F.data == 'pass_buttons')
async def pass_buttons_btn(call: CallbackQuery, state: FSMContext):
"""
Ловит кнопку Пропустить
:param call: CallbackQuery
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
await delete_msg(
msg=state_data.get('last_msg')
)
preview_msg = await tmessage.send_preview(
message_data=state_data['edited_message_data'],
chat_id=call.from_user.id
)
last_msg = await call.message.answer(
text=tmessage.check_data_text,
reply_markup=tmessage.check_data_ikb()
)
await state.update_data(
preview_msg=preview_msg,
last_msg=last_msg
)
await state.set_state(
state=None
)
@router.callback_query(F.data == 'publish_message')
async def publish_message_btn(call: CallbackQuery, state: FSMContext):
"""
Ловит кнопку Опубликовать
:param call: CallbackQuery
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
state_data['edited_message_data']['buttons'] = json.dumps(
state_data['edited_message_data']['buttons']
)
p = Postgres()
await p.update_data(
table_name='message',
new_data=state_data['edited_message_data'],
query_filter={}
)
await state_data['last_msg'].edit_text(
text=tmessage.publish_message_text,
reply_markup=await tcommands.start_ikb()
)
await delete_msg(
msg=state_data.get('preview_msg')
)
@router.callback_query(F.data.startswith('included_message_'))
async def included_message_btn(call: CallbackQuery, state: FSMContext):
"""
Ловит кнопку 📝 Редактировать
:param call: CallbackQuery
:param state: FSMContext
:return:
"""
state_data = await state.get_data()
if call.data[17:] == 'true':
included = True
else:
included = False
await state_data['last_msg'].edit_text(
text=tmessage.actions_text.format(
include='Да' if included else 'Нет'
),
reply_markup=tmessage.actions_ikb(
included=included
)
)
p = Postgres()
await p.update_data(
table_name='message',
new_data={'included': included},
query_filter={}
)