public SlackRequest Map(SlashCommandRequest slashCommandRequest) { _logger.Debug(JsonConvert.SerializeObject(slashCommandRequest)); var text = slashCommandRequest.text ?? ""; foreach (CommandType commandType in Enum.GetValues(typeof(CommandType))) { var description = commandType.GetDescription(); var i = text.IndexOf(description, StringComparison.CurrentCultureIgnoreCase); if (i == 0 && (text.Length == description.Length || text[description.Length] == ' ')) { return(new SlackRequest { CommandType = commandType, AuthorizationToken = slashCommandRequest.token, CommandText = text.Substring(i + description.Length).Trim(' ', '#', ':', '<', '>'), ChannelName = slashCommandRequest.channel_name }); } } if (text.Trim() == "") { return(new SlackRequest { CommandType = CommandType.Help, AuthorizationToken = slashCommandRequest.token, CommandText = "", ChannelName = slashCommandRequest.channel_name }); } var error = $"Unable to find a command in slash command text\n{text}."; _logger.Error(error); throw new SlackRequestMapException(error); }
public async Task <IActionResult> SlashCommand() { var form = await Request.ReadFormAsync(); var slashRequest = new SlashCommandRequest(form); var commandType = DetermineCommand(slashRequest); SlashCommandResponse response = null; switch (commandType) { case ReportBotCommand.AddItem: response = await ProcessAddItem(slashRequest); break; case ReportBotCommand.GenerateReport: response = await ProcessGenerate(slashRequest); break; default: response = ProcessUnknownRequest(slashRequest); break; } return(Ok(response)); }
private static async Task ProcessSlashCommand(SlashCommandRequest slashCommandRequest) { var slashCommandResponse = new SlashCommandResponse { ResponseType = ResponseType.Ephemeral, Text = ExceptionResponse }; try { var slackRequest = RequestMapper.Map(slashCommandRequest); var slackResponse = Processor.Process(slackRequest); slashCommandResponse = ResponseMapper.MapToSlashCommandResponse(slackResponse); } catch (Exception e) { Log.Error("Encountered error while processing slash command.", e); } finally { var content = JsonConvert.SerializeObject(slashCommandResponse, JsonSerializerSettings); Log.Debug($"Response to Slack hook: {content}"); await HttpClient.PostAsync(slashCommandRequest.response_url, new StringContent(content) { Headers = { ContentType = new MediaTypeHeaderValue("application/json") } }); } }
private async Task <SlashCommandResponse> ProcessAddItem(SlashCommandRequest request) { var dashIndex = request.Text.IndexOf('-'); var project = request.Text.Substring(0, dashIndex).Trim(); var item = request.Text.Substring(dashIndex + 1).Trim(); return(await Provider.AddItemToReport(project, item, request.Username)); }
public void RejectedSlashCommandTests(string text) { var sc = new SlashCommandRequest { text = text }; Assert.Throws <SlackRequestMapException>(() => Mapper.Map(sc)); }
public SlashCommandResponse SlashCommand(SlashCommandRequest slashCommandRequest) { Task.Run(() => ProcessSlashCommand(slashCommandRequest)); return(new SlashCommandResponse { Text = "Response incoming. Only you should see this.", ResponseType = ResponseType.Ephemeral }); }
private ReportBotCommand DetermineCommand(SlashCommandRequest request) { if (request.Text.Trim().StartsWith(SlackConsts.GENERATE_COMMAND)) { return(ReportBotCommand.GenerateReport); } else if (request.Text.Contains("-")) { return(ReportBotCommand.AddItem); } else { return(ReportBotCommand.Unknown); } }
public void SlashCommandTests(string text, string token, string channel_name, CommandType commandType, string commandText) { var sc = new SlashCommandRequest { text = text, token = token, channel_name = channel_name }; var sr = Mapper.Map(sc); sr.CommandType.ShouldBeEquivalentTo(commandType); sr.CommandText.ShouldBeEquivalentTo(commandText); sr.ChannelName.ShouldBeEquivalentTo(channel_name); sr.AuthorizationToken.ShouldBeEquivalentTo(token); }
private SlashCommandResponse ProcessUnknownRequest(SlashCommandRequest request) { return(new SlashCommandResponse(SlackConsts.UNKOWN_REQUEST_RESPONSE, true)); }
private async Task <SlashCommandResponse> ProcessGenerate(SlashCommandRequest request) { return(await Provider.GenerateReport()); }