-2

I have written this function for my telegram bot in PHP but it doesn't work:

if($text == "t"){
    $keyboard = array(
        "inline_keyboard" => array(
            array(
                array(
                    "text" => "My Button Text", 
                    "callback_data" => "myCallbackData"
                )
            )
        )
    );
    $key = array(
        "keyboard" => $keyboard,
    );
    keyboard($chatId, "asadsd", $key);
}
function keyboard($chatID, $text, $t)
{
    $t2 = $t;
    $t3 = json_encode($t2);

    api("sendMessage?chat_id=$chatId&text=$text&parse_mode=HTML&reply_markup=$t3");        
}

It should work but it is not doing so.

How can I fix this? (me and my bot)

halfer
  • 19,824
  • 17
  • 99
  • 186
Ace_69
  • 13
  • 1
  • 2
  • In what way is it not working? – Jason Aller Feb 03 '19 at 19:29
  • Pro-tip: titles are best phrased as a plain English summary of your problem, or a short question that summarises your problem. "Telegram bot keyboard PHP" is a collection of themes/tags, but is not very descriptive about the issue itself. – halfer May 19 '19 at 19:07
  • Ace, please always respond to requests for clarification here. @Jason's message seems to have gone unreplied to. – halfer May 19 '19 at 19:09

1 Answers1

1

Your code isn't working because you are trying to put the inline_keyboard array into a classic keyboard array. So:

  • Remove $key = array(...); and when you're calling the keyboard function put $keyboard instead of $key .
  • Put $chatID instead of $chatId in the keyboard function because php variable names are case-sensitive.
  • Remove the $t2 and $t3 since they are useless.
  • I suggest putting an arguments variable and using http_build_query to build the args, so it will automatically urlencode and build the vars.

Your code should result like this:

if($text == "t"){
    $keyboard = array(
        "inline_keyboard" => array(
            array(
                array(
                    "text" => "My Button Text", 
                    "callback_data" => "myCallbackData"
                )
            )
        )
    );

    keyboard($chatId, "asadsd", $keyboard);
}
function keyboard($chatID, $text, $t)
{
    $args = array(
        "chat_id" => $chatID,
        "text" => $text,
        "parse_mode" => "HTML",
        "reply_markup" => json_encode($t),
    );
    api("sendMessage?".http_build_query($args));        
}

Assuming everything is set, the code should work fine. Next time, please, be more specific

Pato05
  • 326
  • 1
  • 14