Pyrogram-Based Random Quote Handler

  • Share this:

Code introduction


This function uses the Pyrogram library to handle and return random quotes. When a user sends a specific query through Telegram, the function returns a response containing random quotes.


Technology Stack : Pyrogram

Code Type : Python Function

Code Difficulty : Intermediate


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

# Function to handle inline queries
def handle_inline_queries(client: Client, query: raw.types.InputInlineQuery):
    results = []
    # Generate a list of random quotes
    quotes = [
        "The only way to do great work is to love what you do. - Steve Jobs",
        "Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful. - Albert Schweitzer",
        "The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt",
        "The only limit to our realization of tomorrow will be our doubts of today. - Franklin D. Roosevelt"
    ]
    # Create an InlineQueryResultArticle for each quote
    for quote in quotes:
        results.append(
            InlineQueryResultArticle(
                id=quote,
                title="Random Quote",
                input_message_content=InputTextMessageContent(quote)
            )
        )
    # Answer the inline query with the results
    client.answer_inline_query(query.id, results)

# Example usage of the function
# You need to replace 'your_api_id', 'your_api_hash', and 'your_user_name' with your actual Pyrogram credentials
def main():
    app = Client('your_user_name', 'your_api_id', 'your_api_hash')
    app.on_inline_query(filters.query('random_quote'), handle_inline_queries)
    app.run()

if __name__ == '__main__':
    main()                
              
Tags: