mnemo_cards/mnemo_cards_telegram_bot/lib/bot_config.dart

79 lines
2.2 KiB
Dart
Raw Normal View History

2025-11-10 23:55:41 +00:00
import 'dart:io';
class BotConfig {
BotConfig({
required this.backendUrl,
required this.shareDailyLimit,
2025-12-11 17:49:15 +00:00
required this.apiKey,
required this.botToken,
2025-12-11 20:21:38 +00:00
required this.adminIds,
2025-11-10 23:55:41 +00:00
});
final String backendUrl;
final int shareDailyLimit;
2025-12-11 17:49:15 +00:00
final String apiKey;
final String botToken;
2025-12-11 20:21:38 +00:00
final List<String> adminIds;
2025-11-10 23:55:41 +00:00
2025-11-27 19:45:45 +00:00
static const String defaultBackendUrl = 'https://api.mnemo-cards.online';
2025-11-10 23:55:41 +00:00
static const int defaultShareDailyLimit = 1;
2025-12-11 17:49:15 +00:00
factory BotConfig.fromEnvironment({
2025-11-10 23:55:41 +00:00
Map<String, String>? environment,
}) {
final env = environment ?? Platform.environment;
2025-12-11 17:49:15 +00:00
// Backend URL: MNEMO_BACKEND_URL or BACKEND_URL or default
final backendUrl = <String?>[
2025-11-10 23:55:41 +00:00
env['MNEMO_BACKEND_URL'],
env['BACKEND_URL'],
defaultBackendUrl,
].firstWhere(
(value) => value != null && value.trim().isNotEmpty,
)!;
2025-12-11 17:49:15 +00:00
// Share daily limit: BOT_SHARE_DAILY_LIMIT or default
2025-11-10 23:55:41 +00:00
final shareDailyLimitStr = env['BOT_SHARE_DAILY_LIMIT'];
final shareDailyLimit = shareDailyLimitStr != null
? int.tryParse(shareDailyLimitStr) ?? defaultShareDailyLimit
: defaultShareDailyLimit;
2025-12-11 17:49:15 +00:00
// 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');
}
2025-12-11 20:21:38 +00:00
// 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');
}
2025-11-10 23:55:41 +00:00
return BotConfig(
2025-12-11 17:49:15 +00:00
backendUrl: backendUrl.trim(),
2025-11-10 23:55:41 +00:00
shareDailyLimit: shareDailyLimit,
2025-12-11 17:49:15 +00:00
apiKey: apiKey,
botToken: botToken,
2025-12-11 20:21:38 +00:00
adminIds: adminIds,
2025-11-10 23:55:41 +00:00
);
}
}