Closed massadm closed 3 years ago
Can you share the code where you're trying to execute getChat
please?
It's possible that you're trying to access the wrong object in your response.
<?php
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\InlineKeyboard;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle;
use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent;
use Longman\TelegramBot\Entities\ServerResponse;
class InlinequeryCommand extends SystemCommand
{
protected $name = 'inlinequery';
protected $description = 'Handle inline query';
protected $version = '1.2.0';
public function execute(): ServerResponse
{
$inline_query = $this->getInlineQuery();
return $inline_query->answer([
new InlineQueryResultArticle([
'id' => '001',
'title' => 'Title',
'description' => 'Description',
'input_message_content' => new InputTextMessageContent([
'message_text' => 'Message',
]),
'reply_markup' => new InlineKeyboard([
['text' => 'Button', 'callback_data' => 'data'],
]),
]),
]);
}
}
Just click button "Button".
Right, then the error must be in your CallbackqueryCommand.php
, not InlinequeryCommand.php
.
Can you share your CallbackqueryCommand.php
file and also a more detailed stack trace of the error please?
Tested with sample example from here example-bot:CallbackqueryCommand
In this case callback_query
has no chat
property in result
as it has with message
e.g.
{
"ok": true,
"result": [
{
"update_id": ...,
"message": {
"message_id": ...,
"from": {
"id": ...,
...
},
"chat": {
"id": ...,
...
},
"date": ...,
"text": "...",
"entities": [
...
]
}
}
]
}
vs
{
"ok": true,
"result": [
{
"update_id": ...,
"callback_query": {
"id": "...",
"from": {
"id": ...,
...
},
"inline_message_id": "...",
"chat_instance": "...",
"data": "..."
}
}
]
}
Temporary fixed with help of \TelegramBot\TelegramBotManager\BotManager::setCustomGetUpdatesCallback like this:
} elseif ($update_content instanceof CallbackQuery) {
/** @var CallbackQuery $update_content */
$message = $update_content->getMessage();
$chat_id = ($message!==null && $message->getChat()!==null) ? $message->getChat()->getId() : null;
Stack trace:
#0 ./vendor/php-telegram-bot/telegram-bot-manager/src/BotManager.php(457): TelegramBot\TelegramBotManager\BotManager->defaultGetUpdatesCallback()
#1 ./vendor/php-telegram-bot/telegram-bot-manager/src/BotManager.php(421): TelegramBot\TelegramBotManager\BotManager->handleGetUpdates()
#2 ./vendor/php-telegram-bot/telegram-bot-manager/src/BotManager.php(342): TelegramBot\TelegramBotManager\BotManager->handleGetUpdatesLoop()
#3 ./vendor/php-telegram-bot/telegram-bot-manager/src/BotManager.php(152): TelegramBot\TelegramBotManager\BotManager->handleRequest()
#4 ./manager.php(53): TelegramBot\TelegramBotManager\BotManager->run()
#5 {main}
Ah! I see now what's going on, the response of an inline button is different when it comes through an inline query. Thanks for pointing that out 👍
Would you like to make a PR to fix this? 😃
Bug Report
RAW update
Summary
Fatal error: Call to a member function getChat() on null in BotManager.php:505
How to reproduce
answer() InlineQuery with InlineQueryResult*[] containing input_message_content and reply_markup with InlineKeyboard with callback_data-button, then choose that item from result then click inline button to get CallbackQuery above.
Tested in PM/Saved Messages chat.