Exemplo n.º 1
0
        public void GetById()
        {
            _slackUserRepository.AddSlackUser(slackUserDetails);
            var slackUser = _slackUserRepository.GetById(_stringConstant.StringIdForTest);

            Assert.Equal(slackUser.Name, _stringConstant.FirstNameForTest);
        }
        public void SlackEventUpdate()
        {
            _oAuthLoginRepository.SlackEventUpdate(slackEvent);
            var user = _slackUserRepository.GetById(slackEvent.Event.User.UserId);

            Assert.Equal(user.Name, slackEvent.Event.User.Name);
        }
        /// <summary>
        /// Used to connect task mail bot and to capture task mail
        /// </summary>
        /// <param name="container"></param>
        public static void Main(IComponentContext container)
        {
            _logger         = container.Resolve <ILogger>();
            _stringConstant = container.Resolve <IStringConstantRepository>();
            try
            {
                _taskMailRepository = container.Resolve <ITaskMailRepository>();
                _slackUserDetails   = container.Resolve <ISlackUserRepository>();

                _environmentVariableRepository = container.Resolve <IEnvironmentVariableRepository>();
                // assigning bot token on Slack Socket Client
                string            botToken = _environmentVariableRepository.TaskmailAccessToken;
                SlackSocketClient client   = new SlackSocketClient(botToken);
                // Creating a Action<MessageReceived> for Slack Socket Client to get connect. No use in task mail bot
                MessageReceived messageReceive = new MessageReceived();
                messageReceive.ok = true;
                Action <MessageReceived> showMethod = (MessageReceived messageReceived) => new MessageReceived();
                // Telling Slack Socket Client to the bot whose access token was given early
                client.Connect((connected) => { });
                try
                {
                    // Method will hit when someone send some text in task mail bot
                    client.OnMessageReceived += (message) =>
                    {
                        var    user      = _slackUserDetails.GetById(message.user);
                        string replyText = "";
                        var    text      = message.text;
                        if (text.ToLower() == _stringConstant.TaskMailSubject.ToLower())
                        {
                            replyText = _taskMailRepository.StartTaskMail(user.Name).Result;
                        }
                        else
                        {
                            replyText = _taskMailRepository.QuestionAndAnswer(user.Name, text).Result;
                        }
                        // Method to send back response to task mail bot
                        client.SendMessage(showMethod, message.channel, replyText);
                    };
                }
                catch (Exception)
                {
                    client.CloseSocket();
                }
            }
            catch (Exception ex)
            {
                _logger.Error(_stringConstant.LoggerErrorMessageTaskMailBot + " " + ex.Message + "\n" + ex.StackTrace);
                throw ex;
            }
        }
Exemplo n.º 4
0
        public async Task <string> ProcessMessages(string userId, string channelId, string message)
        {
            string              replyText = string.Empty;
            SlackUserDetails    user      = _slackUserDetails.GetById(userId);
            SlackChannelDetails channel   = _slackChannelRepository.GetById(channelId);
            //the command is split to individual words
            //commnads ex: "scrum time", "later @userId"
            var messageArray = message.Split(null);

            if (user != null && String.Compare(message, _stringConstant.ScrumHelp, true) == 0)
            {
                replyText = _stringConstant.ScrumHelpMessage;
            }
            else if (user != null && channel != null)
            {
                //commands could be"scrum time" or "scrum halt" or "scrum resume"
                if (String.Compare(message, _stringConstant.ScrumTime, true) == 0 || String.Compare(message, _stringConstant.ScrumHalt, true) == 0 || String.Compare(message, _stringConstant.ScrumResume, true) == 0)
                {
                    replyText = await Scrum(channel.Name, user.Name, messageArray[1].ToLower());
                }
                //a particular employee is on leave, getting marked as later or asked question again
                //commands would be "leave @userId"
                else if ((String.Compare(messageArray[0], _stringConstant.Leave, true) == 0) && messageArray.Length == 2)
                {
                    int fromIndex = message.IndexOf("<@") + "<@".Length;
                    int toIndex   = message.LastIndexOf(">");
                    if (toIndex > 0)
                    {
                        try
                        {
                            //the userId is fetched
                            string applicantId = message.Substring(fromIndex, toIndex - fromIndex);
                            //fetch the user of the given userId
                            SlackUserDetails applicant = _slackUserDetails.GetById(applicantId);
                            if (applicant != null)
                            {
                                string applicantName = applicant.Name;
                                replyText = await Leave(channel.Name, user.Name, applicantName);
                            }
                            else
                            {
                                replyText = _stringConstant.NotAUser;
                            }
                        }
                        catch (Exception)
                        {
                            replyText = _stringConstant.ScrumHelpMessage;
                        }
                    }
                    else
                    {
                        replyText = await AddScrumAnswer(user.Name, message, channel.Name);
                    }
                }
                //all other texts
                else
                {
                    replyText = await AddScrumAnswer(user.Name, message, channel.Name);
                }
            }
            //If channel is not registered in the database
            else if (user != null)
            {
                //If channel is not registered in the database and the command encountered is "add channel channelname"
                if (channel == null && String.Compare(messageArray[0], _stringConstant.Add, true) == 0 && String.Compare(messageArray[1], _stringConstant.Channel, true) == 0)
                {
                    replyText = AddChannelManually(messageArray[2], user.Name, channelId).Result;
                }
                else
                {
                    replyText = _stringConstant.ChannelAddInstruction;
                }
            }
            else if (user == null)
            {
                SlackBotUserDetail botUser = _slackBotUserDetail.FirstOrDefault(x => x.UserId == userId);
                if (botUser == null)
                {
                    replyText = _stringConstant.NoSlackDetails;
                }
            }

            return(replyText);
        }