TelegramBot / Api

Native PHP Wrapper for Telegram BOT API
MIT License
1.06k stars 324 forks source link

How to use answercallbackquery? #403

Open bilibvivo opened 1 year ago

bilibvivo commented 1 year ago
//Handle text messages
$bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot, $log) {
  $message = $update->getMessage();
  $id = $message->getChat()->getId();
  $callback_query = $update->getCallbackQuery();

  $log->info('web hook', ['message'=>$message->getText(), 'callback'=>$callback_query]);
  if ($callback_query) {
      $callback_query_data = $callback_query->getData();
      $bot->sendDocument($id, $document, $callback_query_data);
      return;
  } elseif (!stristr($message->getText(), '/s')) {
      return;
  }

  //handle text
   ....
}

When I click the inline button, it doesn't work. I think CallbackQuery is not in this event listener, how should I listen to it and respond to it?

Gramotun commented 8 months ago

Use this event:

$bot->callbackQuery(function (\TelegramBot\Api\Types\CallbackQuery $callback) use ($bot) {
      $message = $callback->getMessage();
      $chat_id = $message->getChat()->getId();
      $text = $message->getText();
      $callback_data = $callback->getData();
      ...
});