Search code examples
javascriptphpajaxtelegramtelegram-bot

inline keyboard button make action change user url using php?


i have a php code for telegram bot inline keyboard i want when i press one of the two buttons i want to redirect the user to another page, look at my code is working but i dont know how to do this action on the inline keyboard

if ($telegram == "on"){
        $keyboard = json_encode([
            "inline_keyboard" => [
                [
                    [
                        "text" => "✅ ADMINPAGE ✅",
                        "callback_data" => "callbackone"
                    ],

                    [
                        "text" => "❌ HOMEPAGE ❌",
                        "callback_data" => "callbacktwo"
                    ],

                ]
            ]
        ]);
        $parameters = array(
            "chat_id" => $chat_id,
            "text" => $txt,
            'reply_markup' => $keyboard

        );
    
    
        $send = ($parameters);
        $website_telegram = "https://api.telegram.org/bot{$bot_url}";
        $ch = curl_init($website_telegram . '/sendMessage');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ($send));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close($ch);
    }

Solution

  • The InlineKeyboardButton accepts an url parameter:

    url

    Optional. HTTP or tg:// URL to be opened when the button is pressed.
    Links tg://user?id=<user_id> can be used to mention a user by their ID without using a username, if this is allowed by their privacy settings.


    So you'll need something like:

    $keyboard = json_encode([
        "inline_keyboard" => [
            [
                [
                    "text" => "✅ ADMINPAGE ✅",
                    "url" => "https://example.com/admin"
                ],
    
                [
                    "text" => "❌ HOMEPAGE ❌",
                    "url" => "https://example.commin"
                ]
            ]
        ]
    ]);