php-telegram-bot / example-bot

[WIP] An A-Z example of a PHP Telegram Bot.
MIT License
371 stars 187 forks source link

Command doesn't work #54

Closed mogilka closed 2 years ago

mogilka commented 2 years ago

Hello, please help. My Start command stopped working a month ago. In the beginning of April I've upgraded OS server successfully and I've updated your php-telegram-bot (source code and DB). Everything looks to be working fine, but commands. I've checked everything but I can't find the error

Here is my start command:

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Commands\SystemCommand;

/**
 * Переопределение системной команды /start
 */
class StartCommand extends SystemCommand {

    protected $name = 'start';
    protected $description = 'Подключение к боту';
    protected $usage = '/start';
    protected $version = '1.0.0';
    protected $private_only = true;

    public function execute(): ServerResponse {
        try {
            $message = $this->getMessage();
            $chatid = $message->getChat()->getId();
                $text = "";

            return Request::sendMessage([
                'chat_id'=>$chatid,
                'text'=>$text
            ]);
        } catch (TelegramException $e) {
            \Yii::error(__METHOD__.$e->getTraceAsString());
        }
        return Request::emptyResponse();
    }
}

My hook:

    public function actionHook() {
        // Load composer
        require __DIR__.'/../vendor/autoload.php';
        try {
            $BOT_NAME = Yii::$app->params["telebot"];
            // Create Telegram API object
            $telegram = new Telegram(Yii::$app->params["telekey"], $BOT_NAME);

            $commands_paths = [
                            Yii::getAlias("@app")."/bot"
                       ];
                       $telegram->addCommandsPaths($commands_paths);

            $mysql_credentials = require('../config/dbot.php');
            $telegram->enableMySQL($mysql_credentials);

            //set bot admin
            $USER_ID = ---------; //получить у @myidbot
            $telegram->enableAdmin($USER_ID);

            TelegramLog::initialize(
                new Logger($BOT_NAME, [
                    (new StreamHandler(Yii::getAlias("@app").'/botlog/debug.log', Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
                    (new StreamHandler(Yii::getAlias("@app").'/botlog/error.log', Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
                ]),
                new Logger($BOT_NAME."_updates", [
                    (new StreamHandler(Yii::getAlias("@app").'/botlog/update.log', Logger::INFO))->setFormatter(new LineFormatter(null, null, true)),
                ]));
            TelegramLog::debug(print_r($telegram->getCommandsPaths()));
            $telegram->handle();
        } catch (TelegramException $e) {
            // Silence is golden!
            // log telegram errors
            //echo $e->getMessage();
            Yii::error(__METHOD__.$e->getMessage());
        }
    }

Before handle I set commands path and check it - via this line:

TelegramLog::debug(print_r($telegram->getCommandsPaths()));

But in the debug.log I see a message:

[2022-05-11T09:44:26.745532+00:00] zvezdo_bot.DEBUG: 1 [] []

It means that commands path haven't set. But why? What's wrong and how to fix it?

OxMohsen commented 2 years ago

can you please share the result of getWebhookInfo method or error_log btw i see your code and find that your $text variable is empty so nothing will be send to user.

noplanman commented 2 years ago

Hi @mogilka, the output is correct, because print_r needs a second parameter to return the actual value.

Try with:

TelegramLog::debug(print_r($telegram->getCommandsPaths(), true));

Also, as @OxMohsen mentioned, you need to add some text to a message.

mogilka commented 2 years ago

Hi @mogilka, the output is correct, because print_r needs a second parameter to return the actual value.

Try with:

TelegramLog::debug(print_r($telegram->getCommandsPaths(), true));

Also, as @OxMohsen mentioned, you need to add some text to a message.

you're right, thank you. Now I see that commands_paths contains my folder and SystemCommands folder. I tried to send unempty message but nothing happens anyway

mogilka commented 2 years ago

can you please share the result of getWebhookInfo method or error_log btw i see your code and find that your $text variable is empty so nothing will be send to user.

Sending unempty message doesn't work too. Error.log is empty unfortunately. Here is WebhookInfo output:

[2022-05-12T08:10:23.391332+00:00] zvezdo_bot.DEBUG: {"ok":true,"result":{"url":"https:\/\/zvezdochet.guru\/ru\/bot\/hook","has_custom_certificate":false,"pending_update_count":19,"last_error_date":1652342962,"last_error_message":"Wrong response from the webhook: 500 Internal Server Error","max_connections":40,"ip_address":"---.---.---.75"}} [] []

It means Telegram bot limit expired?

noplanman commented 2 years ago

There is an error with your server setup: 500 Internal Server Check the error log of your webserver.

What version of PHP and the bot are you using? It may have to do with missing PHP extensions.

mogilka commented 2 years ago

@noplanman Okey, everything worked today after some errors fixed. All of them occurred after big php-telegram-bot updating (I don't remember when I did it last time before April 2022):

  1. First of all, webserver log showed an error, fixed:

2022/05/13 07:59:04 [error] PHP Fatal error: Declaration of Longman\TelegramBot\Commands\UserCommands\HelpCommand::execute() must be compatible with Longman\TelegramBot\Commands\Command::execute()

  1. Then, after the next update I had to execute 0.76.1-0.77.0.sql. During this I faced to MySQL error:

[MySQL: Error Code: 1118 Row size too large (> 8126). Changing some columns to TEXT or BLOB]

The reason for this is not very clear to me but I fixed it by setting some variables in MySQL config. After this I tested my bot commands and they was executed successfully

Answering your questions about PHP version:

~$ php -v PHP 7.4.28 (cli) (built: Feb 17 2022 16:17:19) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies

Thanks to all