mnemo_cards/mnemo_cards_telegram_bot/lib/bot_config.dart
Dmitry 0df722d3bd
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
s
2025-12-11 23:21:38 +03:00

78 lines
2.2 KiB
Dart

import 'dart:io';
class BotConfig {
BotConfig({
required this.backendUrl,
required this.shareDailyLimit,
required this.apiKey,
required this.botToken,
required this.adminIds,
});
final String backendUrl;
final int shareDailyLimit;
final String apiKey;
final String botToken;
final List<String> adminIds;
static const String defaultBackendUrl = 'https://api.mnemo-cards.online';
static const int defaultShareDailyLimit = 1;
factory BotConfig.fromEnvironment({
Map<String, String>? environment,
}) {
final env = environment ?? Platform.environment;
// Backend URL: MNEMO_BACKEND_URL or BACKEND_URL or default
final backendUrl = <String?>[
env['MNEMO_BACKEND_URL'],
env['BACKEND_URL'],
defaultBackendUrl,
].firstWhere(
(value) => value != null && value.trim().isNotEmpty,
)!;
// Share daily limit: BOT_SHARE_DAILY_LIMIT or default
final shareDailyLimitStr = env['BOT_SHARE_DAILY_LIMIT'];
final shareDailyLimit = shareDailyLimitStr != null
? int.tryParse(shareDailyLimitStr) ?? defaultShareDailyLimit
: defaultShareDailyLimit;
// API Key: required
final apiKey = env['TELEGRAM_BOT_API_KEY'];
if (apiKey == null || apiKey.isEmpty) {
throw Exception(
'TELEGRAM_BOT_API_KEY environment variable is required');
}
// Bot Token: required
final botToken = env['TELEGRAM_BOT_TOKEN'];
if (botToken == null || botToken.isEmpty) {
throw Exception('TELEGRAM_BOT_TOKEN environment variable is required');
}
// Admin IDs: required, comma or space separated
final adminIdsStr = env['ADMIN_IDS'];
if (adminIdsStr == null || adminIdsStr.trim().isEmpty) {
throw Exception(
'ADMIN_IDS environment variable is required');
}
final adminIds = adminIdsStr
.split(RegExp(r'[,;\s]+'))
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.toList();
if (adminIds.isEmpty) {
throw Exception(
'ADMIN_IDS must contain at least one admin ID');
}
return BotConfig(
backendUrl: backendUrl.trim(),
shareDailyLimit: shareDailyLimit,
apiKey: apiKey,
botToken: botToken,
adminIds: adminIds,
);
}
}