mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-14 10:53:23 +00:00
Refactor examples/inline_bot.py
This commit is contained in:
parent
a4f8dc907a
commit
c6871f8071
1 changed files with 21 additions and 7 deletions
|
|
@ -1,9 +1,11 @@
|
||||||
import asyncio
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiogram import Bot, types, Dispatcher, executor
|
from aiogram import Bot, Dispatcher, executor
|
||||||
|
from aiogram.types import InlineQuery, \
|
||||||
|
InputTextMessageContent, InlineQueryResultArticle
|
||||||
|
|
||||||
API_TOKEN = 'BOT TOKEN HERE'
|
API_TOKEN = 'BOT_TOKEN_HERE'
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
@ -12,10 +14,22 @@ dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
@dp.inline_handler()
|
@dp.inline_handler()
|
||||||
async def inline_echo(inline_query: types.InlineQuery):
|
async def inline_echo(inline_query: InlineQuery):
|
||||||
input_content = types.InputTextMessageContent(inline_query.query or 'echo')
|
# id affects both preview and content,
|
||||||
item = types.InlineQueryResultArticle(id='1', title='echo',
|
# so it has to be unique for each result
|
||||||
input_message_content=input_content)
|
# (Unique identifier for this result, 1-64 Bytes)
|
||||||
|
# you can set your unique id's
|
||||||
|
# but for example i'll generate it based on text because I know, that
|
||||||
|
# only text will be passed in this example
|
||||||
|
text = inline_query.query or 'echo'
|
||||||
|
input_content = InputTextMessageContent(text)
|
||||||
|
result_id: str = hashlib.md5(text.encode()).hexdigest()
|
||||||
|
item = InlineQueryResultArticle(
|
||||||
|
id=result_id,
|
||||||
|
title=f'Result {text!r}',
|
||||||
|
input_message_content=input_content,
|
||||||
|
)
|
||||||
|
# don't forget to set cache_time=1 for testing (default is 300s or 5m)
|
||||||
await bot.answer_inline_query(inline_query.id, results=[item], cache_time=1)
|
await bot.answer_inline_query(inline_query.id, results=[item], cache_time=1)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue