public ActionResult OnPost() { base.Init(); JsonResult result = new JsonResult("Error sending Telegram message."); MemoryStream stream = new MemoryStream(); Request.Body.CopyTo(stream); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { string requestBody = reader.ReadToEnd(); if (requestBody.Length > 0) { TestTelegramForm tf = JsonConvert.DeserializeObject <TestTelegramForm>(requestBody); if (tf != null) { TelegramHelper.SendMessage(tf.Telegram_BotToken.Trim(), tf.Telegram_ChatId, "PT Magic Telegram test message.", tf.Telegram_SilentMode.Equals("on"), Log); result = new JsonResult("Success"); } } } return(result); }
public void Execute() { if (CheckMessagingWorks() == false) { throw new Exception("Watch previous error."); } while (true) { DebugHelper.LogInfo("Search termin."); var hasTermin = false; try { hasTermin = _terminChecker.HasTermin(); } catch (Exception ex) { DebugHelper.LogError(ex.Message); } if (hasTermin) { DebugHelper.LogSuccess("Termin finded!"); DebugHelper.LogInfo($"Sending info..."); if (!string.IsNullOrEmpty(_config.ToEmail)) { EmailHelper.SendEmail(_emailConfig, _config.ToEmail, MessageText.TerminFindedSubject, MessageText.TerminFindedMessage(_config.TerminUri)); } if (!string.IsNullOrEmpty(_config.ToTelegram)) { TelegramHelper.SendMessage(_telegramConfig, _config.ToTelegram, MessageText.TerminFindedMessage(_config.TerminUri)); } } else { DebugHelper.LogInfo("Termin not finded."); } Task.Delay(TimeSpan.FromMinutes(_config.PeriodInMinutes)).Wait(); } }
private bool CheckMessagingWorks() { var isValid = true; if (string.IsNullOrEmpty(_config.ToEmail.Trim()) && string.IsNullOrEmpty(_config.ToTelegram.Trim())) { DebugHelper.LogError("Not found email or telegram info.\n" + "Please, input needed information in appSettings.json file."); isValid = false; } if (!string.IsNullOrEmpty(_config.ToEmail)) { var emailWorks = EmailHelper.SendEmail(_emailConfig, _config.ToEmail, MessageText.TestMessageSubject, MessageText.TestMailMessage); if (emailWorks == false) { DebugHelper.LogError($"Failed to send test message to email: {_config.ToEmail}."); isValid = false; } } if (!string.IsNullOrEmpty(_config.ToTelegram)) { var telegaWorks = TelegramHelper.SendMessage(_telegramConfig, _config.ToTelegram, MessageText.TestTelegramMessage); if (telegaWorks == false) { DebugHelper.LogError($"Failed to send test message to telegram: {_config.ToTelegram}."); isValid = false; } } return(isValid); }