コード例 #1
0
        public void EnqueueMessage(TelegramOutboundMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            _pendingMessages.Add(message);
        }
コード例 #2
0
        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"));
        }
コード例 #3
0
        public void EnqueueMessage(TelegramOutboundMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (!Settings.IsEnabled)
            {
                return;
            }

            _pendingMessages.Add(message);
        }
コード例 #4
0
 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.");
         }
     }
 }
コード例 #5
0
        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"));
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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"));
        }
コード例 #8
0
        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}.");
            }
        }
コード例 #9
0
ファイル: TelegramBotService.cs プロジェクト: wuzhenda/HA4IoT
        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}.");
                }
            }
        }