Core: Get callback query data in current conversation

Created on 19 Jul 2016  Â·  7Comments  Â·  Source: php-telegram-bot/core

Hi,
I want to get selected InlineKeyboardButton data with current conversation.
Web someone clicked on the inline keyboard data sent to CallbackqueryCommand and in return our selected button.
But how can i get this data without need to send callbackquery or pass this data to current conversation?
email-21
email-222

Most helpful comment

InlinekeyboardCommand.php + CallbackqueryCommand.php

Execute /inlinekeyboard, click the callback button, see what happens then look into CallbackqueryCommand.

All 7 comments

You will have to redirect it to your command via callbackquerycommand.php, then in your own command also add support for incoming callback queries and handle it there.

Simple redirect idea is to use this data scheme in callback queries: "command_userdata", for example: '[email protected]' (whole data cant exceed 64 bytes, and shouldnt contain any special characters)

Can you give me a sample code?

On Tuesday, July 19, 2016, jacklul [email protected] wrote:

You will have to redirect it to your command via callbackquerycommand.php,
then in your own command also add support for incoming callback queries and
handle it there.

Simple redirect idea is to use this data scheme in callback queries:
"command_userdata", for example: '[email protected]
cant exceed 64 bytes, and shouldnt contain any special characters)

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/akalongman/php-telegram-bot/issues/256#issuecomment-233664050,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AEM1FEWHXQso68OH3kTSSYGSQ8r2dzgoks5qXOj8gaJpZM4JPn9K
.

This is my clean approach, having private $commands 'table' to assign commands to execute to appropriate contents of callback data.

CallbackqueryCommand.php

class CallbackqueryCommand extends SystemCommand
{
    protected $name = 'callbackquery';
    protected $description = 'Reply to callback query';

    // data before _ in callback query data => actual command
    private $commands = [
        'help' => 'help',
        'reply' => 'contact',
    ];

    public function execute()
    {
        $update = $this->getUpdate();
        $callback_query = $update->getCallbackQuery();
        $user_id = $callback_query->getFrom()->getId();
        $data = $callback_query->getData();

        $command = explode('_', $data);
        $command = $command[0];

        if (isset($this->commands[$command]) && $this->getTelegram()->getCommandObject($this->commands[$command])) {
            return $this->getTelegram()->executeCommand($this->commands[$command], $update);
        } else {
            $data = [];
            $data['callback_query_id'] = $callback_query->getId();
            $data['text'] = 'Invalid request!';
            $data['show_alert'] = true;
        }

        return Request::answerCallbackQuery($data);
    }
}

Stripped version of my own code but it should work.

For in-command usage example see https://github.com/akalongman/php-telegram-bot/issues/182#issuecomment-218896824.

I edited my CallbackqueryCommand.php with @jacklul example, but how can I execute this command?
/callbackquery help_test no response
/callbackquery help_reply no response
/callbackquery help _ 111 no response

I'm missing something about callback feature I think :

Executing /callbackquery won't trigger it as code expects callback query object, not message. It must be used with Inline buttons with callback data.

Trying to understand callbacks but can't get it working, hope I can find some example, thanks

InlinekeyboardCommand.php + CallbackqueryCommand.php

Execute /inlinekeyboard, click the callback button, see what happens then look into CallbackqueryCommand.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smaznet picture smaznet  Â·  4Comments

vansanblch picture vansanblch  Â·  3Comments

Zoha picture Zoha  Â·  3Comments

noplanman picture noplanman  Â·  3Comments

marcolino7 picture marcolino7  Â·  3Comments