예제 #1
0
        public void Connect()
        {
            _botId   = _slackClient.TestAuthorization();
            _botName = _slackClient.Users.Find(item => item.Id == _botId).RealName;
            Stats stats = null;

            _ws.OnMessage += (sender, e) =>
            {          //This method should be reconfigured for use (Console Applications, ASP.NET Web API etc.)
                switch ((string)JObject.Parse(e.Data)["type"])
                {
                case "message":
                    Message message = JsonConvert.DeserializeObject <Message>(e.Data);
                    if (message.Text == null)
                    {
                        break;
                    }
                    if (message.Subtype != "bot_message" && message.Text.Contains("<@" + _botId + ">"))
                    {
                        string userName = _slackClient.Users.Find(item => item.Id == message.User).Name;
                        string userId   = _slackClient.Users.Find(item => item.Id == message.User).Id;
                        stats.MessageReceived();
                        Match m = Regex.Match(message.Text, @"^<@" + _botId + @">\s[a-zA-Z0-9\s]*");
                        if (m.Captures.Count == 0)
                        {
                            throw new InvalidOperationException("Wrong parameter type entered.");
                        }
                        List <string> parameters = new List <string>(m.Value.Split(' '));
                        Dictionary <string, object> objectParameters = new Dictionary <string, object>();
                        {
                            objectParameters.Add("stats", stats);
                            objectParameters.Add("_botName", _botName);
                            objectParameters.Add("_botId", _botId);
                            objectParameters.Add("userName", userName);
                            objectParameters.Add("parameters", parameters);
                            objectParameters.Add("message", message);
                            objectParameters.Add("_ws", _ws);
                            objectParameters.Add("_slackClient", _slackClient);
                            objectParameters.Add("_timerPipeline", _timerPipeline);
                            objectParameters.Add("userId", userId);
                        }
                        _mainPipeline.Run(objectParameters);
                    }
                    break;

                default:
                    break;
                }
            };
            _ws.OnOpen += (sender, e) =>
            {
                stats = new Stats();
                Console.WriteLine("Client is started.");
            };
            _ws.OnClose += (sender, e) =>
            {
                Console.WriteLine("Client is closed.");
                //Should consider saving logs to database in future.
            };
            _ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            _ws.Connect();
            _timer.Enabled = true;
        }