Pyrogram Inline Query Bot for Word Extraction

  • Share this:

Code introduction


This function creates a Pyrogram-based chatbot that can handle inline queries from users. When a user sends an inline query, the bot will return results for each word in the query.


Technology Stack : Pyrogram

Code Type : Pyrogram Bot

Code Difficulty : Intermediate


                
                    
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent

def create_inline_query_bot(api_id, api_hash, bot_token):
    # Create a new Pyrogram client instance
    app = Client("my_bot", api_id=api_id, api_hash=api_hash, bot_token=bot_token)

    @app.on_inline_query()
    async def inline_query_handler(client, query):
        # Extract the query string from the inline query
        query_string = query.query
        # Create an InlineQueryResultArticle for each word in the query string
        results = [InlineQueryResultArticle(
            id=f"word_{i}",
            title=f"Word {i}",
            input_message_content=InputTextMessageContent(f"Here is the word: {query_string.split()[i]}")
        ) for i in range(len(query_string.split()))]
        # Send the results back to the user
        await query.answer(results)

    # Start the bot
    app.start()

    return app                
              
Tags: