在...vendor/longman/telegram-bot/src/Request.php 中为 null 时调用成员函数 getBotUsername()

Call to a member function getBotUsername() on null in ...vendor/longman/telegram-bot/src/Request.php

我正在尝试使用 Telegram Bot (longman/telegram-bot) 和 Laravel Lumen 作业发送 Telegram 消息。

所以,首先我要使用 composer require longman/telegram-bot 安装 Telegram 机器人并添加到 composer.json "Longman\TelegramBot\": "vendor/longman/telegram-bot/src"。然后创造一个工作

<?php

namespace App\Jobs;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Exception\TelegramException;

class ProcessTelegramMessageJob extends Job
{
    const TELEGRAM_API_KEY = 'BOTKEY';
    const TELEGRAM_BOT_NAME = 'BOTNAME';

    protected $telegram;
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;

        try {

            $this->telegram = new Telegram(self::TELEGRAM_API_KEY, self::TELEGRAM_BOT_NAME);
            $this->telegram->handle();

        } catch (TelegramException $e) {}
    }

    public function handle()
    {
        $data = [

            'chat_id' => $this->data['user_id'],
            'text' => $this->data['message'],
        ];

        $result = Request::sendMessage($data);
    }
}

但是,当作业尝试调度时,我在日志中看到 Call to a member function getBotUsername() on null in ...vendor/longman/telegram-bot/src/Request.php。据我了解 Request 是看不到 Telegram 实例的。

如何解决这个错误? 提前致谢!

PS 电报对象转储

O:28:"Longman\TelegramBot\Telegram":16:{s:10:"*version";s:6:"0.52.0";s:10:"*api_key";s:45:"Api key here";s:15:"*bot_username";s:13:"Bot name here";s:9:"*bot_id";s:9:"470649282";s:8:"*input";s:116:"{"id":1,"url":"cib","_pos":1,"is_vis":true,"deleted_at":null,"checked":false,"data":{"id":1,"_node":1,"head":"CIB"}}";s:17:"*commands_paths";a:1:{i:0;s:98:"/var/www/www-root/data/www/domain.com/vendor/longman/telegram-bot/src/Commands/SystemCommands";}s:9:"*update";O:35:"Longman\TelegramBot\Entities\Update":9:{s:2:"id";i:1;s:3:"url";s:3:"cib";s:4:"_pos";i:1;s:6:"is_vis";b:1;s:10:"deleted_at";N;s:7:"checked";b:0;s:4:"data";a:3:{s:2:"id";i:1;s:5:"_node";i:1;s:4:"head";s:3:"CIB";}s:8:"raw_data";a:7:{s:2:"id";i:1;s:3:"url";s:3:"cib";s:4:"_pos";i:1;s:6:"is_vis";b:1;s:10:"deleted_at";N;s:7:"checked";b:0;s:4:"data";a:3:{s:2:"id";i:1;s:5:"_node";i:1;s:4:"head";s:3:"CIB";}}s:12:"bot_username";s:13:"Bot name here";}s:14:"*upload_path";N;s:16:"*download_path";N;s:16:"*mysql_enabled";b:0;s:6:"*pdo";N;s:18:"*commands_config";a:0:{}s:14:"*admins_list";a:0:{}s:24:"*last_command_response";O:43:"Longman\TelegramBot\Entities\ServerResponse":4:{s:2:"ok";b:1;s:6:"result";b:1;s:8:"raw_data";a:2:{s:2:"ok";b:1;s:6:"result";b:1;}s:12:"bot_username";N;}s:16:"*botan_enabled";b:0;s:15:"*run_commands";b:0;}

根据他们的 Request documentation,您的请求需要先包含 $telegram 实例,检查下面的代码

$result = Request::initialize($this->telegram)->sendMessage($data);

编辑:

使用可行的解决方案更正了我的答案,并在评论中解释了原因

<?php

namespace App\Jobs;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Exception\TelegramException;

class ProcessTelegramMessageJob extends Job
{
    const TELEGRAM_API_KEY = 'BOTKEY';
    const TELEGRAM_BOT_NAME = 'BOTNAME';

    protected $telegram;
    protected $data;

    public function __construct($data)
    {
        $this->data = $data;

        try {

            $this->telegram = new Telegram(self::TELEGRAM_API_KEY, self::TELEGRAM_BOT_NAME);
            $this->telegram->handle();
            $data = [
                'chat_id' => $this->data['user_id'],
                'text' => $this->data['message'],
            ];
            $result = Request::sendMessage($data);

        } catch (TelegramException $e) {}
    }

    public function handle()
    {
        //
    }
}