Пример #1
0
        protected async override Task HandleSlackRequestAsync(SlackRequest request)
        {
            // parse request into two strings and check if the first one is a recognized command
            var args = request.Text?.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries) ?? new[] { "" };

            switch (args[0].ToLowerInvariant())
            {
            case "clear": {
                var messages = await _table.ListMessagesAsync();

                // enumerate message from the message table
                if (messages.Any())
                {
                    foreach (var message in messages)
                    {
                        await _table.DeleteMessage(message.MessageId);
                    }
                    Console.WriteLine($"{messages.Count():N0} messages cleared.");
                }
                else
                {
                    Console.WriteLine("There are no messages to clear out.");
                }
            }
            break;

            case "list": {
                var messages = await _table.ListMessagesAsync();

                // enumerate message from the message table
                if (messages.Any())
                {
                    Console.WriteLine($"{messages.Count():N0} messages found.");
                    var count = 0;
                    foreach (var message in messages)
                    {
                        Console.WriteLine($"{++count:N0}: {message.Text} [from {message.Source}]");
                    }
                }
                else
                {
                    Console.WriteLine("There are no messages.");
                }
            }
            break;

            case "send":

                // add a new message to the message table
                if ((args.Length == 1) || string.IsNullOrWhiteSpace(args[1]))
                {
                    Console.WriteLine("No messages after the `send` command to send.");
                }
                else
                {
                    await _table.InsertMessageAsync(new Message {
                        Source = "Slack",
                        Text   = args[1]
                    });

                    Console.WriteLine("Message sent.");
                }
                break;

            case "error":
                throw new Exception("Oh no, I haz br0ken!");

            default:
                Console.WriteLine("Sorry, I only understand `send`, `list`, and `clear` commands.");
                break;
            }
        }