Exemplo 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;
        }
Exemplo 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);
        }
Exemplo n.º 3
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));
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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);
        }