marko988
marko988

Reputation: 33

Reply keyboard markup php bot telegram

just a quick question about a telegram php bot (the bot is set as webhook on heroku)

I currently have the following code:

<?php
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if(!$update)
{
  exit;
}
$message = isset($update['message']) ? $update['message'] : "";
$messageId = isset($message['message_id']) ? $message['message_id'] : "";
$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";
$firstname = isset($message['chat']['first_name']) ? $message['chat']['first_name'] : "";
$lastname = isset($message['chat']['last_name']) ? $message['chat']['last_name'] : "";
$username = isset($message['chat']['username']) ? $message['chat']['username'] : "";
$date = isset($message['date']) ? $message['date'] : "";
$text = isset($message['text']) ? $message['text'] : "";
$text = trim($text);
$text = strtolower($text);
header("Content-Type: application/json");
$response = '';
if(strpos($text, "/start") === 0 || $text=="ciao") {
    $response = "Ciao $firstname, sono il bot del gruppo A7A. Scrivi /serie per sapere quali serie traduciamo al momento, o /sub per conoscere l'avanzamento delle traduzioni. Per contattarci, usa /scrivici";
}
elseif($text=="/serie" || $text=="/serie@a7asubtitlesbot") {
    $response = "Al momento ci occupiamo delle seguenti serie ߓ:\r\nThe Flash s06 (solo crossover),\n\rArrow S08 (solo Crossover),\n\rLegends of Tomorrow s05 (solo crossover),\nWatchmen,\r\nThe 100,\nVeronica Mars s04,\nVictoria,\r\nThe Alienist,\r\nA Teacher,\r\nBig Sky,\r\nCall Your Mother,\r\nYounger s07,\r\nRiverdale,\r\nFather Brown s08,\r\nGossip Girl 2021,\r\nStargirl (DC),\r\nProfessor T,\r\nThe North Water,\r\nOne of Us Is Lying,\r\nBrassic,\r\nDexter New Blood,\r\nWheel of Time";
}
elseif($text=="/sub" || $text=="/sub@a7asubtitlesbot") {
    $response = "Di quale serie vorresti conoscere l'avanzamento?\r\nScrivi il titolo della serie citando questo messaggio.";
} else {
    $response = "";
}
$parameters = array('chat_id' => $chatId, "text" => $response);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
 

now the bot responds as text, sending everything is written after "$response"

I was wondering what should I change to make the bot send a custom keyboard (replykeyboardmarkup) when the user send the command /sub

elseif($text=="/sub" || $text=="/sub@a7asubtitlesbot")
{
    $response = "Di quale serie vorresti conoscere l'avanzamento?\r\nScrivi il titolo della serie citando questo messaggio.";
}

Now the bot sends this "Di quale serie vorresti conoscere l'avanzamento?\r\nScrivi il titolo della serie citando questo messaggio." when I type /sub

If I want make it send a keyboard with 3/4 buttons what should I write after $response ?

Upvotes: 0

Views: 3626

Answers (1)

user1191247
user1191247

Reputation: 12998

You will probably find things easier if you use one of the telegram bot libraries

This example has not been tested and may not work straight out of the box but hopefully it will get you moving in the right direction.

elseif($text=='/sub' || $text=='/sub@a7asubtitlesbot') {
    $response = "Di quale serie vorresti conoscere l'avanzamento?\r\nScrivi il titolo della serie citando questo messaggio.";

    // I am not quite sure on the nesting structure for the arrays of buttons
    $keyboard = [[['text' => 'button 1'], ['text' => 'button 2'], ['text' => 'button 3']]];

    // alternative nesting to try
    // $keyboard = [[['text' => 'button 1']], [['text' => 'button 2']], [['text' => 'button 3']]];

} else {
    $response = '';
}

$parameters = array('chat_id' => $chatId, 'text' => $response);

if (is_array($keyboard)) {
    $parameters['reply_markup'] = ['keyboard' => $keyboard, 'one_time_keyboard' => true];
}

$parameters['method'] = 'sendMessage';
echo json_encode($parameters);

Upvotes: 1

Related Questions