php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.79k stars 949 forks source link

CallbackqueryCommand -> executeCommand #1444

Open VAtapin opened 4 months ago

VAtapin commented 4 months ago

| Operating system | Ubuntu 20.04.6 LTS | PHP Telegram Bot version | 0.81 | PHP version | 8.1.27 | MySQL version | x none | Update Method | Webhook | Self-signed certificate | no

On several occasions, I dedicated my evenings to solving a seemingly simple problem, only to discover a multitude of individuals facing the same challenge as myself. It became evident that this issue is systemic and demands clarification.

In the realm of inline keyboards, I diligently specified the callback_data argument, following the example provided by the bot. I received an alert as expected, accompanied by gratitude for the exemplary illustration. However, I encountered a stumbling block – a crucial piece of information was elusive. I couldn't find any guidance on how to execute a command, such as $this->getTelegram()->executeCommand('command'), when a button is pressed. This seems like a natural progression in the process. Why go through the trouble of implementing CallbackqueryCommand if this essential aspect is not addressed?

Regrettably, the example provided appears to be incomplete or, perhaps, insufficiently informative. Have I overlooked a crucial detail in the solution? Your guidance on this matter would be immensely appreciated. Thank you in advance for any hints you can provide.

araikphp commented 3 months ago

I completely agree with your perspective. It's quite logical to expect that if you're adding a keyboard to a command, the callback triggered by that command's button should intuitively direct back to the same command. The lack of clear documentation or guidance on executing a command like $this->getTelegram()->executeCommand('command') upon pressing a button is indeed a significant oversight. It's a step that naturally extends the functionality of inline keyboards and should be addressed in the documentation or examples provided. Your effort to shed light on this issue is valuable, and hopefully, it will prompt an enhancement in the resources available to developers.

Hitmare commented 3 months ago

The way Telegram sends interactions with InlineButtons is through Callbackqueries.

To start a command or any function that is not implemented by Telegram like URL, Web App, Login URL and such is done by sending a callback_data InlineKeyboard API Funciton

The callback_data is send back to the bot and can be handled by the CallbackQueryCommand.php Command file, found in the example-bot

there you can access the callback_data and use functions like switch or if to check if the callback_data contains a certain value to then trigger the $this->getTelegram()->executeCommand('command') function