H
H
HappyMen2019-07-05 01:30:34
PHP
HappyMen, 2019-07-05 01:30:34

How to handle callback via php in Telegram bot Api?

I use this library - https://github.com/TelegramBot/
There is such a button: How to send a message to the user if it is pressed? For example, I want to send "This is a help message" in response to a button click. For example, a specific code, please. Thanks in advance With the help of the function

spoiler
public function on(Closure $event, Closure $checker = null)
    {
        $this->events->add($event, $checker);

        return $this;
    }
Does not work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bkosun, 2019-07-05
@HappyMen

Use the callbackQuery method, something like this:

require_once "vendor/autoload.php";

try {
    $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN');
    // or initialize with botan.io tracker api key
    // $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');
    

    $bot->callbackQuery(function ($callbackQuery) use ($bot) {

    switch ($callbackQuery->getData()){
        case 'help':
            $message = 'Help is here!';
        break;
        default:
            $message = 'Hello!';
    }

    $bot->sendMessage($callbackQuery->getMessage()->getChat()->getId(), $message);

    });
    
    $bot->run();

} catch (\TelegramBot\Api\Exception $e) {
    $e->getMessage();
}

https://github.com/TelegramBot/Api/blob/master/src...
https://github.com/TelegramBot/Api/blob/master/src...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question