Inline bot

inline_bot.py
 1import hashlib
 2import logging
 3
 4from aiogram import Bot, Dispatcher, executor
 5from aiogram.types import InlineQuery, \
 6    InputTextMessageContent, InlineQueryResultArticle
 7
 8API_TOKEN = 'BOT_TOKEN_HERE'
 9
10logging.basicConfig(level=logging.DEBUG)
11
12bot = Bot(token=API_TOKEN)
13dp = Dispatcher(bot)
14
15
16@dp.inline_handler()
17async def inline_echo(inline_query: InlineQuery):
18    # id affects both preview and content,
19    # so it has to be unique for each result
20    # (Unique identifier for this result, 1-64 Bytes)
21    # you can set your unique id's
22    # but for example i'll generate it based on text because I know, that
23    # only text will be passed in this example
24    text = inline_query.query or 'echo'
25    input_content = InputTextMessageContent(text)
26    result_id: str = hashlib.md5(text.encode()).hexdigest()
27    item = InlineQueryResultArticle(
28        id=result_id,
29        title=f'Result {text!r}',
30        input_message_content=input_content,
31    )
32    # don't forget to set cache_time=1 for testing (default is 300s or 5m)
33    await bot.answer_inline_query(inline_query.id, results=[item], cache_time=1)
34
35
36if __name__ == '__main__':
37    executor.start_polling(dp, skip_updates=True)