public void EnqueueMessage(TelegramOutboundMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } _pendingMessages.Add(message); }
private StringContent ConvertOutboundMessageToJsonMessage(TelegramOutboundMessage message) { var json = new JsonObject(); json.SetNamedNumber("chat_id", message.ChatId); json.SetNamedString("parse_mode", "HTML"); json.SetNamedString("text", message.Text); return(new StringContent(json.Stringify(), Encoding.UTF8, "application/json")); }
public void EnqueueMessage(TelegramOutboundMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (!Settings.IsEnabled) { return; } _pendingMessages.Add(message); }
private async Task ProcessPendingMessagesAsync() { while (true) { try { TelegramOutboundMessage message = _pendingMessages.Take(); await SendMessageAsync(message); } catch (Exception exception) { Log.Error(exception, "Error while processing pending Telegram messages."); } } }
private StringContent ConvertOutboundMessageToJsonMessage(TelegramOutboundMessage message) { if (message.Text.Length > 4096) { throw new InvalidOperationException("The Telegram outbound message is too long."); } var json = new JsonObject(); json.SetNamedNumber("chat_id", message.ChatId); json.SetNamedString("text", message.Text); if (message.Format == TelegramMessageFormat.HTML) { json.SetNamedString("parse_mode", "HTML"); } return(new StringContent(json.Stringify(), Encoding.UTF8, "application/json")); }
public async Task <bool> TrySendMessageAsync(TelegramOutboundMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } try { await SendMessageAsync(message); return(true); } catch (Exception exception) { Log.Warning(exception, "Error while sending Telegram message."); return(false); } }
private StringContent ConvertOutboundMessageToJsonMessage(TelegramOutboundMessage message) { if (message.Text.Length > 4096) { throw new InvalidOperationException("The Telegram outbound message is too long."); } var json = new JObject { ["chat_id"] = message.ChatId, ["text"] = message.Text }; if (message.Format == TelegramMessageFormat.HTML) { json["parse_mode"] = "HTML"; } return(new StringContent(json.ToString(), Encoding.UTF8, "application/json")); }
public async Task SendMessageAsync(TelegramOutboundMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } using (var httpClient = new HttpClient()) { string uri = $"{BaseUri}{AuthenticationToken}/sendMessage"; StringContent body = ConvertOutboundMessageToJsonMessage(message); HttpResponseMessage response = await httpClient.PostAsync(uri, body); if (!response.IsSuccessStatusCode) { throw new InvalidOperationException($"Sending Telegram message failed (StatusCode={response.StatusCode})."); } Log.Info($"Sent Telegram message '{message.Text}' to chat {message.ChatId}."); } }
private async Task SendMessageAsync(TelegramOutboundMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } using (var httpClient = new HttpClient()) { var uri = $"{BaseUri}{Settings.AuthenticationToken}/sendMessage"; var body = ConvertOutboundMessageToJsonMessage(message); var response = await httpClient.PostAsync(uri, body); if (!response.IsSuccessStatusCode) { _log.Warning($"Sending Telegram message failed (Message='${message.Text}' StatusCode={response.StatusCode})."); } else { _log.Info($"Sent Telegram message '{message.Text}' to chat {message.ChatId}."); } } }