Skip to content

Commit 3ac2156

Browse files
authored
Merge pull request #29 from nutgram/listen-command
Listen command
2 parents cb966cc + ab56fa5 commit 3ac2156

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"require": {
3131
"php": "^8.2",
3232
"nunomaduro/termwind": "^1.0|^2.0",
33-
"nutgram/nutgram": "^4.17.0"
33+
"nutgram/nutgram": "^4.20"
3434
},
3535
"require-dev": {
3636
"illuminate/testing": "^9.0|^10.0|^11.0|^12.0",

src/Console/ListenCommand.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Nutgram\Laravel\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Process;
7+
use function Illuminate\Support\artisan_binary;
8+
use function Illuminate\Support\php_binary;
9+
10+
class ListenCommand extends Command
11+
{
12+
protected $signature = 'nutgram:listen {--pollingTimeout=5}';
13+
14+
protected $description = 'Start the bot for development and reloads after every update.';
15+
16+
public function handle(): void
17+
{
18+
$this->warn('This running mode is very inefficient and only suitable for development purposes. DO NOT USE IN PRODUCTION!');
19+
$this->info('Listening...');
20+
while (true) {
21+
$result = Process::start([
22+
php_binary(), artisan_binary(), 'nutgram:run', '--once',
23+
'--pollingTimeout='.$this->option('pollingTimeout'),
24+
], function (string $type, string $output) {
25+
$this->output->write($output);
26+
})->wait();
27+
if ($result->exitCode() !== 0) {
28+
$this->error('An error occurred while running the bot. Exiting...');
29+
break;
30+
}
31+
}
32+
}
33+
}

src/Console/RunCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use Psr\Container\ContainerExceptionInterface;
77
use Psr\Container\NotFoundExceptionInterface;
88
use SergiX44\Nutgram\Nutgram;
9+
use SergiX44\Nutgram\RunningMode\SingleUpdate;
910

1011
class RunCommand extends Command
1112
{
12-
protected $signature = 'nutgram:run';
13+
protected $signature = 'nutgram:run {--once} {--pollingTimeout=}';
1314

1415
protected $description = 'Start the bot in long polling mode';
1516

@@ -19,6 +20,14 @@ class RunCommand extends Command
1920
*/
2021
public function handle(Nutgram $bot): void
2122
{
23+
if ($pollingTimeout = $this->option('pollingTimeout')) {
24+
config()?->set('nutgram.config.polling.timeout', (int)$pollingTimeout);
25+
}
26+
27+
if ($this->option('once')) {
28+
$bot->setRunningMode(SingleUpdate::class);
29+
}
30+
2231
$bot->run();
2332
}
2433
}

src/NutgramServiceProvider.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ public function register()
7373
return $bot;
7474
});
7575

76+
$this->app->resolving(Nutgram::class, function (Nutgram $bot, Application $app) {
77+
if (config('nutgram.routes', false)) {
78+
(function () use ($bot) {
79+
require file_exists($this->telegramRoutes) ? $this->telegramRoutes : self::ROUTES_PATH;
80+
})();
81+
}
82+
});
83+
7684
$this->app->alias(Nutgram::class, 'nutgram');
85+
$this->app->alias(Nutgram::class, 'telegram');
7786
$this->app->alias(Nutgram::class, FakeNutgram::class);
78-
$this->app->singleton('telegram', fn (Application $app) => $app->get(Nutgram::class));
7987

8088
if (config('nutgram.mixins', false)) {
8189
Nutgram::mixin(new Mixins\NutgramMixin());
@@ -92,6 +100,7 @@ public function boot(): void
92100

93101
$this->commands([
94102
Console\RunCommand::class,
103+
Console\ListenCommand::class,
95104
Console\RegisterCommandsCommand::class,
96105
Console\HookInfoCommand::class,
97106
Console\HookRemoveCommand::class,
@@ -111,10 +120,5 @@ public function boot(): void
111120
self::ROUTES_PATH => $this->telegramRoutes,
112121
], 'nutgram');
113122
}
114-
115-
if (config('nutgram.routes', false)) {
116-
$bot = $this->app->get(Nutgram::class);
117-
require file_exists($this->telegramRoutes) ? $this->telegramRoutes : self::ROUTES_PATH;
118-
}
119123
}
120124
}

0 commit comments

Comments
 (0)