Pyrogram InlineKeyboardButton + Callback Handling

    0

    0

    Ali Abdi

    Pyrogram InlineKeyboardMarkup - InlineKeyboardButton with Callback Query Handling

    from pyrogram import Client, filters
    from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
    
    # BotApp
    app = Client(
      name="SESSION_NAME",
      bot_token="BOT_TOKEN",
      # ACCOUNT_NAME
      api_id=API_ID,
      api_hash="API_HASH",
      device_model="TITLE_ON_DEVICES_SECTION",
      app_version="TEXT_ON_DEVICES_SECTION"
    )
    
    @app.on_message(filters.command(["start"]))
    async def on_start_command(c, m):
      
      # Get current chat ID (bot's chat)
      CurrentChatID = m.chat.id
      
      # Send language selection buttons
      SetLang = await app.send_message(
        chat_id=CurrentChatID,
        text="Select your language:",
    
        reply_markup=InlineKeyboardMarkup(
          [
            [
              InlineKeyboardButton(
                text="🇬🇧 English",
                callback_data="EN"
              ),
    
              InlineKeyboardButton(
                text="🇮🇷 فارسی",
                callback_data="FA"
              )
            ]
          ]
        )
      )
    
      # Activate callback of language selection buttons
      # Works with callback_data text as regex (EN or FA)
      @app.on_callback_query(filters.regex(pattern=(r"EN|FA")))
      async def on_language_inlinekeyboard(bot, callbackdata):
        # Remove language selection message
        await SetLang.delete()
      
        # Send selection result (callbackdata) to the user
        await app.send_message(
          chat_id=CurrentChatID,
          text=str(callbackdata.data)
        )
    
    app.run()
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.