gpt4 book ai didi

Telegram Bot : How to get callback_data value?

转载 作者:行者123 更新时间:2023-12-04 23:46:47 24 4
gpt4 key购买 nike

我在 Telegram 中有一个简单的机器人。我的“/select”命令显示两个按钮,每个按钮都有自己的值。因此,如果用户单击按钮,我可以获得文本,但无法获得 callback_data 值。不知道我做错了什么。

这是代码:

$update = json_decode(file_get_contents('php://input'));

$callback_query = $update['callback_query'];

if (isset($callback_query)){
//Fetching callback
$data = $callback_query->data;
$message = $callback_query->message;
$message_id = $callback_query->message->message_id;
$chat_id = $message->chat->id;

switch($data){
case "1":
bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "1"
]);
break;

case "2":
bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "2"
]);
break;
}
}else{

$message = $update->message;
$message_id = $update->message->message_id;
$text = $message->text;
$chat_id = $message->chat->id;

//Statement beginning

switch($text){

case "/select":
$keyboard = array(
'keyboard' => [[['text' => "one", 'callback_data' => "1"]],[['text' => "two", 'callback_data' => "2"]]],
'resize_keyboard' => true,
'one_time_keyboard' => true
);
$markup = json_encode($keyboard, true);

bot('SendMessage',[
'chat_id' => $chat_id,
'reply_markup' => $markup,
'text' => "Choose your option"
]);
break;

default:

bot('SendMessage',[
'chat_id' => $chat_id,
'text' => "This is a test"
]);
}

}

最佳答案

您只需要处理从 Telegram 传入到您的 webhook 的更新,这意味着如果更新类型为 callback_query 并解析数据,则您需要设置条件。

请查看 Telegram 文档,或观看一些 PHP 示例,例如:

How can I differentiate between a 'Message' update and a 'Callback Query' update? (Telegram Bot API)

https://jqueryajaxphp.com/telegram-bot-api-keyboards-callbacks/

祝你好运!保持冷静并发送 Telegram :D

关于 Telegram Bot : How to get callback_data value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com