示例#1
0
        public async Task TheHandleMethodShouldChatAsync(string regex, string response, string arguments)
        {
            // Arrange
            var chatResponses = new[] { new ChatResponse {
                                            Regex = regex, Response = response
                                        }, };

            chatResponseRepository.GetAsync(CancellationToken.None).Returns(chatResponses);

            // Act
            await commandHandler.HandleAsync(arguments, string.Empty, default);

            // Assert
            slackbot.Received().SendMessage(string.Empty, response);
        }
        public async Task HandleAsync(string arguments, string channel, CancellationToken cancellationToken)
        {
            Match match = regex.Match(arguments ?? string.Empty);

            if (match.Success)
            {
                switch (match.Groups[1].Value)
                {
                case "add":

                    await chatResponseRepository.AddAsync(match.Groups[2].Value, match.Groups[3].Value);

                    slackbot.SendMessage(channel, "I added your chat response to my AI.");

                    break;

                case "remove":
                    if (await chatResponseRepository.RemoveAsync(match.Groups[2].Value))
                    {
                        slackbot.SendMessage(channel, "I just forgot your response. Can't remember a thing.");
                    }

                    break;
                }
            }
            else
            {
                foreach (ChatResponse chatResponse in await chatResponseRepository.GetAsync(cancellationToken))
                {
                    if (new Regex(chatResponse.Regex).IsMatch(arguments ?? string.Empty))
                    {
                        slackbot.SendMessage(channel, chatResponse.Response);

                        break;
                    }
                }
            }
        }