【问题标题】:Reply keyboard markup php bot telegram回复键盘标记 php bot 电报
【发布时间】:2022-01-14 12:30:20
【问题描述】:

只是一个关于电报 php 机器人的快速问题(该机器人在 heroku 上设置为 webhook)

我目前有以下代码:

<?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);
 

现在机器人以文本形式响应,发送的所有内容都写在“$response”之后

我想知道当用户发送命令 /sub 时我应该更改什么以使机器人发送自定义键盘(replykeyboardmarkup)

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

现在机器人发送这个“Di quale serie vorresti conoscere l'avanzamento?\r\nScrivi il titolo della serie citando questo messaggio。” 当我输入 /sub

如果我想让它发送一个带有 3/4 按钮的键盘,我应该在 $response 之后写什么?

【问题讨论】:

    标签: php telegram php-telegram-bot


    【解决方案1】:

    如果您使用telegram bot libraries 之一,您可能会发现事情更容易

    此示例尚未经过测试,可能无法直接使用,但希望它能让您朝着正确的方向前进。

    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);
    

    【讨论】:

    • 好的,非常感谢!我使用了第一个结构,它工作得很好! ;)
    • 很高兴它成功了。如果它解决了您的问题,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2018-03-31
    • 2017-06-04
    • 1970-01-01
    • 2022-10-05
    • 2018-03-23
    相关资源
    最近更新 更多