php-telegram-bot / telegram-bot-manager

PHP Telegram Bot Manager
MIT License
209 stars 66 forks source link

Fatal error: Call to a member function getChat() on null in BotManager.php:505 #63

Closed massadm closed 3 years ago

massadm commented 3 years ago

Bug Report

? !
Operating system Linux 5.10.15-1-MANJARO
PHP Telegram Bot Manager version 1.6.0
PHP Telegram Bot version 0.70.1
PHP version 8.0.1
Update Method getUpdates

RAW update

Longman\TelegramBot\Entities\CallbackQuery::__set_state(array(
  ...
   'raw_data' => 
  array (
    'id' => '...',
    'from' => 
    array (
      'id' => ...,
      'is_bot' => false,
      'first_name' => '...',
      'username' => '...',
      'language_code' => '...',
    ),
    'inline_message_id' => '...',
    'chat_instance' => '...',
    'data' => '...',
  ),
   'bot_username' => '...',
))

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.

noplanman commented 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.

massadm commented 3 years ago
<?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".

noplanman commented 3 years ago

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?

massadm commented 3 years ago

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;
massadm commented 3 years ago
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}
noplanman commented 3 years ago

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? 😃