Skip to content
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions src/Console/ListenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Nutgram\Laravel\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;
use function Illuminate\Support\artisan_binary;
use function Illuminate\Support\php_binary;

class ListenCommand extends Command
{
protected $signature = 'nutgram:listen {--pollingTimeout=5}';

protected $description = 'Start the bot for development and reloads after every update.';

public function handle(): void
{
$this->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;
}
}
}
}
11 changes: 10 additions & 1 deletion src/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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();
}
}
16 changes: 10 additions & 6 deletions src/NutgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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,
Expand All @@ -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;
}
}
}