Beispiel #1
0
        public Slack(SlackModel slackConfig, VeeamSession veeamSession)
        {
            // Create a new client
            var client = new SlackClient(slackConfig.WebHook);

            // Set the Veeam Session Data
            _veeamSession = veeamSession;

            // Build the message
            if (!string.IsNullOrEmpty(slackConfig.IconUrl))
            {
                _message.IconUrl = new Uri(slackConfig.IconUrl);
            }
            if (!string.IsNullOrEmpty(slackConfig.Channel))
            {
                _message.Channel = slackConfig.Channel;
            }
            if (!string.IsNullOrEmpty(slackConfig.BotName))
            {
                _message.Username = slackConfig.BotName;
            }

            // Build the rich format with an attachment
            _message.Attachments = new List <SlackAttachment> {
                FormAttachment()
            };

            //Enable TLS 1.2
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            // Post the message
            client.Post(_message);
            Console.WriteLine("Slack message sent.");
        }
        /// <summary>
        /// SlackApiによるメッセージ送信
        /// </summary>
        /// <param name="text"></param>
        /// <param name="menuList"></param>
        /// <returns></returns>
        protected string PostMessageAtSlackApi <T>(SlackModel <T> slackModel)
        {
            using (WebClient webClient = SlackApi.CreateHeader_Post())
            {
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(slackModel);

                return(webClient.UploadString(new Uri("https://slack.com/api/chat.postMessage"), json));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Adds a new service to our config
        /// </summary>
        /// <returns></returns>
        public int AddService()
        {
            // Create a config interface
            var mConfigInterface = new ConfigInterface();
            var serviceMenu      = new TypedMenu <MenuChoice>(_services, "Choose a service to configure", x => x.Name);
            var ynMenu           = new TypedMenu <MenuChoice>(_yesNo, "Save this configuration", x => x.Name);

            var service = serviceMenu.Display();

            switch (service.Id)
            {
            case "slack":
                // Create a new slack config
                var slackConfig = new SlackModel();
                // get the slack models or create a new list
                var slackModels = mConfigInterface.Config.SlackModels ?? new List <SlackModel>();
                // Prompt for the config options
                slackConfig.PromptForNew();
                // Add the config to our main config
                slackModels.Add(slackConfig);
                // Set the new array
                mConfigInterface.Config.SlackModels = slackModels;
                break;

            case "cancel":
                Console.WriteLine("Oh well maybe next time.");
                return(130);

            default:
                Console.WriteLine("You dirty dog, you!");
                return(2);                        // No way you should have gotten here!
            }

            // Prompt to confirm
            var shouldSave = ynMenu.Display();

            switch (shouldSave.Id)
            {
            case "yes":
                Console.Write("Saving... ");
                if (mConfigInterface.WriteConfig())
                {
                    Console.WriteLine("Saved!");
                    return(0);
                }
                else
                {
                    Console.WriteLine("Save Error!");
                    return(2);
                }

            default:
                // If we did not get yes return a 130
                Console.WriteLine("Oh well maybe next time.");
                return(130);
            }
        }
Beispiel #4
0
        public static string sendSlackNotification(string message, string accessToken, string channel)
        {
            var url    = String.Format("https://hooks.slack.com/services/{0}", accessToken);
            var client = new SlackModel(url);

            client.PostMessage(username: "******",
                               text: message,
                               channel: channel);
            return("Sent");
        }
Beispiel #5
0
        public async Task PostMessage(SlackModel model)
        {
            var payload = new SlackPayload
            {
                Channel  = model.Channel,
                Username = model.Username,
                Emojii   = model.Emoji,
                mrkdwn   = true,
                Text     = model.Message,
            };

            await PostMessage(payload);
        }
Beispiel #6
0
        public void PostMessage(string text)
        {
            SlackModel model = new SlackModel()
            {
                Channel  = SlackConstant.Channel,
                Username = SlackConstant.Username,
                Text     = text
            };

            string payloadJson = JsonConvert.SerializeObject(model);

            using (WebClient client = new WebClient())
            {
                NameValueCollection data = new NameValueCollection
                {
                    ["payload"] = payloadJson
                };

                var response = client.UploadValues(_uri, "POST", data);

                string responseText = _encoding.GetString(response);
            }
        }
        public async Task <IActionResult> Publish(SlackModel slackModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _slackPublisher.PostMessage(slackModel);

                    TempData["success_message"] = "Sent!";
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString());
                    TempData["error_message"] = e.ToString();
                }

                return(View("Index", slackModel));
            }
            else
            {
                TempData["error_message"] = "Plz fill all fields, Smarting";
                return(View("Index", slackModel));
            }
        }