diff --git a/composer.json b/composer.json index a3af4fc..02408d7 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "require": { "php": "^8.2", "nunomaduro/termwind": "^1.0|^2.0", - "nutgram/nutgram": "^4.17.0" + "nutgram/nutgram": "^4.20" }, "require-dev": { "illuminate/testing": "^9.0|^10.0|^11.0|^12.0", diff --git a/src/Console/ListenCommand.php b/src/Console/ListenCommand.php new file mode 100644 index 0000000..34530f0 --- /dev/null +++ b/src/Console/ListenCommand.php @@ -0,0 +1,33 @@ +warn('This running mode is very inefficient and only suitable for development purposes. DO NOT USE IN PRODUCTION!'); + $this->info('Listening...'); + while (true) { + $result = Process::start([ + php_binary(), artisan_binary(), 'nutgram:run', '--once', + '--pollingTimeout='.$this->option('pollingTimeout'), + ], function (string $type, string $output) { + $this->output->write($output); + })->wait(); + if ($result->exitCode() !== 0) { + $this->error('An error occurred while running the bot. Exiting...'); + break; + } + } + } +} diff --git a/src/Console/RunCommand.php b/src/Console/RunCommand.php index 1d7830e..0ab7612 100644 --- a/src/Console/RunCommand.php +++ b/src/Console/RunCommand.php @@ -6,10 +6,11 @@ use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SergiX44\Nutgram\Nutgram; +use SergiX44\Nutgram\RunningMode\SingleUpdate; class RunCommand extends Command { - protected $signature = 'nutgram:run'; + protected $signature = 'nutgram:run {--once} {--pollingTimeout=}'; protected $description = 'Start the bot in long polling mode'; @@ -19,6 +20,14 @@ class RunCommand extends Command */ public function handle(Nutgram $bot): void { + if ($pollingTimeout = $this->option('pollingTimeout')) { + config()?->set('nutgram.config.polling.timeout', (int)$pollingTimeout); + } + + if ($this->option('once')) { + $bot->setRunningMode(SingleUpdate::class); + } + $bot->run(); } } diff --git a/src/NutgramServiceProvider.php b/src/NutgramServiceProvider.php index 02fa384..9ed2e78 100644 --- a/src/NutgramServiceProvider.php +++ b/src/NutgramServiceProvider.php @@ -73,9 +73,17 @@ public function register() return $bot; }); + $this->app->resolving(Nutgram::class, function (Nutgram $bot, Application $app) { + if (config('nutgram.routes', false)) { + (function () use ($bot) { + require file_exists($this->telegramRoutes) ? $this->telegramRoutes : self::ROUTES_PATH; + })(); + } + }); + $this->app->alias(Nutgram::class, 'nutgram'); + $this->app->alias(Nutgram::class, 'telegram'); $this->app->alias(Nutgram::class, FakeNutgram::class); - $this->app->singleton('telegram', fn (Application $app) => $app->get(Nutgram::class)); if (config('nutgram.mixins', false)) { Nutgram::mixin(new Mixins\NutgramMixin()); @@ -92,6 +100,7 @@ public function boot(): void $this->commands([ Console\RunCommand::class, + Console\ListenCommand::class, Console\RegisterCommandsCommand::class, Console\HookInfoCommand::class, Console\HookRemoveCommand::class, @@ -111,10 +120,5 @@ public function boot(): void self::ROUTES_PATH => $this->telegramRoutes, ], 'nutgram'); } - - if (config('nutgram.routes', false)) { - $bot = $this->app->get(Nutgram::class); - require file_exists($this->telegramRoutes) ? $this->telegramRoutes : self::ROUTES_PATH; - } } }