示例#1
0
        public async Task VerifyAndSaveBot(string token)
        {
            try
            {
                var chatId = await GetChatId(token);

                var(callName, username) = await GetBotName(token);

                _optionsService.Options.IsTelegramTokenValid = true;
                _optionsService.Options.TelegramToken        = token;
                _optionsService.Options.TelegramChatId       = chatId;
                _optionsService.Options.TelegramBotCallName  = callName;
                _optionsService.Options.TelegramBotUsername  = username;
                await _optionsService.Save();

                _logger.LogInformation("Telegram Bot is activated.");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error when verifying Telegram token.");
                _optionsService.Options.IsTelegramTokenValid = false;
                _optionsService.Options.TelegramToken        = token;
                _optionsService.Options.TelegramChatId       = 0;
                await _optionsService.Save();
            }
        }
示例#2
0
        public async Task <IActionResult> TelegramSave(SettingsModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.TelegramToken))
            {
                await _telegramService.VerifyAndSaveBot(model.TelegramToken);
            }
            else
            {
                _optionsService.Options.IsTelegramTokenValid = false;
                _optionsService.Options.TelegramChatId       = 0;
                _optionsService.Options.TelegramToken        = "";
                await _optionsService.Save();
            }

            return(RedirectToAction("Settings"));
        }