mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
Bot menu
This commit is contained in:
parent
44807155f3
commit
4e70ebf32c
@ -1,16 +1,80 @@
|
|||||||
from aiogram import Bot, Dispatcher, executor, types
|
from aiogram import types, Bot as AioBot
|
||||||
from aiogram.dispatcher import FSMContext
|
from aiogram.dispatcher import FSMContext
|
||||||
|
from aiogram.utils.exceptions import Unauthorized, TelegramAPIError
|
||||||
|
import re
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from ..utils.router import Router
|
from ..utils.router import Router
|
||||||
|
from olgram.models.bot import Bot
|
||||||
|
from olgram.models.user import User
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
token_pattern = r'[0-9]{8,10}:[a-zA-Z0-9_-]{35}'
|
||||||
|
|
||||||
|
|
||||||
@router.message_handler(commands=["my_bots"])
|
@router.message_handler(commands=["my_bots"])
|
||||||
async def my_bots(message: types.Message, state: FSMContext):
|
async def my_bots(message: types.Message, state: FSMContext):
|
||||||
await message.answer("У тебя много ботов )")
|
user = await User.get_or_none(telegram_id=message.from_user.id)
|
||||||
|
bots = await Bot.filter(owner=user)
|
||||||
|
if not bots:
|
||||||
|
await message.answer(dedent("""
|
||||||
|
У вас нет добавленных ботов.
|
||||||
|
|
||||||
|
Отправьте команду /add_bot, чтобы добавить бот.
|
||||||
|
"""))
|
||||||
|
return
|
||||||
|
|
||||||
|
bots_str = "\n".join(["@" + bot.name for bot in bots])
|
||||||
|
await message.answer(dedent(f"""
|
||||||
|
Ваши боты:
|
||||||
|
{bots_str}
|
||||||
|
"""))
|
||||||
|
|
||||||
|
|
||||||
@router.message_handler(commands=["add_bot"])
|
@router.message_handler(commands=["add_bot"])
|
||||||
async def add_bot(message: types.Message, state: FSMContext):
|
async def add_bot(message: types.Message, state: FSMContext):
|
||||||
await message.answer("Добавь тогда ок")
|
await message.answer("Окей, пришли тогда токен plz")
|
||||||
|
await state.set_state("add_bot")
|
||||||
|
|
||||||
|
|
||||||
|
@router.message_handler(state="add_bot", content_types="text", regexp="^[^/].+") # Not command
|
||||||
|
async def bot_added(message: types.Message, state: FSMContext):
|
||||||
|
token = re.findall(token_pattern, message.text)
|
||||||
|
|
||||||
|
async def on_invalid_token():
|
||||||
|
await message.answer(dedent("""
|
||||||
|
Это не токен бота.
|
||||||
|
|
||||||
|
Токен выглядит вот так: 123456789:AAAA-abc123_AbcdEFghijKLMnopqrstu12
|
||||||
|
"""))
|
||||||
|
|
||||||
|
async def on_dummy_token():
|
||||||
|
await message.answer(dedent("""
|
||||||
|
Не удалось запустить этого бота: неверный токен
|
||||||
|
"""))
|
||||||
|
|
||||||
|
async def on_unknown_error():
|
||||||
|
await message.answer(dedent("""
|
||||||
|
Не удалось запустить этого бота: непредвиденная ошибка
|
||||||
|
"""))
|
||||||
|
|
||||||
|
if not token:
|
||||||
|
return await on_invalid_token()
|
||||||
|
|
||||||
|
token = token[0]
|
||||||
|
|
||||||
|
try:
|
||||||
|
test_bot = AioBot(token)
|
||||||
|
test_bot_info = await test_bot.get_me()
|
||||||
|
except ValueError:
|
||||||
|
return await on_invalid_token()
|
||||||
|
except Unauthorized:
|
||||||
|
return await on_dummy_token()
|
||||||
|
except TelegramAPIError:
|
||||||
|
return await on_unknown_error()
|
||||||
|
|
||||||
|
user, _ = await User.get_or_create(telegram_id=message.from_user.id)
|
||||||
|
bot = Bot(token=token, owner=user, name=test_bot_info.username)
|
||||||
|
await bot.save()
|
||||||
|
|
||||||
|
await message.answer("Бот добавлен!")
|
||||||
|
@ -1,17 +1,39 @@
|
|||||||
from aiogram import Bot, Dispatcher, executor, types
|
from aiogram import Bot, Dispatcher, executor, types
|
||||||
from aiogram.dispatcher import FSMContext
|
from aiogram.dispatcher import FSMContext
|
||||||
|
from textwrap import dedent
|
||||||
from ..utils.router import Router
|
from ..utils.router import Router
|
||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
|
|
||||||
|
|
||||||
@router.message_handler(commands=["start"])
|
@router.message_handler(commands=["start"], state="*")
|
||||||
async def start(message: types.Message, state: FSMContext):
|
async def start(message: types.Message, state: FSMContext):
|
||||||
"""
|
"""
|
||||||
Start command handler
|
Start command handler
|
||||||
"""
|
"""
|
||||||
|
await state.reset_state()
|
||||||
|
|
||||||
await message.answer(
|
# TODO: locale
|
||||||
"Привет. Я бот Olgram"
|
|
||||||
)
|
await message.answer(dedent("""
|
||||||
|
Olgram Bot — это конструктор ботов обратной связи в Telegram.
|
||||||
|
|
||||||
|
Используйте эти команды, чтобы управлять этим ботом:
|
||||||
|
|
||||||
|
/add_bot - добавить бот
|
||||||
|
/my_bots - управление ботами
|
||||||
|
|
||||||
|
/help - помощь
|
||||||
|
|
||||||
|
Ссылка на репозиторий https://github.com/civsocit/olgram
|
||||||
|
"""))
|
||||||
|
|
||||||
|
|
||||||
|
@router.message_handler(commands=["help"], state="*")
|
||||||
|
async def help(message: types.Message, state: FSMContext):
|
||||||
|
"""
|
||||||
|
Help command handler
|
||||||
|
"""
|
||||||
|
await message.answer(dedent("""
|
||||||
|
<todo: help here>
|
||||||
|
"""))
|
||||||
|
@ -6,6 +6,7 @@ class Bot(Model):
|
|||||||
id = fields.IntField(pk=True)
|
id = fields.IntField(pk=True)
|
||||||
token = fields.CharField(max_length=50, unique=True)
|
token = fields.CharField(max_length=50, unique=True)
|
||||||
owner = fields.ForeignKeyField("models.User", related_name="bots")
|
owner = fields.ForeignKeyField("models.User", related_name="bots")
|
||||||
|
name = fields.CharField(max_length=33)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
table = 'bot'
|
table = 'bot'
|
||||||
|
@ -3,7 +3,7 @@ from tortoise import Model, fields
|
|||||||
|
|
||||||
class User(Model):
|
class User(Model):
|
||||||
id = fields.IntField(pk=True)
|
id = fields.IntField(pk=True)
|
||||||
telegram_id = fields.IntField(index=True)
|
telegram_id = fields.IntField(index=True, unique=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
table = 'user'
|
table = 'user'
|
||||||
|
Loading…
Reference in New Issue
Block a user