Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listen command #24

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/.github export-ignore
/tests export-ignore
.codeclimate.yml export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.styleci.yml export-ignore
CHANGELOG.md export-ignore
CODE_OF_CONDUCT.md export-ignore
phpunit.xml.dist export-ignore
psalm.xml export-ignore
README.md export-ignore
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": "dev-single-polling-update"
},
"require-dev": {
"illuminate/testing": "^9.0|^10.0|^11.0",
Expand Down
Binary file removed composer.phar
Binary file not shown.
25 changes: 25 additions & 0 deletions src/Console/ListenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Nutgram\Laravel\Console;

use Illuminate\Console\Command;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\RunningMode\SingleUpdate;

class ListenCommand extends Command
{
protected $signature = 'nutgram:listen';

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

public function handle(): void
{
$this->info('Listening...');
while (true) {
app()->forgetInstance(Nutgram::class);
$bot = app(Nutgram::class);
$bot->setRunningMode(SingleUpdate::class);
$bot->run();
}
}
}
14 changes: 9 additions & 5 deletions src/NutgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ 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, FakeNutgram::class);
$this->app->singleton('telegram', fn (Application $app) => $app->get(Nutgram::class));
Expand All @@ -91,6 +99,7 @@ public function boot(): void

$this->commands([
Console\RunCommand::class,
Console\ListenCommand::class,
Console\RegisterCommandsCommand::class,
Console\HookInfoCommand::class,
Console\HookRemoveCommand::class,
Expand All @@ -110,10 +119,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;
}
}
}
Loading