mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
часть функционала бота
This commit is contained in:
parent
aae253c540
commit
fe9e7bd111
2
main.py
2
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():
|
||||
|
@ -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}
|
||||
"""))
|
71
olgram/commands/bot_actions.py
Normal file
71
olgram/commands/bot_actions.py
Normal file
@ -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}")
|
@ -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()
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user