inline keyboard on telegram error 400 bad request

Issue

I wanna send a json encoded keyboard as reply markup but I get error 400 bad requset!

This is how json looks like:

{"inline_keyboard":[[{"text":"Play, Guitar, Play - \u003Cspan style=\"font-size:14px;\"\u003E Conway Twitty ","callback_data":"some url"}]]}

and this is how the code looks like:

 // Create keyboard
    $keyboard = json_encode([
        "inline_keyboard" => [
            $innerdata,
    ]], true);

    $url = "https://api.telegram.org/bot$token/sendMessage?chat_id=$chat_id&text=Choose&reply_markup=".$keyboard;
    $res = file_get_contents($url); 
        break;

I’ve also tried deleting one of the [ ] but it didn’t work.

Solution

I actually find the problem. It was a problem with data size. As Telegram says, the callback data should not be more than 64 bytes

And it can be fixed by:

if(mb_strlen($callback_data) < 64){
//the rest of the code
}

Answered By – Shayan.Jpr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published