Skip to content

Commit

Permalink
multibot configuration draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Dec 6, 2023
1 parent c960924 commit b2883cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions config/nutgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@

// Set log channel
'log_channel' => env('TELEGRAM_LOG_CHANNEL', 'null'),

// Additional tokens, allows to use multiple bots
'additional_bots' => [
// 'another_bot' => [
// 'token' => env('TELEGRAM_ANOTHER_BOT_TOKEN'),
// ],
],
];
7 changes: 7 additions & 0 deletions src/Facades/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,11 @@ public static function fake(): void
{
static::swap(Nutgram::fake());
}

public static function instance(string $name): Nutgram
{
$bot = app(self::getFacadeAccessor(), ['name' => $name]);
static::swap($bot);
return $bot;
}
}
22 changes: 14 additions & 8 deletions src/NutgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function register()

$this->mergeConfigFrom(self::CONFIG_PATH, 'nutgram');

$this->app->singleton(Nutgram::class, function (Application $app) {
$this->app->bind(Nutgram::class, function (Application $app, array $params = []) {
$configuration = new Configuration(
apiUrl: config('nutgram.config.api_url', Configuration::DEFAULT_API_URL),
botId: config('nutgram.config.bot_id'),
Expand All @@ -51,7 +51,12 @@ public function register()
return Nutgram::fake(config: $configuration);
}

$bot = new Nutgram(config('nutgram.token') ?? FakeNutgram::TOKEN, $configuration);
$params['name'] ??= 'default';
$token = $params['token']
?? config("nutgram.additional_bots.{$params['name']}.token")
?? config('nutgram.token')
?? FakeNutgram::TOKEN;
$bot = new Nutgram($token, $configuration);

if ($app->runningInConsole()) {
$bot->setRunningMode(Polling::class);
Expand All @@ -69,12 +74,18 @@ public function register()
$bot->setRunningMode($webhook);
}

if (config('nutgram.routes', false)) {
(function (Nutgram $bot) {
require file_exists($this->telegramRoutes) ? $this->telegramRoutes : self::ROUTES_PATH;
})($bot);
}

return $bot;
});

$this->app->alias(Nutgram::class, 'nutgram');
$this->app->alias(Nutgram::class, FakeNutgram::class);
$this->app->singleton('telegram', fn (Application $app) => $app->get(Nutgram::class));
$this->app->alias(Nutgram::class, 'telegram');

if (config('nutgram.mixins', false)) {
Nutgram::mixin(new Mixins\NutgramMixin());
Expand Down Expand Up @@ -110,10 +121,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;
}
}
}

0 comments on commit b2883cb

Please sign in to comment.