Esempio n. 1
0
        private static GitterChatMessageResult PostToIncomingWebHook(ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            context.Verbose("Posting to incoming webhook {0}...", string.Concat(messageSettings.IncomingWebHookUrl.TrimEnd('/').Reverse().SkipWhile(c => c != '/').Reverse()));

            var gitterWebHookService = new WebhookService();
            var result = gitterWebHookService.PostAsync(messageSettings.IncomingWebHookUrl, message, messageSettings.MessageLevel == GitterMessageLevel.Error ? MessageLevel.Error : MessageLevel.Info);

            var parsedResult = new GitterChatMessageResult(result.Result, DateTime.UtcNow.ToString("u"), string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return parsedResult;
        }
Esempio n. 2
0
        public GitterChatMessageResult PostMessage(string message, GitterChatMessageSettings messageSettings)
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException("messageSettings");
            }

            if (string.IsNullOrWhiteSpace(message))
            {
                throw new NullReferenceException("Unable to send empty message.");
            }

            return _context.PostMessage(message, messageSettings);
        }
        public GitterChatMessageResult PostMessage(string message, GitterChatMessageSettings messageSettings)
        {
            if (messageSettings == null)
            {
                throw new ArgumentNullException("messageSettings");
            }

            if (string.IsNullOrWhiteSpace(message))
            {
                throw new NullReferenceException("Unable to send empty message.");
            }

            return(_context.PostMessage(message, messageSettings));
        }
Esempio n. 4
0
        private static GitterChatMessageResult PostToChatApi(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            if (string.IsNullOrWhiteSpace(messageSettings.Token))
            {
                throw new NullReferenceException("No authorization token provided.");
            }

            var gitterApiService = new GitterApiService(messageSettings.Token);
            var messageResponse = gitterApiService.SendMessageAsync(messageSettings.RoomId, message);

            var parsedResult = new GitterChatMessageResult(!string.IsNullOrWhiteSpace(messageResponse.Result.Id), messageResponse.Result.SentDate.ToString("u"), string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return parsedResult;
        }
Esempio n. 5
0
        /// <summary>
        /// Sends a message to Gitter, based on the provided settings
        /// </summary>
        /// <param name="context">The Cake Context</param>
        /// <param name="message">The message to be sent</param>
        /// <param name="messageSettings">The settings to be used when sending the message</param>
        /// <returns>An instance of <see cref="GitterChatMessageResult"/> indicating success/failure</returns>
        internal static GitterChatMessageResult PostMessage(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            GitterChatMessageResult result;
            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(context, message, messageSettings);
            }
            else
            {
                result = context.PostToChatApi(message, messageSettings);
            }

            if (!result.Ok && messageSettings.ThrowOnFail == true)
            {
                throw new CakeException(result.Error ?? "Failed to send message, unknown error");
            }

            return result;
        }
Esempio n. 6
0
        private static async Task <GitterChatMessageResult> PostToChatApi(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            if (string.IsNullOrWhiteSpace(messageSettings.Token))
            {
                throw new NullReferenceException("No authorization token provided.");
            }

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            if (!string.IsNullOrWhiteSpace(messageSettings.Token))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", messageSettings.Token);
            }

            string url = $"https://api.gitter.im/v1/rooms/{messageSettings.RoomId}/chatMessages";

            var content = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                { "text", message }
            });

            var httpResponse = await httpClient.PostAsync(url, content);

            var response = await httpResponse.Content.ReadAsStringAsync();

            var result       = JsonMapper.ToObject(response);
            var parsedResult = new GitterChatMessageResult(
                httpResponse.IsSuccessStatusCode,
                result.GetString("sent"),
                string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return(parsedResult);
        }
Esempio n. 7
0
        private static async Task <GitterChatMessageResult> PostToIncomingWebHook(ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            context.Verbose("Posting to incoming webhook {0}...", string.Concat(messageSettings.IncomingWebHookUrl.TrimEnd('/').Reverse().SkipWhile(c => c != '/').Reverse()));

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");

            var content = new FormUrlEncodedContent(new Dictionary <string, string>
            {
                { "message", message },
                { "level", messageSettings.MessageLevel == GitterMessageLevel.Error ? "error" : "info" }
            });

            var httpResponse = await httpClient.PostAsync(new Uri(messageSettings.IncomingWebHookUrl), content);

            var response = await httpResponse.Content.ReadAsStringAsync();

            var result = JsonMapper.ToObject(response);

            var parsedResult = new GitterChatMessageResult(
                httpResponse.IsSuccessStatusCode,
                DateTime.UtcNow.ToString("u"),
                string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return(parsedResult);
        }
Esempio n. 8
0
        /// <summary>
        /// Sends a message to Gitter, based on the provided settings
        /// </summary>
        /// <param name="context">The Cake Context</param>
        /// <param name="message">The message to be sent</param>
        /// <param name="messageSettings">The settings to be used when sending the message</param>
        /// <returns>An instance of <see cref="GitterChatMessageResult"/> indicating success/failure</returns>
        internal static GitterChatMessageResult PostMessage(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            GitterChatMessageResult result;

            if (!string.IsNullOrWhiteSpace(messageSettings.IncomingWebHookUrl))
            {
                result = PostToIncomingWebHook(context, message, messageSettings).Result;
            }
            else
            {
                result = context.PostToChatApi(message, messageSettings).Result;
            }

            if (!result.Ok && messageSettings.ThrowOnFail == true)
            {
                throw new CakeException(result.Error ?? "Failed to send message, unknown error");
            }

            return(result);
        }
Esempio n. 9
0
        private static GitterChatMessageResult PostToChatApi(this ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            if (string.IsNullOrWhiteSpace(messageSettings.Token))
            {
                throw new NullReferenceException("No authorization token provided.");
            }

            var gitterApiService = new GitterApiService(messageSettings.Token);
            var messageResponse  = gitterApiService.SendMessageAsync(messageSettings.RoomId, message);

            var parsedResult = new GitterChatMessageResult(!string.IsNullOrWhiteSpace(messageResponse.Result.Id), messageResponse.Result.SentDate.ToString("u"), string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return(parsedResult);
        }
Esempio n. 10
0
        private static GitterChatMessageResult PostToIncomingWebHook(ICakeContext context, string message, GitterChatMessageSettings messageSettings)
        {
            context.Verbose("Posting to incoming webhook {0}...", string.Concat(messageSettings.IncomingWebHookUrl.TrimEnd('/').Reverse().SkipWhile(c => c != '/').Reverse()));

            var gitterWebHookService = new WebhookService();
            var result = gitterWebHookService.PostAsync(messageSettings.IncomingWebHookUrl, message, messageSettings.MessageLevel == GitterMessageLevel.Error ? MessageLevel.Error : MessageLevel.Info);

            var parsedResult = new GitterChatMessageResult(result.Result, DateTime.UtcNow.ToString("u"), string.Empty);

            context.Debug("Result parsed: {0}", parsedResult);

            return(parsedResult);
        }