66 lines
2.2 KiB
PHP
66 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Services\CorunaArchive;
|
|
use App\Services\CorunaCrypto;
|
|
use App\Services\SettingsService;
|
|
use App\Telegram\TelegramBotContext;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Nutgram\Laravel\RunningMode\LaravelWebhook;
|
|
use SergiX44\Nutgram\Nutgram;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
$this->app->singleton(TelegramBotContext::class);
|
|
|
|
$this->app->singleton(CorunaCrypto::class, function () {
|
|
$override = config('coruna.session_key');
|
|
|
|
return new CorunaCrypto(is_string($override) && $override !== '' ? $override : null);
|
|
});
|
|
|
|
$this->app->singleton('xxbb.crypto', function () {
|
|
$key = config('coruna.xxbb.session_key', 'Ek8pl31K2yeHgQwy');
|
|
|
|
return new CorunaCrypto(is_string($key) && $key !== '' ? $key : 'Ek8pl31K2yeHgQwy');
|
|
});
|
|
|
|
$this->app->singleton(CorunaArchive::class, function ($app) {
|
|
return new CorunaArchive(
|
|
$app->make(CorunaCrypto::class),
|
|
(string) config('coruna.seven_zip', ''),
|
|
);
|
|
});
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
try {
|
|
$this->app->make(SettingsService::class)->applyToConfig();
|
|
} catch (\Throwable) {
|
|
// settings table may not exist yet during migrate
|
|
}
|
|
|
|
// Override Nutgram's production safe_mode (md5 APP_KEY) so webhook updates
|
|
// are not silently dropped when Telegram secret_token is unset/mismatched.
|
|
$this->app->afterResolving(Nutgram::class, function (Nutgram $bot): void {
|
|
if ($this->app->runningUnitTests() || $this->app->runningInConsole()) {
|
|
return;
|
|
}
|
|
|
|
$secret = (string) config('coruna.telegram.webhook_secret', '');
|
|
$webhook = $secret !== ''
|
|
? new LaravelWebhook(
|
|
getToken: static fn () => request()?->header('X-Telegram-Bot-Api-Secret-Token'),
|
|
secretToken: $secret,
|
|
)
|
|
: new LaravelWebhook;
|
|
$webhook->setSafeMode($secret !== '');
|
|
$bot->setRunningMode($webhook);
|
|
});
|
|
}
|
|
}
|