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

I'am trying to send a Telegram message with Telegram Bot (longman/telegram-bot) and Laravel Lumen jobs.

So, first I'am install a Telegram bot with composer require longman/telegram-bot and add to composer.json "Longman\\TelegramBot\\": "vendor/longman/telegram-bot/src". Then create a job

<?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);
    }
}

But, when job try to dispatch, I see in log Call to a member function getBotUsername() on null in ...vendor/longman/telegram-bot/src/Request.php. As I understand Request is not see Telegram instance.

How to fix this error? Thanks in advance!

PS Dump of telegram object

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;}
dongshuang0011
dongshuang0011 我的建议是将这个作为一个问题发布在他们的github中,所以他们可以看看它,因为这是一个有趣的问题,如果它像@Evgeniy那样会变坏
2 年多之前 回复

1个回答



根据他们的请求文档,您的请求需要首先包含$ telegram实例,检查下面的代码</ p>

  $ result  = Request :: initialize($ this-&gt; telegram) - &gt; sendMessage($ data); 
</ code> </ pre>

编辑:</ strong> </ p>

使用工作解决方案更正了我的答案,在评论中解释了原因</ p>

<前> &lt;?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-&gt; data = $ data;

try {

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

} catch(TelegramException $ e){}
}

公共函数句柄()
{
//
}
}
</ </ code> </ pre>
</ div>

展开原文

原文

according to their Request documentation, your request needs to include $telegram instance first, check the code below

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

EDIT:

Corrected my answer with a working solution, explained why in the comments

<?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()
    {
        //
    }
}

doutangliang7769
doutangliang7769 欢迎你,尝试将这个问题发布在他们的github上
2 年多之前 回复
doujiang1832
doujiang1832 非常感谢您的解释!
2 年多之前 回复
dqde43215
dqde43215 我用一个工作代码@Evgeniy更新了我的答案,让其他人知道他们是否遇到同样的问题
2 年多之前 回复
douqufan9148
douqufan9148 他们尝试在Request下创建一个“singleton”,但是在执行代码块之后丢失了它的上下文,在本例中是构造函数,所以当你在handle函数中调用Request :: sendMessage时,它是自己的:$ telegram instance为null因为它是 一个全新的背景。 如果我们甚至可以称之为“单身”,这只是一个糟糕的用途
2 年多之前 回复
dongrong6235
dongrong6235 当我在构造函数中放置sendMessage时,消息发送成功。 但为什么? :)为什么他们不发送句柄方法?
2 年多之前 回复
duanrong0738
duanrong0738 尝试将$ result = Request :: sendMessage($ data)与$ data对象一起移动到构造函数中,就在$ this-> telegram-> handle()下面;
2 年多之前 回复
drm30963
drm30963 再次出错调用未定义的方法Longman \ TelegramBot \ Telegram :: sendMessage()
2 年多之前 回复
doujia2386
doujia2386 尝试$ result = $ this-> telegram-> sendMessage($ data);
2 年多之前 回复
douyangcheng4965
douyangcheng4965 更新问题,Telegram对象不为空。
2 年多之前 回复
dongmin1166
dongmin1166 在我看来,就像你在构造函数中未能创建Telegram对象一样,如果在构造函数中抛出异常,你是否会死掉并转储异常?
2 年多之前 回复
dongzhuo1498
dongzhuo1498 谢谢你的回答,现在在另一条消息中调用一个成员函数sendMessage()on null。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐