How to create custom keyboard markup in PHP Telegram bot

I’m working on building a Telegram bot using PHP and I’m having trouble getting the custom keyboard to work properly with reply_markup. The keyboard buttons are not showing up when I send messages. I think there might be an issue with how I’m formatting the JSON or making the API call. Can someone point out what I’m doing wrong?

file_get_contents($botURL."/sendMessage?chat_id=".$userID."&text=ButtonDemo&reply_markup={\"keyboard\":[[\"click_me\"]]}")

yeah, ur JSON encoding might be off. i had that issue too, telegram’s kinda strict with formats. also, don’t forget to include resize_keyboard=true, makes a difference for mobile views!

Your code’s missing the one_time_keyboard parameter and the JSON structure isn’t encoded right. I hit this exact issue when I started with Telegram bots. You need to structure the keyboard array properly and use json_encode() instead of hardcoding the JSON. Try this:

$keyboard = array(
    'keyboard' => array(
        array('Click Me', 'Another Button')
    ),
    'one_time_keyboard' => true,
    'resize_keyboard' => true
);

$reply_markup = json_encode($keyboard);
$url = $botURL."/sendMessage?chat_id=".$userID."&text=ButtonDemo&reply_markup=".urlencode($reply_markup);
file_get_contents($url);

Don’t forget urlencode() when passing JSON as a URL parameter. Fixed my buttons not showing up.

You’re manually building the JSON string - that’s what’s causing your encoding issues. I ran into the same headache when I first started with Telegram’s API. Build your keyboard as a PHP array first, then encode it properly.

$keyboard = [
    'keyboard' => [['click_me']],
    'resize_keyboard' => true
];

$params = [
    'chat_id' => $userID,
    'text' => 'ButtonDemo',
    'reply_markup' => json_encode($keyboard)
];

$url = $botURL . '/sendMessage?' . http_build_query($params);
file_get_contents($url);

http_build_query() handles URL encoding for you and stops all those manual query string mistakes. Fixed my keyboard problems completely.