telegram-bot-sdk
telegram-bot-sdk copied to clipboard
Keyboard not work (webhook)
Hi, first of all thank you very much for your great work.
I have a problem sending a message with an options keyboard. Selecting an option displays a wait icon. The application does not receive the callback of the selected option.
My settings are as follows:
Laravel: 6.18.8 telegram-bot-skk: 3.1.0
File web.php
Route::post('/bot', function () {
Telegram::commandsHandler(true);
return 'ok';
});
Config file telegram.php
'bot_token' => env('TELEGRAM_BOT_TOKEN', 'YOUR-BOT-TOKEN'),
'async_requests' => env('TELEGRAM_ASYNC_REQUESTS', false),
'http_client_handler' => null,
'commands' => [
Telegram\Bot\Commands\HelpCommand::class,
ResumenDiarioCommand::class,
BuscarCommand::class,
],
File MyCustomCommand.php
public function handle()
{
$update = $this->telegram->getWebhookUpdate();
Log::info('handle');
Log::info($update);
if ($update->isType('callback_query')) {
Log::info('...is callback');
} else {
Log::info('show keyboard...');
$keyboard = Keyboard::make()
->inline()
->row(
Keyboard::inlineButton(['text' => 'DOE', 'callback_data' => 'doe']),
Keyboard::inlineButton(['text' => 'BOE', 'callback_data' => 'boe'])
);
$this->replyWithMessage([
'chat_id' => $this->dataRequest->chat()->id(),
'text' => 'Select',
'reply_markup' => $keyboard
]);
}
}
also, I have configured a connection with Ngrok.
When the buttons are displayed, the following appears ... and nothing happens.
I've reviewed a lot of documentation and issues but can't find a solution. Thank you very much for your time.
A greeting.
What do you mean nothing happens? You see the buttons right? Your code matches exactly with your code. Do you mean pressing a button does not work? Please write in English.
@Xibel thank you for your response and sorry for not expressing myself correctly.
Yes, in the image I have left you can see that the buttons appear well. When I press a button the only thing that apparently happens is that the waiting icon appears.
When I said that it did not work, I meant that by pressing the button I expected to receive a callback response from Telegram but I do not receive anything.
I have checked the configuration, the code, the communication ... I really don't know what else to check.
Thanks again.
Ah, now I understand :)
I have no experience with inline keyboards but occurs to me that there is no command behind your buttons. Nothing is send to your bot via webhook. I think you should change this:
Keyboard::inlineButton(['text' => 'DOE', 'callback_data' => 'doe']), Keyboard::inlineButton(['text' => 'BOE', 'callback_data' => 'boe'])
to
Keyboard::inlineButton(['text' => 'DOE', 'callback_data' => '/doe']),
Keyboard::inlineButton(['text' => 'BOE', 'callback_data' => '/boe'])
Could you try this out?
The result is the same. Nothing seems to be happening. I believe that the content of callback_data is something like the value of the button, to differentiate one from the other.
I also have no experience. I've hardly used Telegram Bot for a couple of days. Keep investigating.
Thank you @Xibel
I will try myself to build a callback thingy this evening. If you define /command, you need a commandhandler that will respond and take action on an incoming command like /doe. In that handler you check if this is a callback. Later more.
Related: https://github.com/irazasyed/telegram-bot-sdk/issues/736
Can You show me the code used for retrieve call back data?
@suadikhasen All my code is in the first post. I have nothing else. In MyCustomCommand there is the keyboard definition and the check if the answer is a call query.
Should I have something else?
Thanks.
The problem is you write your query code customcommand. Custom command run when there is a command otherwise it can When the keyboard is pressed customcommand not executed because keyboard is not command so use another class to handle your query .
Feel free to ask again
@suadikhasen thanks for your answer.
So do I have to create another class to handle the keyboard response? Ok, it makes sense since it is true that pressing the keyboard does not launch any command.
But, when creating another class, how are the keyboard response and the new class related? Is there some kind of special class for reading responses from the keyboard?
I know that there is some concept that is escaping me. Sorry.
Thanks for your reply again.
in your route web.php
Route::post('/bot', function () {
$telegram =Telegram::commandsHandler(true);
if(!$telegram->isType('Command')){
//your new class called in here
}
return 'ok';
});``
commandhandler method return update object if there is no command
Pass true for command handler
Oh, thank you very much @suadikhasen , it finally works.
In my route web.php I have:
$telegram = Telegram::commandsHandler(true);
if( ! $telegram->hasCommand()){
// ... instance and run query class
}
return 'ok';
In my new Query Class I send a message to Telegram and it works correctly.
I just have one last question. Although everything works, the clock icon continues to appear when you click on the keyboard, do you know what it may be?
In the attached image you can see the icon. It is visible for a few seconds even if the reply message has been sent to Telegram.
Thank you very much again, you have helped me a lot.
@mpijierro Where do you get documentation for v 3.0?
Telegram::answerCallbackQuery([
'callback_query_id' => $update->callbackQuery->get('id'),
'text' => __('Any text'),
]);
Do not forget to answer callback query
if anybody else stumbles upon the issue of non-working buttons, please check this answer
https://stackoverflow.com/questions/62876048/telegram-inline-keyboard-button-not-working
basically, revoke your token and try again. pavel durov says no.
How i handle it:
$msg = $telegram->getWebhookUpdate()->getMessage();
if (!$message->getFrom()->isBot) {
// do something with usual messages
} else {
$cb = $telegram->getWebhookUpdate()->getCallbackQuery();
$chatID = $cb->getId();
$command = $cb->getData(); // here we receive 'callback_data' setted in created Menu
switch ($command) {
case .......
default .......
}
$telegram->answerCallbackQuery([
'callback_query_id' => $chatID,
'text' => "CALLBACK RESPONSE",
'show_alert' => true // if you need modal window, not popup info
]);
}
How i handle it:
$msg = $telegram->getWebhookUpdate()->getMessage(); if (!$message->getFrom()->isBot) { // do something with usual messages } else { $cb = $telegram->getWebhookUpdate()->getCallbackQuery(); $chatID = $cb->getId(); $command = $cb->getData(); // here we receive 'callback_data' setted in created Menu switch ($command) { case ....... default ....... } $telegram->answerCallbackQuery([ 'callback_query_id' => $chatID, 'text' => "CALLBACK RESPONSE", 'show_alert' => true // if you need modal window, not popup info ]); }
Where you have put the code? at routes/web.php ???