Настройка текста приветствия

This commit is contained in:
mihalin 2021-09-08 02:26:35 +03:00
parent fe9e7bd111
commit 56b9528338
2 changed files with 25 additions and 25 deletions

View File

@ -1,27 +1,9 @@
""" """
Здесь работа с конкретным ботом Здесь работа с конкретным ботом
""" """
from aiogram import types, Bot as AioBot from aiogram import types
from aiogram.utils.exceptions import TelegramAPIError from aiogram.utils.exceptions import TelegramAPIError
from aiogram.dispatcher import FSMContext from olgram.models.models import Bot
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): async def delete_bot(bot: Bot, call: types.CallbackQuery):

View File

@ -8,6 +8,8 @@ from textwrap import dedent
from olgram.utils.mix import edit_or_create from olgram.utils.mix import edit_or_create
from olgram.commands import bot_actions from olgram.commands import bot_actions
import typing as ty
menu_callback = CallbackData('menu', 'level', 'bot_id', 'operation', 'chat') menu_callback = CallbackData('menu', 'level', 'bot_id', 'operation', 'chat')
@ -130,8 +132,9 @@ async def send_bot_delete_menu(bot: Bot, call: types.CallbackQuery):
"""), reply_markup=keyboard) """), reply_markup=keyboard)
async def send_bot_text_menu(bot: Bot, call: types.CallbackQuery): async def send_bot_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None, chat_id: ty.Optional[int] = None):
await call.answer() if call:
await call.answer()
keyboard = types.InlineKeyboardMarkup(row_width=2) keyboard = types.InlineKeyboardMarkup(row_width=2)
keyboard.insert( keyboard.insert(
types.InlineKeyboardButton(text="Сбросить текст", types.InlineKeyboardButton(text="Сбросить текст",
@ -139,7 +142,7 @@ async def send_bot_text_menu(bot: Bot, call: types.CallbackQuery):
chat=empty)) chat=empty))
) )
keyboard.insert( keyboard.insert(
types.InlineKeyboardButton(text="<< Назад", types.InlineKeyboardButton(text="<< Завершить редактирование",
callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, chat=empty)) callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, chat=empty))
) )
@ -154,7 +157,20 @@ async def send_bot_text_menu(bot: Bot, call: types.CallbackQuery):
Отправьте сообщение, чтобы изменить текст. Отправьте сообщение, чтобы изменить текст.
""") """)
text = text.format(bot.start_text) text = text.format(bot.start_text)
await edit_or_create(call, text, keyboard, parse_mode="markdown") if call:
await edit_or_create(call, text, keyboard, parse_mode="markdown")
else:
await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="markdown")
@dp.message_handler(state="wait_start_text", content_types="text", regexp="^[^/].+") # Not command
async def start_text_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)
bot.start_text = message.text
await bot.save()
await send_bot_text_menu(bot, chat_id=message.chat.id)
@dp.callback_query_handler(menu_callback.filter(), state="*") @dp.callback_query_handler(menu_callback.filter(), state="*")
@ -175,13 +191,15 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon
operation = callback_data.get("operation") operation = callback_data.get("operation")
if level == "2": if level == "2":
await state.reset_state()
if operation == "chat": if operation == "chat":
return await send_chats_menu(bot, call) return await send_chats_menu(bot, call)
if operation == "delete": if operation == "delete":
return await send_bot_delete_menu(bot, call) return await send_bot_delete_menu(bot, call)
if operation == "text": if operation == "text":
await state.set_state("wait_start_text") await state.set_state("wait_start_text")
await state.set_data({"bot_id": bot.id}) async with state.proxy() as proxy:
proxy["bot_id"] = bot.id
return await send_bot_text_menu(bot, call) return await send_bot_text_menu(bot, call)
if level == "3": if level == "3":