telegram-bot-sdk icon indicating copy to clipboard operation
telegram-bot-sdk copied to clipboard

Keyboard not work (webhook)

Open mpijierro opened this issue 4 years ago • 19 comments

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. Captura de pantalla de 2020-04-20 14-46-58

I've reviewed a lot of documentation and issues but can't find a solution. Thank you very much for your time.

A greeting.

mpijierro avatar Apr 20 '20 12:04 mpijierro

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 avatar Apr 20 '20 13:04 Xibel

@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.

mpijierro avatar Apr 20 '20 13:04 mpijierro

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?

Xibel avatar Apr 20 '20 13:04 Xibel

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

mpijierro avatar Apr 20 '20 14:04 mpijierro

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.

Xibel avatar Apr 20 '20 15:04 Xibel

Related: https://github.com/irazasyed/telegram-bot-sdk/issues/736

assada avatar Apr 21 '20 19:04 assada

Can You show me the code used for retrieve call back data?

suadikhasen avatar Apr 27 '20 03:04 suadikhasen

@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.

mpijierro avatar Apr 27 '20 07:04 mpijierro

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 avatar Apr 28 '20 11:04 suadikhasen

@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.

mpijierro avatar Apr 28 '20 11:04 mpijierro

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';
});``

suadikhasen avatar Apr 29 '20 01:04 suadikhasen

commandhandler method return update object if there is no command

suadikhasen avatar Apr 29 '20 01:04 suadikhasen

Pass true for command handler

suadikhasen avatar Apr 29 '20 01:04 suadikhasen

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?

Captura de pantalla de 2020-04-29 10-29-17

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 avatar Apr 29 '20 08:04 mpijierro

@mpijierro Where do you get documentation for v 3.0?

Itsmaqsud avatar May 05 '20 09:05 Itsmaqsud

            Telegram::answerCallbackQuery([
                'callback_query_id' => $update->callbackQuery->get('id'),
                'text'  =>  __('Any text'),
            ]);

Do not forget to answer callback query

marechenok avatar Sep 01 '20 11:09 marechenok

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. image

4r7d3c0 avatar Oct 16 '20 23:10 4r7d3c0

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
    ]);
}

DenisChernov avatar Jan 13 '21 14:01 DenisChernov

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 ???

shopnaill avatar Jun 24 '21 23:06 shopnaill