php-telegram-bot / laravel

Laravel package for PHP Telegram Bot Library
Other
171 stars 51 forks source link

How can i handle requests from Callback buttons in command? #2

Closed igarbera closed 5 years ago

igarbera commented 6 years ago

Hello!

I try to process query from callback button :

1) I have webhook handler, it's finely process commands like /start. : public function webhookHandler(PhpTelegramBotContract $telegram_bot){ $telegram_bot->handle(); }

Here is code for process /start command and display the button with a callback, its work fine:

`<?php

namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand; use Longman\TelegramBot\Request; use Longman\TelegramBot\Entities\InlineKeyboard;

class StartCommand extends SystemCommand {

protected $name = 'start';

protected $description = 'Start command';

protected $usage = '/start';

protected $version = '1.1.0';

protected $private_only = true;

public function execute()
{
    $message = $this->getMessage();

    $chat_id = $message->getChat()->getId();
    $text    = 'Start command with callback button';

    $inline_keyboard = new InlineKeyboard([
        ['text' => 'Test callback', 'callback_data' => 'test_callback'],
    ]);

    $data = [
        'chat_id' => $chat_id,
        'text'    => $text,
        'reply_markup' => $inline_keyboard,
    ];

    return Request::sendMessage($data);
}

}`

3) when I send query from inline button, its not processing in command like this (as in https://github.com/php-telegram-bot/example-bot/blob/master/Commands/CallbackqueryCommand.php):

`<?php

namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand; use Longman\TelegramBot\Request;

class TestCallbackCommand extends SystemCommand {

protected $name = 'callbackquery';

protected $description = 'Reply to callback query';

protected $version = '1.1.1';

public function execute()
{
    $callback_query    = $this->getCallbackQuery();
    $callback_query_id = $callback_query->getId();
    $callback_data     = $callback_query->getData();

    $data = [
        'callback_query_id' => $callback_query_id,
        'text'              => 'Hello World!',
        'show_alert'        => $callback_data === 'thumb up',
        'cache_time'        => 5,
    ];

    return Request::answerCallbackQuery($data);
}

}`

So, how can i process queries from callback buttons in commands in Laravel? maybe you have some working examples?

akalongman commented 5 years ago

@igarbera try to log income requests and see what Telegram server is sending