112 lines
2.7 KiB
Python
112 lines
2.7 KiB
Python
import asyncio
|
||
import configparser
|
||
|
||
from aiogram import Bot, Dispatcher, F
|
||
from aiogram.filters import Command
|
||
from aiogram.types import Message, InlineQuery, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent,BufferedInputFile, InputMediaPhoto, ChosenInlineResult
|
||
from io import BytesIO
|
||
|
||
from image import generate_quote
|
||
from formater import format_quote
|
||
|
||
dp = Dispatcher()
|
||
bot = None
|
||
|
||
|
||
works = {}
|
||
|
||
async def main():
|
||
global bot
|
||
cfg = configparser.ConfigParser()
|
||
try:
|
||
cfg.read("config.ini")
|
||
except:
|
||
raise "can't load config.ini, make sure you have one"
|
||
token = cfg['bot']['BOT_TOKEN']
|
||
if len(token) == 0:
|
||
raise "token seems empty"
|
||
bot = Bot(token=token)
|
||
|
||
print("Starting bot...")
|
||
try:
|
||
await dp.start_polling(bot)
|
||
finally:
|
||
print("Bot stopped")
|
||
|
||
@dp.message(F.text, Command("start"))
|
||
async def start(message: Message):
|
||
await message.answer(f"usage:\n\n@{(await bot.get_me()).username} some cool quote")
|
||
|
||
@dp.inline_query()
|
||
async def inline_query(query: InlineQuery):
|
||
text = query.query.strip()
|
||
|
||
result = InlineQueryResultArticle(
|
||
id="generate",
|
||
title="сгенерировать",
|
||
description=text,
|
||
input_message_content=InputTextMessageContent(
|
||
message_text="ща",
|
||
),
|
||
reply_markup=InlineKeyboardMarkup(
|
||
inline_keyboard=[
|
||
[
|
||
InlineKeyboardButton(
|
||
text="⠀",
|
||
callback_data="noop",
|
||
)
|
||
]
|
||
]
|
||
),
|
||
)
|
||
|
||
await query.answer(
|
||
results=[result],
|
||
cache_time=0,
|
||
is_personal=True,
|
||
)
|
||
|
||
|
||
@dp.callback_query(F.data == "noop")
|
||
async def noop(callback):
|
||
await callback.answer()
|
||
|
||
|
||
@dp.chosen_inline_result()
|
||
async def chosen_inline_result(result: ChosenInlineResult):
|
||
if not result.inline_message_id:
|
||
print("Telegram не дал inline_message_id")
|
||
return
|
||
|
||
text = result.query
|
||
|
||
image = generate_quote(format_quote(text))
|
||
|
||
buffer = BytesIO()
|
||
image.save(buffer, "PNG")
|
||
buffer.seek(0)
|
||
|
||
msg = await bot.send_photo(
|
||
chat_id=result.from_user.id,
|
||
photo=BufferedInputFile(
|
||
buffer.read(),
|
||
filename="dengi.png",
|
||
),
|
||
)
|
||
|
||
file_id = msg.photo[-1].file_id
|
||
|
||
await bot.delete_message(
|
||
result.from_user.id,
|
||
msg.message_id,
|
||
)
|
||
|
||
await bot.edit_message_media(
|
||
inline_message_id=result.inline_message_id,
|
||
media=InputMediaPhoto(
|
||
media=file_id,
|
||
),
|
||
)
|
||
|
||
if __name__ == '__main__':
|
||
asyncio.run(main()) |