コード例 #1
0
        public async Task <HttpResponseMessage> PostAsync(TelegramMessage message, string chatId, ParseMode parseMode)
        {
            var payload = new
            {
                chat_id    = chatId,
                text       = message.Text,
                parse_mode = parseMode.ToString()
            };
            var json     = JsonSerializer.Serialize(value: payload);
            var response = await _httpClient.PostAsync(requestUri : _apiUrl,
                                                       content : new StringContent(content: json, encoding: Encoding.UTF8, mediaType: "application/json"));

            return(response);
        }
コード例 #2
0
        protected void SendMessage(string token, string chatId, TelegramMessage message)
        {
            SelfLog.WriteLine($"Trying to send message to chatId '{chatId}': '{message}'.");

            var client = new TelegramBot(botToken: token, timeoutSeconds: 5);

            var sendMessageTask = client.PostAsync(message: message, chatId: chatId, parseMode: _parseMode);

            Task.WaitAll(sendMessageTask);

            var sendMessageResult = sendMessageTask.Result;

            if (sendMessageResult != null)
            {
                SelfLog.WriteLine($"Message sent to chatId '{chatId}': '{sendMessageResult.StatusCode}'.");
            }
        }