Пример #1
0
        /// <summary>
        /// post a message to a channel with embeded token and channel information
        /// </summary>
        /// <param name="teamsMessage"></param>
        public void PostMessage(TeamsMessage teamsMessage)
        {
            string jsonPayload = JsonConvert.SerializeObject(teamsMessage);

            using (HttpClient webClient = new HttpClient())
            {
                foreach (Uri channelAndToken in _ChannelsAndTokens)
                {
                    try
                    {
                        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, channelAndToken);
                        requestMessage.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
                        HttpResponseMessage httpResponseMessage = webClient.SendAsync(requestMessage).Result;
                        string responseText = httpResponseMessage.Content.ReadAsStringAsync().Result;
                        System.Diagnostics.Trace.WriteLine(responseText);
                        if (responseText.Contains("Microsoft Teams endpoint returned HTTP error 429", StringComparison.OrdinalIgnoreCase))
                        {
                            //TODO retry logic ?
                        }
                    }
                    catch (Exception postError)
                    {
                        Console.WriteLine("Error posting to Teams " + postError.Message);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        ///Post a message to the predefined Url
        /// </summary>
        /// <param name="messageText"></param>
        public void PostMessage(string messageText)
        {
            TeamsMessage teamsMessage = new TeamsMessage()
            {
                //Max message length is 1024 characters
                Text = messageText.TrimTo(1024, false)
            };

            PostMessage(teamsMessage);
        }