コード例 #1
0
        private async Task ExecuteCommandAsync(CoffeeContext context, SlackRequest request, SlackResponse response)
        {
            var splitted = request.Text.ToString().Trim().Split(' ', 2);

            var command = splitted[0];
            var option  = splitted.Length > 1 ? splitted[1] : "";

            using var coffee = new CoffeeService(context);

            var user = await coffee.FindUserAsync(request.UserId);

            if (user == null)
            {
                var userName = await GetUserNameAsync(request.Workspace.Name, request.UserId);

                user = await coffee.CreateUserAsync(request.UserId, userName, false);

                await coffee.SaveAsync();
            }
            else if (string.IsNullOrEmpty(user.Name))
            {
                var userName = await GetUserNameAsync(request.Workspace.Name, request.UserId);

                user = await coffee.UpdateUserNameAsync(request.UserId, userName);

                await coffee.SaveAsync();
            }

            try
            {
                await commands.HandleCommandAsync(coffee, user, command, option, response, _logger);

                await coffee.SaveAsync();
            }
            catch (BadRequestException e)
            {
                response.Empty();
                response.Ephemeral(e.ResponseMsg);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error occured during handling command: {command}");
            }

            await response.SendAsync(_slackService);
        }