douyu5679 2018-02-09 09:15
浏览 82
已采纳

在... 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;}
  • 写回答

1条回答 默认 最新

  • dongxi5494 2018-02-09 09:32
    关注

    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()
        {
            //
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退