php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.91k stars 958 forks source link

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

Closed esomkin closed 6 years ago

esomkin commented 6 years ago

Required Information

Expected behaviour

I have a Telegram bot, which is located on domain1.com. Telegram bot tested and works fine. Also I have a system (build with Laravel Lumen), which is located on domain2.com and it is try to send Telegram messages without user interaction as described in wiki (https://github.com/php-telegram-bot/core/wiki/How-to-send-message-to-channel-user-without-user-interaction). Both domains has valid SSL certificates.

So, to teach system send Telegram messages, I'am using a tutorial from wiki too (https://github.com/php-telegram-bot/core/wiki/Install-on-Laravel). Messages is send by Laravel jobs, so my job is

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

Actual behaviour

When Laravel queue try to dispatch a job ProcessTelegramMessageJob it is process infinitelly. In log I see a message Call to a member function getBotUsername() on null in …vendor/longman/telegram-bot/src/Request.php. How can I fix this problem? Thank you in advance!

esomkin commented 6 years ago

I'am trying to initialize Request by telegram object manually, but code $result = Request::initialize($this->telegram)->sendMessage($data); gives an error Call to a member function sendMessage() on null

noplanman commented 6 years ago

Hi @esomkin When you create a new Telegram object, the Request class gets initialised already, no need to manually do that. You get the error because Request::initialize(...) does not return anything.

Try just using Request::sendMessage(...) after you've created your Telegram object!

Also, in your initial message, I think you want $this->telegram->handle(); to be $this->handle();, right?

Or does the webhook call link into your job?

esomkin commented 6 years ago

Hi @noplanman

Thank you for answer, I'am using Request::sendMessage(...) too, as you can see in my question. Handle method of a Job class start automatically, $this->telegram->handle() is removed, but result is the same - error in console.

esomkin commented 6 years ago

@noplanman It is strange but, message send successful only if I place send code into __construct.

noplanman commented 6 years ago

I think you didn't read my comment properly...

You want $this->telegram->handle(); to be $this->handle();. Otherwise the method obviously isn't called.

Are you new to PHP programming?

esomkin commented 6 years ago

@noplanman I read your comment, and I understand it good. Handle method of a Laravel job is called automatically by Laravel, that is why I place sendMessage here. $this->telegram->handle(); I add as in hook.php (https://github.com/php-telegram-bot/example-bot/blob/master/hook.php), it can be removed and it does not matter. The question is why it works only if place creating new Telegram object and Request::messageSend in __construct together?

esomkin commented 6 years ago

@noplanman Please check discussion about this problem https://stackoverflow.com/questions/48702352/call-to-a-member-function-getbotusername-on-null-in-vendor-longman-telegram/48702699?noredirect=1#48702699

noplanman commented 6 years ago

Riiight @ Laravel calling handle() method 👍

You could also just call Request::initialize($this->telegram); first in your handle() method, before sending the message. Then it should work.

(Also for next time, best come here first instead of SO, so that we can do something about issues that come up)

jacklul commented 6 years ago

Suggestion: Could we add an exception throw when Request is not initialized @noplanman ? 🤔

esomkin commented 6 years ago

@noplanman Ok, thank you for your answers!

noplanman commented 6 years ago

So we can close off here then, right?

esomkin commented 6 years ago

@noplanman I think, yes