templates first iteration

This commit is contained in:
mihalin 2022-02-11 01:02:23 +03:00
parent ea5249d1b8
commit 45e28bf9b7
4 changed files with 83 additions and 2 deletions

View File

@ -1,7 +1,7 @@
from olgram.router import dp from olgram.router import dp
from aiogram import types, Bot as AioBot from aiogram import types, Bot as AioBot
from olgram.models.models import Bot, User from olgram.models.models import Bot, User, DefaultAnswer
from aiogram.dispatcher import FSMContext from aiogram.dispatcher import FSMContext
from aiogram.utils.callback_data import CallbackData from aiogram.utils.callback_data import CallbackData
from textwrap import dedent from textwrap import dedent
@ -158,7 +158,7 @@ async def send_bot_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] =
) )
text = dedent(""" text = dedent("""
Сейчас вы редактируете текст, который отправляется после того, как пользователь отправит вашему боту {0} Сейчас вы редактируете текст, который отправляется после того, как пользователь отправит вашему боту @{0}
команду /start команду /start
Текущий текст: Текущий текст:
@ -188,6 +188,11 @@ async def send_bot_second_text_menu(bot: Bot, call: ty.Optional[types.CallbackQu
callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="text", callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="text",
chat=empty)) chat=empty))
) )
keyboard.insert(
types.InlineKeyboardButton(text="Шаблоны ответов...",
callback_data=menu_callback.new(level=3, bot_id=bot.id, operation="templates",
chat=empty))
)
keyboard.insert( keyboard.insert(
types.InlineKeyboardButton(text="Сбросить текст", types.InlineKeyboardButton(text="Сбросить текст",
callback_data=menu_callback.new(level=3, bot_id=bot.id, callback_data=menu_callback.new(level=3, bot_id=bot.id,
@ -211,6 +216,37 @@ async def send_bot_second_text_menu(bot: Bot, call: ty.Optional[types.CallbackQu
await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML") await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML")
async def send_bot_templates_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None,
chat_id: ty.Optional[int] = None):
if call:
await call.answer()
keyboard = types.InlineKeyboardMarkup(row_width=2)
keyboard.insert(
types.InlineKeyboardButton(text="<< Завершить редактирование",
callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, chat=empty))
)
text = dedent("""
Сейчас вы редактируете шаблоны ответов для @{0}. Текущие шаблоны:
<pre>
{1}
</pre>
Отправьте какую-нибудь фразу (например: "Ваш заказ готов, ожидайте!"), чтобы добавить её в шаблон.
Чтобы удалить шаблон из списка, отправьте его номер в списке (например, 4)
""")
templates = await bot.answers
templates_text = "\n".join(f"{n}. {template.text}" for n, template in enumerate(templates))
if not templates_text:
templates_text = "(нет шаблонов)"
text = text.format(bot.name, templates_text)
if call:
await edit_or_create(call, text, keyboard, parse_mode="HTML")
else:
await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML")
@dp.message_handler(state="wait_start_text", content_types="text", regexp="^[^/].+") # Not command @dp.message_handler(state="wait_start_text", content_types="text", regexp="^[^/].+") # Not command
async def start_text_received(message: types.Message, state: FSMContext): async def start_text_received(message: types.Message, state: FSMContext):
async with state.proxy() as proxy: async with state.proxy() as proxy:
@ -231,6 +267,28 @@ async def second_text_received(message: types.Message, state: FSMContext):
await send_bot_second_text_menu(bot, chat_id=message.chat.id) await send_bot_second_text_menu(bot, chat_id=message.chat.id)
@dp.message_handler(state="wait_template", content_types="text", regexp="^[^/](.+)?") # Not command
async def template_received(message: types.Message, state: FSMContext):
async with state.proxy() as proxy:
bot_id = proxy.get("bot_id")
bot = await Bot.get_or_none(pk=bot_id)
if message.text.isdigit():
# Delete template
number = int(message.text)
templates = await bot.answers
if number < 0 or number >= len(templates):
await message.answer(f"Неправильное число. Чтобы удалить шаблон, введите число от 0 до {len(templates)}")
return
await templates[number].delete()
else:
# Add template
template = DefaultAnswer(text=message.text, bot=bot)
await template.save()
await send_bot_templates_menu(bot, chat_id=message.chat.id)
@dp.callback_query_handler(menu_callback.filter(), state="*") @dp.callback_query_handler(menu_callback.filter(), state="*")
async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMContext): async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMContext):
level = callback_data.get("level") level = callback_data.get("level")
@ -245,6 +303,7 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
return return
if level == "1": if level == "1":
await state.reset_state()
return await send_bot_menu(bot, call) return await send_bot_menu(bot, call)
operation = callback_data.get("operation") operation = callback_data.get("operation")
@ -276,3 +335,8 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
if operation == "reset_second_text": if operation == "reset_second_text":
await bot_actions.reset_bot_second_text(bot, call) await bot_actions.reset_bot_second_text(bot, call)
return await send_bot_second_text_menu(bot, call) return await send_bot_second_text_menu(bot, call)
if operation == "templates":
await state.set_state("wait_template")
async with state.proxy() as proxy:
proxy["bot_id"] = bot.id
return await send_bot_templates_menu(bot, call)

View File

@ -0,0 +1,7 @@
-- upgrade --
CREATE TABLE IF NOT EXISTS "defaultanswer" (
"id" BIGSERIAL NOT NULL PRIMARY KEY,
"bot_id" INT NOT NULL REFERENCES "bot" ("id") ON DELETE CASCADE
);
-- downgrade --
DROP TABLE IF EXISTS "defaultanswer";

View File

@ -0,0 +1,4 @@
-- upgrade --
ALTER TABLE "defaultanswer" ADD "text" TEXT NOT NULL;
-- downgrade --
ALTER TABLE "defaultanswer" DROP COLUMN "text";

View File

@ -83,3 +83,9 @@ class BannedUser(Model):
class Meta: class Meta:
table = "bot_banned_user" table = "bot_banned_user"
class DefaultAnswer(Model):
id = fields.BigIntField(pk=True)
bot = fields.ForeignKeyField("models.Bot", related_name="answers", on_delete=fields.relational.CASCADE)
text = fields.TextField()