<commit_before><?php declare(strict_types=1);
/**
 * This file is part of the TelegramBotManager package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace TelegramBot\TelegramBotManager;

use Longman\IPTools\Ip;
use Longman\TelegramBot\Entities;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog;
use TelegramBot\TelegramBotManager\Exception\InvalidAccessException;
use TelegramBot\TelegramBotManager\Exception\InvalidWebhookException;

class BotManager
{
    /**
     * @var string Telegram post servers IP range
     */
    const TELEGRAM_IP_RANGE = '192.168.127.12-149.154.167.233';

    /**
     * @var string The output for testing, instead of echoing
     */
    private $output = '';

    /**
     * @var \Longman\TelegramBot\Telegram
     */
    private $telegram;

    /**
     * @var \TelegramBot\TelegramBotManager\Params Object that manages the parameters.
     */
    private $params;

    /**
     * @var \TelegramBot\TelegramBotManager\Action Object that contains the current action.
     */
    private $action;

    /**
     * BotManager constructor.
     *
     * @param array $params
     *
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException
     * @throws \Longman\TelegramBot\Exception\TelegramException
     */
    public function __construct(array $params)
    {
        // Initialise logging before anything else, to allow errors to be logged.
        $this->initLogging($params['logging'] ?? []);

        $this->params = new Params($params);
        $this->action = new Action($this->params->getScriptParam('a'));

        // Set up a new Telegram instance.
        $this->telegram = new Telegram(
            $this->params->getBotParam('api_key'),
            $this->params->getBotParam('bot_username')
        );
    }

    /**
     * Check if we're busy running the PHPUnit tests.
     *
     * @return bool
     */
    public static function inTest(): bool
    {
        return defined('PHPUNIT_TEST') && PHPUNIT_TEST === true;
    }

    /**
     * Return the Telegram object.
     *
     * @return \Longman\TelegramBot\Telegram
     */
    public function getTelegram(): Telegram
    {
        return $this->telegram;
    }

    /**
     * Get the Params object.
     *
     * @return \TelegramBot\TelegramBotManager\Params
     */
    public function getParams(): Params
    {
        return $this->params;
    }

    /**
     * Get the Action object.
     *
     * @return \TelegramBot\TelegramBotManager\Action
     */
    public function getAction(): Action
    {
        return $this->action;
    }

    /**
     * Run this thing in all its glory!
     *
     * @return \TelegramBot\TelegramBotManager\BotManager
     * @throws \Longman\TelegramBot\Exception\TelegramException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException
     * @throws \Exception
     */
    public function run(): self
    {
        // Make sure this is a valid call.
        $this->validateSecret();
        $this->validateRequest();

        if ($this->action->isAction(['set', 'unset', 'reset'])) {
            return $this->validateAndSetWebhook();
        }

        $this->setBotExtras();

        if ($this->action->isAction('handle')) {
            $this->handleRequest();
        } elseif ($this->action->isAction('cron')) {
            $this->handleCron();
        } elseif ($this->action->isAction('webhookinfo')) {
            $webhookinfo = Request::getWebhookInfo();
            print_r($webhookinfo->getResult() ?: $webhookinfo->printError());
        }

        return $this;
    }

    /**
     * Initialise all loggers.
     *
     * @param array $log_paths
     *
     * @return \TelegramBot\TelegramBotManager\BotManager
     * @throws \Exception
     */
    public function initLogging(array $log_paths): self
    {
        foreach ($log_paths as $logger => $logfile) {
            ('debug' === $logger) && TelegramLog::initDebugLog($logfile);
            ('error' === $logger) && TelegramLog::initErrorLog($logfile);
            ('update' === $logger) && TelegramLog::initUpdateLog($logfile);
        }
<commit_msg>Handle webhookinfo action sooner to prevent running unnecessary code.<commit_after><?php declare(strict_types=1);
/**
 * This file is part of the TelegramBotManager package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace TelegramBot\TelegramBotManager;

use Longman\IPTools\Ip;
use Longman\TelegramBot\Entities;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog;
use TelegramBot\TelegramBotManager\Exception\InvalidAccessException;
use TelegramBot\TelegramBotManager\Exception\InvalidWebhookException;

class BotManager
{
    /**
     * @var string Telegram post servers IP range
     */
    const TELEGRAM_IP_RANGE = '192.168.127.12-149.154.167.233';

    /**
     * @var string The output for testing, instead of echoing
     */
    private $output = '';

    /**
     * @var \Longman\TelegramBot\Telegram
     */
    private $telegram;

    /**
     * @var \TelegramBot\TelegramBotManager\Params Object that manages the parameters.
     */
    private $params;

    /**
     * @var \TelegramBot\TelegramBotManager\Action Object that contains the current action.
     */
    private $action;

    /**
     * BotManager constructor.
     *
     * @param array $params
     *
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidParamsException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidActionException
     * @throws \Longman\TelegramBot\Exception\TelegramException
     */
    public function __construct(array $params)
    {
        // Initialise logging before anything else, to allow errors to be logged.
        $this->initLogging($params['logging'] ?? []);

        $this->params = new Params($params);
        $this->action = new Action($this->params->getScriptParam('a'));

        // Set up a new Telegram instance.
        $this->telegram = new Telegram(
            $this->params->getBotParam('api_key'),
            $this->params->getBotParam('bot_username')
        );
    }

    /**
     * Check if we're busy running the PHPUnit tests.
     *
     * @return bool
     */
    public static function inTest(): bool
    {
        return defined('PHPUNIT_TEST') && PHPUNIT_TEST === true;
    }

    /**
     * Return the Telegram object.
     *
     * @return \Longman\TelegramBot\Telegram
     */
    public function getTelegram(): Telegram
    {
        return $this->telegram;
    }

    /**
     * Get the Params object.
     *
     * @return \TelegramBot\TelegramBotManager\Params
     */
    public function getParams(): Params
    {
        return $this->params;
    }

    /**
     * Get the Action object.
     *
     * @return \TelegramBot\TelegramBotManager\Action
     */
    public function getAction(): Action
    {
        return $this->action;
    }

    /**
     * Run this thing in all its glory!
     *
     * @return \TelegramBot\TelegramBotManager\BotManager
     * @throws \Longman\TelegramBot\Exception\TelegramException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidAccessException
     * @throws \TelegramBot\TelegramBotManager\Exception\InvalidWebhookException
     * @throws \Exception
     */
    public function run(): self
    {
        // Make sure this is a valid call.
        $this->validateSecret();
        $this->validateRequest();

        if ($this->action->isAction('webhookinfo')) {
            $webhookinfo = Request::getWebhookInfo();
            print_r($webhookinfo->getResult() ?: $webhookinfo->printError());
            return $this;
        }
        if ($this->action->isAction(['set', 'unset', 'reset'])) {
            return $this->validateAndSetWebhook();
        }

        $this->setBotExtras();

        if ($this->action->isAction('handle')) {
            $this->handleRequest();
        } elseif ($this->action->isAction('cron')) {
            $this->handleCron();
        }

        return $this;
    }

    /**
     * Initialise all loggers.
     *
     * @param array $log_paths
     *
     * @return \TelegramBot\TelegramBotManager\BotManager
     * @throws \Exception
     */
    public function initLogging(array $log_paths): self
    {
        foreach ($log_paths as $logger => $logfile) {
            ('debug' === $logger) && TelegramLog::initDebugLog($logfile);
            ('error' === $logger) && TelegramLog::initErrorLog($logfile);
            ('update' === $logger) && TelegramLog::initUpdateLog($logfile);
        }

{'fraction_non_alphanumeric': 0.12714508580343215, 'fraction_numerical': 0.005348785379986628, 'mean_word_length': 2.492217898832685, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6674507', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2567, 'n_words': 556}