Exemple #1
0
        public static HttpStatusCode PostToDiscord(IDiscordMessage notification)
        {
            Message request = notification.GetDiscordMessage();
            string  urlKey  = ConfigurationManager.AppSettings["DiscordHookToUse"];
            string  url     = ConfigurationManager.AppSettings[urlKey];

            RestClient  client = new RestClient(url);
            RestRequest rr     = new RestRequest();

            rr.JsonSerializer = new NonSuckyJsonSerializer();
            rr.AddJsonBody(request);
            rr.Method = Method.POST;

            try
            {
                IRestResponse response = client.Execute(rr);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    log.Info($"Message posted to discord successfully: {response.Content}");
                }
                else
                {
                    log.Error($"Non-OK code posting message to discord. {Environment.NewLine}Content: {response.Content} {Environment.NewLine}Error Message: {response.ErrorMessage}");
                }

                return(response.StatusCode);
            }
            catch (Exception ex)
            {
                log.Error("Exception posting message to discord.", ex);
            }

            return(HttpStatusCode.InternalServerError);
        }