InlineKeyboard example
Closed this issue · 8 comments
Hi everyone!
How can I build a command with inline keyboard buttons and callback handlers? I think that the need to be register, but how?
Thanks in advance
@BoShurik thank you. But I'm trying to use it, and bot doesn't give me an answer, if I click the about button...
public function execute(BotApi $api, Update $update)
{
$chatId = $update->getMessage()->getChat()->getId();
if ($update->getCallbackQuery()) {
$chatId = $update->getCallbackQuery()->getMessage()->getChat()->getId();
$slug = $update->getCallbackQuery()->getData();
} else {
$slug = null;
}
$this->showSection($api, $slug, $chatId);
}
public function showSection(BotApi $api, $slug, $chatId, $messageId = null) {
$replyMarkup = new InlineKeyboardMarkup([[['text' => 'About the bot', 'callback_data' => 'about']]]);
switch ($slug) {
case 'about':
$text = 'Text from *About* section';
break;
default:
$text = 'Section error...';
}
if ($messageId) {
$api->editMessageText(
$chatId,
$messageId,
$text,
'markdown',
false,
$replyMarkup
);
} else {
$api->sendMessage(
$chatId,
$text,
'markdown',
false,
null,
$replyMarkup
);
}
}
@MyZik did you overwrite isApplicable method?
By default this method checks only $update->getMessage()
, you need also to check $update->getCallbackQuery()
@MyZik did you overwrite isApplicable method?
By default this method checks only$update->getMessage()
, you need also to check$update->getCallbackQuery()
No, but I updated the code:
private const REGEX_INDEX = '#/start_(\d+)#';
public function execute(BotApi $api, Update $update)
{
$index = isset($update) ? $this->getIndex($update) : 'start';
$messageId = $chatId = null;
if ($update->getCallbackQuery()) {
$chat = $update->getCallbackQuery()->getMessage()->getChat();
$messageId = $update->getCallbackQuery()->getMessage()->getMessageId();
} else {
$chat = $update->getMessage()->getChat();
}
$this->showSection($api, $index, $chat->getId(), $messageId);
}
public function isApplicable(Update $update)
{
if (parent::isApplicable($update)) {
return true;
}
return $this->getIndex($update) !== null;
}
private function getIndex(Update $update): ?int
{
if ($update->getMessage() && preg_match(self::REGEX_INDEX, $update->getMessage()->getText(), $matches)) {
return $matches[1];
}
if ($update->getCallbackQuery() && preg_match(self::REGEX_INDEX, $update->getCallbackQuery()->getData(), $matches)) {
return $matches[1];
}
return null;
}
private function showSection(BotApi $api, $index, $chatId, $messageId = null) {
$replyMarkup = new InlineKeyboardMarkup([[['text' => 'About the bot', 'callback_data' => '/start_about']]]);
switch ($index) {
case 'start':
$text = 'Text from *Start* section';
break;
case 'about':
$text = 'Text from *About* section';
break;
default:
$text = 'Section error...';
}
if ($messageId) {
$api->editMessageText(
$chatId,
$messageId,
$text,
'markdown',
false,
$replyMarkup
);
} else {
$api->sendMessage(
$chatId,
$text,
'markdown',
false,
null,
$replyMarkup
);
}
}
I have only "Section error..." and button doesn't work...
@MyZik you need to change
self::REGEX_INDEX
according to your needs
updated the code...
@MyZik check regex on https://regexr.com/
@MyZik check regex on https://regexr.com/
Thanks, problem is with regex pattern...
It passes to me:
const REGEX_INDEX = '/\/start_(\w+)/';