public ImageModule(IRootPathProvider rootPathProvider, ICommandParser commandParser, IBlobStore store, IImageGenerator imageGenerator) { Post["/image/"] = _ => { Command c = null; if (!commandParser.TryParse(Request.Form.text.HasValue ? (string)Request.Form.text : string.Empty, out c)) { // Invalid input. Early return. return Response.AsJson(UnknownResponse); } if (string.Compare(c.Preamble, Command.HelpConstant, StringComparison.OrdinalIgnoreCase) == 0) { // Help request. Early return. return Response.AsJson(HelpResponse); } var imageId = string.Format("{0}-{1}-{2}.jpg", c.Preamble, Nancy.Helpers.HttpUtility.UrlEncode(c.TopLine ?? string.Empty), Nancy.Helpers.HttpUtility.UrlEncode(c.BottomLine ?? string.Empty)); if (!store.Exists(imageId)) { var img = imageGenerator.GenerateImage(c.Preamble, c.TopLine, c.BottomLine); if (img != null) { using (var ms = new MemoryStream()) { img.Save(ms, ImageFormat.Jpeg); ms.Seek(0, SeekOrigin.Begin); store.Store(imageId, ms); } } else { // We failed to generate an image; send unknown response return Response.AsJson(new Models.UnknownResponse()); } } return Response.AsJson( new Models.ImageResponse(store.GetUri(imageId).ToString(), string.Format("{0} {1}", c.TopLine, c.BottomLine))); }; }
public ImageModule(IRootPathProvider rootPathProvider, ICommandParser commandParser, IBlobStore store, IImageGenerator imageGenerator) { Post["/image/"] = _ => { Command c = null; if (!commandParser.TryParse(Request.Form.text.HasValue ? (string)Request.Form.text : string.Empty, out c)) { // Invalid input. Early return. return(Response.AsJson(UnknownResponse)); } if (string.Compare(c.Preamble, Command.HelpConstant, StringComparison.OrdinalIgnoreCase) == 0) { // Help request. Early return. return(Response.AsJson(HelpResponse)); } var imageId = string.Format("{0}-{1}-{2}.jpg", c.Preamble, Nancy.Helpers.HttpUtility.UrlEncode(c.TopLine ?? string.Empty), Nancy.Helpers.HttpUtility.UrlEncode(c.BottomLine ?? string.Empty)); if (!store.Exists(imageId)) { var img = imageGenerator.GenerateImage(c.Preamble, c.TopLine, c.BottomLine); if (img != null) { using (var ms = new MemoryStream()) { img.Save(ms, ImageFormat.Jpeg); ms.Seek(0, SeekOrigin.Begin); store.Store(imageId, ms); } } else { // We failed to generate an image; send unknown response return(Response.AsJson(new Models.UnknownResponse())); } } return(Response.AsJson( new Models.ImageResponse(store.GetUri(imageId).ToString(), string.Format("{0} {1}", c.TopLine, c.BottomLine)))); }; }
public virtual async Task OnMessageAsync(Message message) { await storageService.SaveLogAsync(Id, message); var command = commandParser.TryParse(message.Text); if (command != null) { await OnCommandMessageAsync(message, command); } else { await OnTextMessageAsync(message); } }
public async ValueTask Process(string data, WebApiUser user) { var parseResult = _parser.TryParse(data); switch (parseResult.Error) { case ParseStatus.Unspecified: await user.Write(_messageFactory.CreateConsoleLog("Invalid input.", "handler")); return; case ParseStatus.UnknownCommand: await user.Write(_messageFactory.CreateConsoleLog( "Unknown command. Please use /help to list the commands and their usages.", "handler")); return; case ParseStatus.NoData: await user.Write(_messageFactory.CreateConsoleLog("This command requires data.", "handler")); return; case ParseStatus.InvalidSyntax: await user.Write( _messageFactory.CreateConsoleLog( $"Invalid syntax. Please use /help to see the usage of that command.", "handler")); return; case ParseStatus.WhiteSpace: await user.Write( _messageFactory.CreateConsoleLog($"Invalid command prefix.", "handler")); return; } parseResult.Command !.Call(user, parseResult.Tokens); }
public static ICommand ParseOrDefault(this ICommandParser parser, string command) => parser.TryParse(command, out var c) ? c : default;