コード例 #1
0
        public override async Task <bool> Execute(Message message, TelegramBotClient botClient)
        {
            var endCommand = false;

            switch (_requestType)
            {
            case RequestType.Action:
                await botClient.ChooseInlineKeyboard(message.Chat.Id,
                                                     "Choose action",
                                                     new[] { "List", "Delete", "Create" });

                break;

            case RequestType.DeleteRequestName:
                if (_aliasManager.Exists(message.Text))
                {
                    _aliasManager.Remove(message.Text);
                }
                else
                {
                    await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id,
                                                                   $"Name not valid '{message.Text}'");
                }
                endCommand = true;
                break;

            case RequestType.CreateRequestName:
                _name = message.Text.Trim();
                if (_aliasManager.Exists(_name))
                {
                    await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id,
                                                                   $"Name not valid '{_name}'");

                    endCommand = true;
                }
                else
                {
                    await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id, "Description");

                    _requestType = RequestType.CreateRequestDescription;
                }
                break;

            case RequestType.CreateRequestDescription:
                _description = message.Text.Trim();
                await botClient.SendTextMessageAsyncNoKeyboard(message.Chat.Id, "Description");

                _requestType = RequestType.CreateRequestCommand;
                break;

            case RequestType.CreateRequestCommand:
                _aliasManager.Create(_name, _description, message.Text.Trim(), false);
                endCommand = true;
                break;

            default: break;
            }

            return(await Task.FromResult(endCommand));
        }
コード例 #2
0
        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()));
                    }
                });
            });
        }