Пример #1
0
        public override async Task <bool> Execute(Message message,
                                                  CallbackQuery callbackQuery,
                                                  TelegramBotClient botClient)
        {
            var endCommand = false;

            switch (_requestType)
            {
            case RequestType.Action:
                switch (callbackQuery.Data)
                {
                case "List":
                    await botClient.SendDocumentAsyncFromText(message.Chat.Id,
                                                              _aliasManager.ToTable(true),
                                                              "list.txt");

                    endCommand = true;
                    break;

                case "Delete":
                    //request name
                    _requestType = RequestType.DeleteRequestName;
                    await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id, "Name alias");

                    break;

                case "Create":
                    //request name
                    _requestType = RequestType.CreateRequestName;
                    await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id, "Name alias");

                    break;

                default: break;
                }
                break;

            default: break;
            }

            return(await Task.FromResult(endCommand));
        }
        private static void CmdAlias(CommandLineApplication parent, AliasManager aliasManager)
        {
            parent.Command("alias", cmd =>
            {
                cmd.Description = "Alias commands";
                cmd.AddFullNameLogo();

                var optCreate  = cmd.Option("--create|-c", "Create new", CommandOptionType.NoValue);
                var optRemove  = cmd.Option("--remove|-r", "Delete", CommandOptionType.NoValue);
                var optVerbose = cmd.VerboseOption();

                cmd.OnExecute(() =>
                {
                    /// <summary>
                    /// Get name alias
                    /// </summary>
                    /// <param name="title"></param>
                    /// <param name="create"></param>
                    /// <returns></returns>
                    string GetName(string title, bool create)
                    {
                        Console.Out.WriteLine(title);
                        var name = " ";
                        while (true)
                        {
                            name = ReadLine.Read("Name: ", name.Trim());
                            if (string.IsNullOrWhiteSpace(name))
                            {
                                Console.Out.WriteLine($"Abort {title}");
                                break;
                            }

                            var exists = aliasManager.Exists(name);

                            if ((create && AliasDef.IsValid(name) && !exists) ||
                                (!create && exists))
                            {
                                break;
                            }
                            else
                            {
                                Console.Out.WriteLine($"Alias '{name}' already exists!");
                            }
                        }

                        return(name);
                    }

                    if (optCreate.HasValue())
                    {
                        //create
                        var name = GetName("Create alias (using comma to more name)", true);
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            return;
                        }

                        var description = ReadLine.Read("Description: ");

                        var command = ReadLine.Read("Command: ");
                        if (string.IsNullOrWhiteSpace(command))
                        {
                            Console.Out.WriteLine("Abort create alias");
                            return;
                        }

                        aliasManager.Create(name, description, command, false);

                        Console.Out.WriteLine($"Alias '{name}' created!");
                    }
                    else if (optRemove.HasValue())
                    {
                        //remove
                        var name = GetName("Remove alias", false);
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            return;
                        }
                        aliasManager.Remove(name);
                        Console.Out.WriteLine($"Alias '{name}' removed!");
                    }
                    else
                    {
                        Console.Out.Write(aliasManager.ToTable(optVerbose.HasValue()));
                    }
                });
            });
        }