Ejemplo n.º 1
0
        public static async Task PostToMattermost(MattermostMessage message)
        {
            if (message.Channel == null)
            {
                message.Channel = _config.BotChannelDefault;
            }
            if (message.Username == null)
            {
                message.Username = _config.BotNameDefault;
            }
            if (message.IconUrl == null)
            {
                message.IconUrl = new Uri(_config.BotImageDefault);
            }
            var mc = new MatterhookClient(_config.MattermostWebhookUrl);

            var response = await mc.PostAsync(message);

            if (response == null || response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(response != null
                    ? $"Unable to post to Mattermost.{response.StatusCode}"
                    : $"Unable to post to Mattermost.");
            }
        }
        public MattermostNotificationService(NotificationOptions options)
        {
            if (string.IsNullOrWhiteSpace(options?.Mattermost?.Hook))
            {
                throw new ArgumentNullException(nameof(options.Mattermost.Hook));
            }

            _clinet  = new MatterhookClient(options.Mattermost.Hook);
            _options = options;
        }
        public static void PostBasicMessage(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text     = "Hello, I was posted using [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)",
                Channel  = config.testChannel,
                Username = "******"
            };

            Task.WaitAll(client.PostAsync(message));
        }
        public static void PostBasicMessageWithCard(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text     = "Hello, I was posted using [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)",
                Channel  = config.testChannel,
                Username = "******",
                Props    = new MattermostProps()
                {
                    Card = "**THIS IS A CARD**\n\nIt came from [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)"
                }
            };

            Task.WaitAll(client.PostAsync(message));
        }
        public static void PostButtonsMessage(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text     = "Message Text Example",
                Channel  = config.testChannel,
                Username = "******",
                IconUrl  =
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Robot_icon.svg/2000px-Robot_icon.svg.png",
                Attachments = new List <MattermostAttachment>
                {
                    new MattermostAttachment
                    {
                        Text    = "Attachment Text Example",
                        Actions = new List <IMattermostAction>
                        {
                            new MattermostAction
                            {
                                Name        = "Merge",
                                Integration = new MattermostIntegration(config.outgoingWebHookUrl,
                                                                        new Dictionary <string, object>
                                {
                                    { "pr", 1234 },
                                    { "action", "merge" }
                                })
                            },
                            new MattermostAction
                            {
                                Name        = "Notify",
                                Integration = new MattermostIntegration(config.outgoingWebHookUrl,
                                                                        new Dictionary <string, object>
                                {
                                    { "text", "code was pushed." }
                                })
                            }
                        }
                    }
                }
            };

            Task.WaitAll(client.PostAsync(message));
        }
        public static void PostMenuMessage(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text =
                    "This is a menu message posted using [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)",
                Channel  = config.testChannel,
                Username = "******",
                IconUrl  =
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Robot_icon.svg/2000px-Robot_icon.svg.png",

                Attachments = new List <MattermostAttachment>
                {
                    new MattermostAttachment
                    {
                        Pretext = "This is optional pretext that shows above the attachment.",
                        Text    = "This is the text of the attachment. ",
                        Actions = new List <IMattermostAction>
                        {
                            new MattermostMessageMenu
                            {
                                Integration = new MattermostIntegration(config.outgoingWebHookUrl,
                                                                        new Dictionary <string, object>
                                {
                                    { "text", "Some data to send always." }
                                }),
                                Name    = "Test",
                                Options = new List <MessageMenuOption>
                                {
                                    new MessageMenuOption("Option1", "value1"),
                                    new MessageMenuOption("Option2", "value2"),
                                    new MessageMenuOption("Option3", "value3")
                                }
                            }
                        }
                    }
                }
            };

            Task.WaitAll(client.PostAsync(message));
        }
        public static void PostChannelsMenuMessage(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text =
                    "This is a message menu with channels source posted using [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)",
                Channel  = config.testChannel,
                Username = "******",
                IconUrl  =
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Robot_icon.svg/2000px-Robot_icon.svg.png",

                Attachments = new List <MattermostAttachment>
                {
                    new MattermostAttachment
                    {
                        Pretext = "This is optional pretext that shows above the attachment.",
                        Text    = "This is the text of the attachment. ",
                        Actions = new List <IMattermostAction>
                        {
                            new MattermostMessageMenuChannels
                            {
                                Name        = "channels",
                                Integration = new MattermostIntegration(config.outgoingWebHookUrl,
                                                                        new Dictionary <string, object>
                                {
                                    { "active", "false" }
                                })
                            }
                        }
                    }
                }
            };

            Task.WaitAll(client.PostAsync(message));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Receive()
        {
            _matterhook = new MatterhookClient(_config.MmConfig.WebhookUrl);
            try
            {
                GhWebhook hook;
                try
                {
                    hook = new GhWebhook(Request, _config.Secret); //Signature validation has been moved to GithubWebhookLibrary
                }
                catch (Exception e)
                {
                    return(StatusCode(400, e.Message));
                }

                var message = new MattermostMessage
                {
                    Channel  = _config.MmConfig.Channel,
                    Username = _config.MmConfig.Username,
                    IconUrl  = _config.MmConfig.IconUrl != null ? _config.MmConfig.IconUrl : null
                };

                switch (hook.Event)
                {
                case PullRequestEvent.EventString:
                {
                    var pr = (PullRequestEvent)hook.PayloadObject;

                    if (pr.Action == "closed")
                    {
                        if (pr.PullRequest.Merged != null && (bool)pr.PullRequest.Merged)
                        {
                            var user = pr.PullRequest.User.Login;

                            if (_config.IgnoredUsers_Detailed == null)
                            {
                                _config.IgnoredUsers_Detailed = new List <Code.IgnoredUser>();
                            }

                            if (_config.IgnoredUsers_Detailed.Exists(x => x.UserName == user))
                            {
                                return(StatusCode(200, $"{user} is already in my list!"));
                            }

                            message.Text += "#users-first-contribution\n";

                            var usrMd = $"[{user}]({pr.PullRequest.User.HtmlUrl})";

                            if (_config.CelebrationEmoji != null)
                            {
                                message.Text += $"{_config.CelebrationEmoji} ";
                            }

                            message.Text += $"A User has had a pull request merged for the first time!";

                            if (_config.CelebrationEmoji != null)
                            {
                                message.Text += $" {_config.CelebrationEmoji}";
                            }

                            message.Text += $"\n\nUser: {usrMd}\nPull: {pr.PullRequest.HtmlUrl}";

                            if (_config.CustomString != null)
                            {
                                message.Text += $"\n\n {_config.CustomString}";
                            }

                            var response = await _matterhook.PostAsync(message);

                            if (response == null || response.StatusCode != HttpStatusCode.OK)
                            {
                                return(StatusCode(500, response != null
                                            ? $"Unable to post to Mattermost: {response.StatusCode}"
                                            : "Unable to post to Mattermost"));
                            }

                            _config.IgnoredUsers_Detailed.Add(new Code.IgnoredUser {
                                    UserName         = user,
                                    PullRequestUrl   = pr.PullRequest.HtmlUrl,
                                    ContributionDate = $"{pr.PullRequest.ClosedAt:dd MMMM yyyy HH:mm:ss}"
                                });
                            _config.Save("/config/config.json");

                            return(StatusCode(200, "Succesfully posted to Mattermost"));
                        }
                    }
        public static void PostAdvancedMessage(Config config)
        {
            var client  = new MatterhookClient(config.incomingWebHookUrl);
            var message = new MattermostMessage
            {
                Text     = "Hello, I was posted using [Matterhook.NET.MatterhookClient](https://github.com/promofaux/Matterhook.NET.MatterhookClient)",
                Channel  = config.testChannel,
                Username = "******",
                IconUrl  =
                    "https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Robot_icon.svg/2000px-Robot_icon.svg.png",

                Attachments = new List <MattermostAttachment>
                {
                    new MattermostAttachment
                    {
                        Fallback = "test",
                        Color    = "#FF8000",
                        Pretext  = "This is optional pretext that shows above the attachment.",
                        Text     =
                            "This is the text of the attachment. It should appear just above an image of the Mattermost logo. The left border of the attachment should be colored orange, and below the image it should include additional fields that are formatted in columns. At the top of the attachment, there should be an author name followed by a bolded title. Both the author name and the title should be hyperlinks.",
                        AuthorName = "Mattermost",
                        AuthorIcon = "http://www.mattermost.org/wp-content/uploads/2016/04/icon_WS.png",
                        AuthorLink = "http://www.mattermost.org/",
                        Title      = "Example Attachment",
                        TitleLink  = "http://docs.mattermost.com/developer/message-attachments.html",

                        Fields = new List <MattermostField>
                        {
                            new MattermostField
                            {
                                Short = false,
                                Title = "Long Field.",
                                Value =
                                    "Testing with a very long piece of text that will take up the whole width of the table. And then some more text to make it extra long."
                            },
                            new MattermostField
                            {
                                Short = true,
                                Title = "Column One",
                                Value = "Testing"
                            },
                            new MattermostField
                            {
                                Short = true,
                                Title = "Column Two",
                                Value = "Testing"
                            },
                            new MattermostField
                            {
                                Short = false,
                                Title = "Another Field",
                                Value = "Testing"
                            }
                        },
                        ImageUrl = "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal_WS.png"
                    }
                }
            };

            Task.WaitAll(client.PostAsync(message));
        }
Ejemplo n.º 10
0
 public async Task PushAsync(string webhook, MattermostMessage message)
 {
     var client = new MatterhookClient(webhook);
     await client.PostAsync(_api, message);
 }