Call to a member function getChat() on null in php-telegram-bot laravel package

  laravel, php, telegram, telegram-bot

I’m using php-telegram-bot package to create a telegram Bot.

I have a conversation command based on SurveyCommand.php example.

all things worked fine but I have a problem after ending conversation.

In the last step of conversation after using $this->conversation->stop(); I want to send a simple message and then call a start command.

like this :

case  5:

                $this->conversation->update();

                unset($notes['state']);
                $out_text = '/Survey result:';

                foreach ($notes as $k => $v) {
                    $out_text .= PHP_EOL . ucfirst($k) . ': ' . $v;
                }

                $range_string = explode('*', $notes['range_string']);

                $notes['from'] = $range_string[0];
                $notes['end'] = $range_string[1];

                unset($notes['range_string']);

                $products = ProductController::productList($notes);

                if ($products->count()) {
                    $inlineKeyboard = new InlineKeyboard([]);
                    $inlineKeyboard->addRow(new InlineKeyboardButton([
                        'text' => 'Founded Products',
                        'switch_inline_query_current_chat' => implode('-', $notes)
                    ]));

                    $data = [
                        'text' => $products->count() . '  Item found. Touch the button below to view.',
                        'chat_id' => $chat_id,
                        'reply_markup' => $inlineKeyboard
                    ];

                    $this->conversation->stop();

                    return Request::sendMessage($data);
                }

                $this->conversation->stop();

                $data = [
                    'text' => 'Not Found Any Product',
                    'chat_id' => $chat_id
                ];

                Request::sendMessage($data);

                 $this->telegram->executeCommand('StartWithoutWelcome');

                break;
        }


        return $result;

    }
}

And StartWithoutWelcomeCommand is like this :

namespace LongmanTelegramBotCommandsSystemCommands;

use IlluminateSupportFacadesLog;
use LongmanTelegramBotCommandsSystemCommand;
use LongmanTelegramBotEntitiesKeyboard;
use LongmanTelegramBotRequest;

/**
 * Start command
 *
 * Gets executed when a user first starts using the bot.
 */
class StartWithoutWelcomeCommand extends SystemCommand
{
    protected $name = 'StartWithoutWelcome';                      // Your command's name
    protected $description = 'A command for test'; // Your command description
    protected $usage = '/StartWithoutWelcome';                    // Usage of your command
    protected $version = '1.0.0';                  // Version of your command

    public function execute()
    {
        $keyboard = new Keyboard(
            ['? کاتالوگ محصولات ?'],
            ['? شعب ️', '?️ میخواهم انتخاب کنم'],
            ['‍?‍? پشتیبانی', '? تخفیف ها',],
            ['? حساب کاربری من']
        );

        $keyboard
            ->setOneTimeKeyboard(true)
            ->setResizeKeyboard(true);


        $chat_id = $this->getMessage()->getChat()->getId();

        $data = [
            'chat_id' => $chat_id,
            'text' => 'Select one option : ',
            'reply_markup' => $keyboard
        ];

        return Request::sendMessage($data);

    }
}

But whenever I run the conversation , when calling StartWithoutWelcome command I got bellow errror :

Call to a member function getChat() on null {"exception":"[object] (Error(code: 0): Call to a member function getChat() on null at /home/easrp/bot_files/app/Commands/StartWithoutWelcomeCommand.php:43)

what is problem and have can I solve it

Source: Laravel

Leave a Reply