我已经用PHP为我的电报机器人编写了此功能,但是它不起作用:
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");
}
它应该工作,但是不行。
答案 0 :(得分:1)
您的代码无法正常工作,因为您试图将inline_keyboard数组放入传统的键盘数组中。所以:
$key = array(...);
,然后在调用键盘功能时放$keyboard
而不是$key
。$chatID
而不是$chatId
,因为php变量名称区分大小写。$t2
和$t3
,因为它们没有用。http_build_query
来构建args,这样它将自动urlencode并构建vars。您的代码应如下所示:
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));
}
假设一切都设置好了,代码应该可以正常工作。下次,请更具体