From fe9e7bd1117ac833d03b2bfe91d841a0ede4e25a Mon Sep 17 00:00:00 2001 From: mihalin Date: Mon, 6 Sep 2021 03:02:08 +0300 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D1=8C=20=D1=84=D1=83?= =?UTF-8?q?=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D0=B0=20=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- olgram/commands/bot.py | 66 ------------------------------- olgram/commands/bot_actions.py | 71 ++++++++++++++++++++++++++++++++++ olgram/commands/bots.py | 2 +- olgram/commands/menu.py | 21 +++++++++- 5 files changed, 92 insertions(+), 70 deletions(-) delete mode 100644 olgram/commands/bot.py create mode 100644 olgram/commands/bot_actions.py diff --git a/main.py b/main.py index 9dfb390..0ecda73 100644 --- a/main.py +++ b/main.py @@ -4,10 +4,10 @@ from tortoise import Tortoise from olgram.router import dp from olgram.settings import TORTOISE_ORM -import olgram.commands.bot import olgram.commands.bots import olgram.commands.start import olgram.commands.menu +import olgram.commands.bot_actions async def init_database(): diff --git a/olgram/commands/bot.py b/olgram/commands/bot.py deleted file mode 100644 index 3771990..0000000 --- a/olgram/commands/bot.py +++ /dev/null @@ -1,66 +0,0 @@ -""" -Здесь работа с конкретным ботом -""" -from aiogram import types, Bot as AioBot -from aiogram.dispatcher import FSMContext -from aiogram.utils.callback_data import CallbackData -from textwrap import dedent - -from olgram.utils.mix import try_delete_message -from olgram.models.models import Bot, User - -from olgram.router import dp - -# Пользователь выбрал бота -select_bot = CallbackData('bot_select', 'bot_id') -# Пользователь выбрал, что хочет сделать со своим ботом -bot_operation = CallbackData('bot_operation', 'bot_id', 'operation') -# Пользователь выбрал чат -select_bot_chat = CallbackData('chat_select', 'bot_id', 'chat_id') -# Пользователь выбрал чат - личные сообщения -select_bot_chat_personal = CallbackData('chat_select_personal', 'bot_id') - - -async def delete_bot_callback(bot: Bot, call: types.CallbackQuery, callback_data: dict, state: FSMContext): - """ - Кнопка "удалить" для бота - """ - await bot.delete() - await call.answer("Бот удалён") - await try_delete_message(call.message) - - - - -async def chat_selected_callback(bot: Bot, call: types.CallbackQuery, callback_data: dict, state: FSMContext): - """ - Пользователь выбрал групповой чат для бота - """ - chat_id = callback_data["chat_id"] - chat = await bot.group_chats.filter(id=chat_id).first() - if not chat: - await call.answer("Нельзя привязать бота к этому чату") - return - bot.group_chat = chat - await bot.save() - await call.answer(f"Выбран чат {chat.name}") - - -async def chat_selected_personal_callback(bot: Bot, call: types.CallbackQuery, callback_data: dict, state: FSMContext): - """ - Пользователь выбрал личный чат для бота - """ - bot.group_chat = None - await bot.save() - await call.answer(f"Выбран личный чат") - - -async def text_bot_callback(bot: Bot, call: types.CallbackQuery, callback_data: dict, state: FSMContext): - """ - Кнопка "текст" для бота - """ - await AioBot.get_current().send_message(call.message.chat.id, dedent(f""" - Текущий текст бота по кнопке start: - - {bot.start_text} - """)) diff --git a/olgram/commands/bot_actions.py b/olgram/commands/bot_actions.py new file mode 100644 index 0000000..82a2ba6 --- /dev/null +++ b/olgram/commands/bot_actions.py @@ -0,0 +1,71 @@ +""" +Здесь работа с конкретным ботом +""" +from aiogram import types, Bot as AioBot +from aiogram.utils.exceptions import TelegramAPIError +from aiogram.dispatcher import FSMContext +from aiogram.utils.callback_data import CallbackData +from textwrap import dedent + +from olgram.utils.mix import try_delete_message +from olgram.models.models import Bot, User + +from olgram.router import dp + + +@dp.message_handler(state="wait_start_text", content_types="text", regexp="^[^/].+") # Not command +async def start_text_received(message: types.Message, state: FSMContext): + await state.reset_state() + data = await state.get_data() + print(data) + bot = await Bot.get_or_none(pk=data.get("bot_id")) + bot.start_text = message.text + await bot.save() + await message.answer("Новый текст приветствия\n\n: " + message.text) + + +async def delete_bot(bot: Bot, call: types.CallbackQuery): + """ + Пользователь решил удалить бота + """ + await bot.delete() + await call.answer("Бот удалён") + try: + await call.message.delete() + except TelegramAPIError: + pass + + +async def reset_bot_text(bot: Bot, call: types.CallbackQuery): + """ + Пользователь решил сбросить текст бота к default + :param bot: + :param call: + :return: + """ + bot.start_text = bot._meta.fields_map['start_text'].default + await bot.save() + await call.answer("Текст сброшен") + + +async def select_chat(bot: Bot, call: types.CallbackQuery, chat: str): + """ + Пользователь выбрал чат, в который хочет получать сообщения от бота + :param bot: + :param call: + :param chat: + :return: + """ + if chat == "personal": + bot.group_chat = None + await bot.save() + await call.answer("Выбран личный чат") + return + + chat_obj = await bot.group_chats.filter(id=chat).first() + if not chat: + await call.answer("Нельзя привязать бота к этому чату") + return + bot.group_chat = chat + await bot.save() + await call.answer(f"Выбран чат {chat_obj.name}") diff --git a/olgram/commands/bots.py b/olgram/commands/bots.py index 7176571..34f92b3 100644 --- a/olgram/commands/bots.py +++ b/olgram/commands/bots.py @@ -101,5 +101,5 @@ async def bot_added(message: types.Message, state: FSMContext): except IntegrityError: return await on_duplication_bot() - await message.answer("Бот добавлен!") + await message.answer("Бот добавлен! Список ваших ботов: /mybots") await state.reset_state() diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index 53bbb58..1973542 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -6,6 +6,7 @@ from aiogram.dispatcher import FSMContext from aiogram.utils.callback_data import CallbackData from textwrap import dedent from olgram.utils.mix import edit_or_create +from olgram.commands import bot_actions menu_callback = CallbackData('menu', 'level', 'bot_id', 'operation', 'chat') @@ -18,6 +19,9 @@ async def send_bots_menu(chat_id: int, user_id: int, call=None): Отправить пользователю список ботов :return: """ + if call: + await call.answer() + user = await User.get_or_none(telegram_id=user_id) bots = await Bot.filter(owner=user) if not bots: @@ -44,6 +48,7 @@ async def send_bots_menu(chat_id: int, user_id: int, call=None): async def send_chats_menu(bot: Bot, call: types.CallbackQuery): + await call.answer() keyboard = types.InlineKeyboardMarkup(row_width=2) chats = await bot.group_chats.all() @@ -81,6 +86,7 @@ async def send_chats_menu(bot: Bot, call: types.CallbackQuery): async def send_bot_menu(bot: Bot, call: types.CallbackQuery): + await call.answer() keyboard = types.InlineKeyboardMarkup(row_width=2) keyboard.insert( types.InlineKeyboardButton(text="Текст", @@ -107,6 +113,7 @@ async def send_bot_menu(bot: Bot, call: types.CallbackQuery): async def send_bot_delete_menu(bot: Bot, call: types.CallbackQuery): + await call.answer() keyboard = types.InlineKeyboardMarkup(row_width=2) keyboard.insert( types.InlineKeyboardButton(text="Да, удалить бот", @@ -124,6 +131,7 @@ async def send_bot_delete_menu(bot: Bot, call: types.CallbackQuery): async def send_bot_text_menu(bot: Bot, call: types.CallbackQuery): + await call.answer() keyboard = types.InlineKeyboardMarkup(row_width=2) keyboard.insert( types.InlineKeyboardButton(text="Сбросить текст", @@ -151,8 +159,6 @@ async def send_bot_text_menu(bot: Bot, call: types.CallbackQuery): @dp.callback_query_handler(menu_callback.filter(), state="*") async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMContext): - await call.answer() - level = callback_data.get("level") if level == "0": @@ -174,4 +180,15 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon if operation == "delete": return await send_bot_delete_menu(bot, call) if operation == "text": + await state.set_state("wait_start_text") + await state.set_data({"bot_id": bot.id}) + return await send_bot_text_menu(bot, call) + + if level == "3": + if operation == "delete_yes": + return await bot_actions.delete_bot(bot, call) + if operation == "chat": + return await bot_actions.select_chat(bot, call, callback_data.get("chat")) + if operation == "reset_text": + await bot_actions.reset_bot_text(bot, call) return await send_bot_text_menu(bot, call)