How to create inline buttons in PHP Bot Telegram
First of all you don't need to use /setinline
command in botFather. this command is for "inline mode" while you are using an inline_keyboard
which is a custom keyboard in normal chat mode.
also you need to provide a callback_data
in your keyboard array for each button:
$keyboard = [
'inline_keyboard' => [
[
['text' => 'forward me to groups', 'callback_data' => 'someString']
]
]
];
$encodedKeyboard = json_encode($keyboard);
$parameters =
array(
'chat_id' => $chatId,
'text' => $response,
'reply_markup' => $encodedKeyboard
);
send('sendMessage', $parameters); // function description Below
At last you need to send it via curl. here is a function i use in my codes:
function send($method, $data)
{
$url = "https://api.telegram.org/bot<Bot-Token>". "/" . $method;
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
return $output;
}
P.S. I personally use PhpStorm, its nice ;)
I have managed some function with https://github.com/irazasyed/telegram-bot-sdk if you don't want to manage row on inline keyword you can skip foreach and use $inline_keyboard[] = $keyword_data; to show keyword
check Image for $keyword_data array
public function inlineKeyword($chat_id, $keyword_data, $msg = '') {
if (empty($msg)) {
$msg = "Select";
}
$inline_keyboard = array();
$row = 0;
$prev_value = '';
foreach ($keyword_data as $value) {
if (isset($prev_value['text']) && strlen($prev_value['text']) < 10 && strlen($value['text']) < 10) {
$inline_keyboard[$row - 1][] = $value;
} else {
$inline_keyboard[$row][] = $value;
}
$prev_value = $value;
$row++;
}
// $inline_keyboard[] = $keyword_data;
$reply_markup = $this->telegram->replyKeyboardMarkup([
'inline_keyboard' => $inline_keyboard,
'resize_keyboard' => true
]);
$response = $this->telegram->sendMessage([
'text' => $msg,
'reply_markup' => $reply_markup,
'chat_id' => $chat_id
]);
}