public async Task <IActionResult> Do() { using var coffee = new CoffeeService(_coffeeContext); string id = Request.Form["userId"]; string name = Request.Form["userName"]; var user = await coffee.FindUserAsync(id); if (user == null) { user = await coffee.CreateUserAsync(id, false); } var splitted = Request.Form["text"].ToString().Trim().Split(' ', 2); var command = splitted[0]; var option = splitted.Length > 1 ? splitted[1] : ""; var handler = handlers.GetHandler(this, user, command); if (handler == null) { return(SlackBadRequest("없는 명령어 입니다.")); } try { return(await handler(option)); } catch (BadRequestException e) { return(SlackBadRequest(e.Message)); } }
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); }
// [CoffeeCommand("대리적립", "", true)] public async Task FillOtherWallet(CoffeeService coffee, User requestUser, string text, SlackResponse response) { var splitted = text.Split(' '); if (splitted.Length != 2 || !int.TryParse(splitted[1], out var amount)) { throw new NotWellFormedException(); } var userId = SlackBot.Utils.StringToUserId(splitted[0]); var user = await coffee.FindUserAsync(userId); if (user == null) { user = await coffee.CreateUserAsync(userId, null, false); await coffee.SaveAsync(); } var u = await coffee.FillWalletAsync(userId, amount, DateTime.Now); response.Ephemeral($"현재 잔액은 {u.Deposit}원 입니다."); }