TelegramBot - 将按钮移动到新行
TelegramBot - Move button to new line
如何让按钮排成一列,而不是排成一行。示例图像,因为我需要:
[点击图片][1].
function sendInlineKeyboard($id_chat, $text, $array)
{
$keyboard = array("inline_keyboard" => array($array));
$toSend = array('method' => 'sendMessage', 'chat_id' => $id_chat, 'text' => $text, 'resize_keyboard' => true, 'reply_markup' => $keyboard);
isset($mark) ? $toSend['parse_mode'] = $mark : '';
isset($id_message) ? $toSend['reply_to_message_id'] = $id_message : '';
$ch = curl_init(API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($toSend));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$a = curl_exec($ch);
return json_decode($a, true);
}
$result = $mysqli->query("SELECT * FROM `test`");
while ($row = $result->fetch_assoc()) {
$array[] = array('text' => $row['city'], 'callback_data' => $id);
sendInlineKeyboard($user_id, 'String', $array);
}
不工作。来自0stone0
的回答:
$keyboard = [
'inline_keyboard' => [
[
['text' => 'Button 1', 'callback_data' => 'someString'],
['text' => 'Button 2', 'callback_data' => 'someString', 'text' => 'Button 3', 'callback_data' => 'someString', 'text' => 'Button 4', 'callback_data' => 'someString'],
['text' => 'Button 5', 'callback_data' => 'someString']
]
]
];
$toSend = array('method' => 'sendMessage', 'chat_id' => $id_chat, 'text' => "Test", 'resize_keyboard' => true, 'reply_markup' => $keyboard);
Telegram 将每个数组作为一行读取。
如果您希望在一行中有多个按钮,请将它们添加到一个数组中:
$keyboard = [
"inline_keyboard" => [
[
[ "B~1" ],
[ "B~2" ]
],
[
[ "B~3" ],
]
]
];
由于您的函数获取用作键盘的 array
变量,因此 'mistake' 位于其他地方。
如果您需要任何进一步的帮助,请使用调用 sendInlineKeyboard
函数的代码编辑您的页面。
编辑:
我做了一个关于数组的小例子;
$keyboard = [
"inline_keyboard" => [
[
[
"text" => "B~1",
"callback_data" => "myCallbackData"
],
[
"text" => "B~2",
"callback_data" => "myCallbackData"
]
],
[
[
"text" => "B~3",
"callback_data" => "myCallbackData"
]
]
]
];
$url = "https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<MY-ID>&text=<TEXT>&reply_markup=" . json_encode($keyboard);
编辑 2
您要求 'full' 宽度的按钮。您提供的屏幕截图显示了 2 个按钮,每行 1 个。我上面的示例结果相同,如果您使用正确的客户端将使用按钮的全屏宽度。第一张截图来自我的telegram mac os 客户端,使用旧版本,相同的按钮如下所示:
因此,您使用的客户端只是将按钮显示得很小,这不是您可以管理的。 (使用其他客户端除外)
如何让按钮排成一列,而不是排成一行。示例图像,因为我需要: [点击图片][1].
function sendInlineKeyboard($id_chat, $text, $array)
{
$keyboard = array("inline_keyboard" => array($array));
$toSend = array('method' => 'sendMessage', 'chat_id' => $id_chat, 'text' => $text, 'resize_keyboard' => true, 'reply_markup' => $keyboard);
isset($mark) ? $toSend['parse_mode'] = $mark : '';
isset($id_message) ? $toSend['reply_to_message_id'] = $id_message : '';
$ch = curl_init(API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($toSend));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$a = curl_exec($ch);
return json_decode($a, true);
}
$result = $mysqli->query("SELECT * FROM `test`");
while ($row = $result->fetch_assoc()) {
$array[] = array('text' => $row['city'], 'callback_data' => $id);
sendInlineKeyboard($user_id, 'String', $array);
}
不工作。来自0stone0
的回答:
$keyboard = [
'inline_keyboard' => [
[
['text' => 'Button 1', 'callback_data' => 'someString'],
['text' => 'Button 2', 'callback_data' => 'someString', 'text' => 'Button 3', 'callback_data' => 'someString', 'text' => 'Button 4', 'callback_data' => 'someString'],
['text' => 'Button 5', 'callback_data' => 'someString']
]
]
];
$toSend = array('method' => 'sendMessage', 'chat_id' => $id_chat, 'text' => "Test", 'resize_keyboard' => true, 'reply_markup' => $keyboard);
Telegram 将每个数组作为一行读取。 如果您希望在一行中有多个按钮,请将它们添加到一个数组中:
$keyboard = [
"inline_keyboard" => [
[
[ "B~1" ],
[ "B~2" ]
],
[
[ "B~3" ],
]
]
];
由于您的函数获取用作键盘的 array
变量,因此 'mistake' 位于其他地方。
如果您需要任何进一步的帮助,请使用调用 sendInlineKeyboard
函数的代码编辑您的页面。
编辑: 我做了一个关于数组的小例子;
$keyboard = [
"inline_keyboard" => [
[
[
"text" => "B~1",
"callback_data" => "myCallbackData"
],
[
"text" => "B~2",
"callback_data" => "myCallbackData"
]
],
[
[
"text" => "B~3",
"callback_data" => "myCallbackData"
]
]
]
];
$url = "https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<MY-ID>&text=<TEXT>&reply_markup=" . json_encode($keyboard);
编辑 2
您要求 'full' 宽度的按钮。您提供的屏幕截图显示了 2 个按钮,每行 1 个。我上面的示例结果相同,如果您使用正确的客户端将使用按钮的全屏宽度。第一张截图来自我的telegram mac os 客户端,使用旧版本,相同的按钮如下所示: