Exemplo n.º 1
0
        protected override async Task Send(NotificationMessage model, SlackNotificationSettings settings)
        {
            try
            {
                var body = new SlackNotificationBody
                {
                    channel    = settings.Channel,
                    icon_emoji = settings.IconEmoji,
                    icon_url   = settings.IconUrl,
                    text       = model.Message,
                    username   = settings.Username
                };

                await Api.PushAsync(settings.Team, settings.Token, settings.Service, body);
            }
            catch (Exception e)
            {
                Logger.LogError(LoggingEvents.SlackNotification, e, "Failed to send Slack Notification");
            }
        }
        private async Task Push(SlackNotificationSettings config, string message)
        {
            try
            {
                var notification = new SlackNotificationBody {
                    username = config.Username, channel = config.Channel ?? string.Empty, text = message
                };

                var result = await Api.PushAsync(config.Team, config.Token, config.Service, notification);

                if (!result.Equals("ok"))
                {
                    Log.Error("Slack returned a message that was not 'ok', the notification did not get pushed");
                    Log.Error($"Message that slack returned: {result}");
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Exemplo n.º 3
0
        public async Task <string> PushAsync(string team, string token, string service, SlackNotificationBody message)
        {
            var request = new RestRequest
            {
                Method   = Method.POST,
                Resource = "/services/{team}/{service}/{token}"
            };

            request.AddUrlSegment("team", team);
            request.AddUrlSegment("service", service);
            request.AddUrlSegment("token", token);
            request.AddJsonBody(message);

            var api = new ApiRequest();

            return(await Task.Run(
                       () =>
            {
                var result = api.Execute(request, new Uri("https://hooks.slack.com/"));
                return result.Content;
            }));
        }
Exemplo n.º 4
0
        public async Task <string> PushAsync(string team, string token, string service, SlackNotificationBody message)
        {
            var     request = new Request($"/services/{team}/{service}/{token}", BaseUrl, HttpMethod.Post);
            dynamic body    = new ExpandoObject();

            body.channel    = message.channel;
            body.text       = message.text;
            body.username   = message.username;
            body.link_names = 1;

            if (!string.IsNullOrEmpty(message.icon_url))
            {
                body.icon_url = message.icon_url;
            }

            if (!string.IsNullOrEmpty(message.icon_emoji))
            {
                body.icon_emoji = message.icon_emoji;
            }
            request.AddJsonBody(body);
            request.ApplicationJsonContentType();

            return(await Api.RequestContent(request));
        }