private void ValidateAndInit(MenuItemWithSubItems menu) { this.RegisterCommand(menu); foreach (MenuItemBase item in menu.SubItems) { if (item is MenuItemToMainMenu) { continue; } if (item is MenuItemBack) { continue; } if (item is MenuItemWithSubItems menuItemWithSubItems) { this.ValidateAndInit(menuItemWithSubItems); } else { this.RegisterCommand(item); } } }
private ReplyKeyboardMarkup FormatReplyKeyboardMarkup(MenuItemWithSubItems menu) { List <List <KeyboardButton> > lines = new List <List <KeyboardButton> >(); List <KeyboardButton> currentLine = new List <KeyboardButton>(); int number = 0; foreach (MenuItemBase item in menu.SubItems.Where(c => !(c is MenuItemToMainMenu) && !(c is MenuItemBack))) { currentLine.Add(new KeyboardButton(item.CommandText)); number++; if (number % menu.ColumnsCount == 0) { lines.Add(currentLine); currentLine = new List <KeyboardButton>(); } } if (currentLine.Count != 0) { lines.Add(currentLine); currentLine = new List <KeyboardButton>(); } bool containsGoToMain = menu.SubItems.Any(c => c is MenuItemToMainMenu); bool containsGoBack = menu.SubItems.Any(c => c is MenuItemBack); if (containsGoToMain && containsGoBack) { lines.Add(new List <KeyboardButton> { new KeyboardButton(menu.Parent.CommandText), new KeyboardButton(this.MainMenu.CommandText) }); } else { if (containsGoBack) { lines.Add(new List <KeyboardButton> { new KeyboardButton(menu.Parent.CommandText) }); } if (containsGoToMain) { lines.Add(new List <KeyboardButton> { new KeyboardButton(this.MainMenu.CommandText) }); } } ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup(); keyboardMarkup.OneTimeKeyboard = !menu.ShowAlways; keyboardMarkup.ResizeKeyboard = true; keyboardMarkup.Selective = true; keyboardMarkup.Keyboard = lines; return(keyboardMarkup); }
static void Main(string[] args) { MenuItemWithSubItems mainMenu = new MenuItemWithSubItems("/showMainMenu", "Welcome to main menu!", true, 3); mainMenu.AddSubItem(new MenuItemWithSubItems("/menuWithSubItems1", "Welcome to 'menuWithSubItems1' menu!") .AddSubItems(new MenuItemBase[] { new MenuItemWithAction("/sub1Action1", async(chat) => { await bot.SendTextMessageAsync(chat.Id, "sub1Action1 pressed"); }), new MenuItemWithAction("/sub1Action2", async(chat) => { await bot.SendTextMessageAsync(chat.Id, "sub1Action2 pressed"); }), new MenuItemWithSubItems("/subMenuWithSubItems1", "Welcome to 'subMenuWithSubItems1' menu!") .AddSubItem(new MenuItemWithAction("/subSub1Action1", async(chat) => { await bot.SendTextMessageAsync(chat.Id, "subSub1Action1 pressed"); })) .AddSubItem(new MenuItemBack()) .AddSubItem(new MenuItemToMainMenu()), new MenuItemToMainMenu(), })); mainMenu.AddSubItem(new MenuItemWithAction("/action2", async(chat) => { await bot.SendTextMessageAsync(chat.Id, "action2 pressed"); })); mainMenu.AddSubItem(new MenuItemWithAction("/action3", async(chat) => { await bot.SendTextMessageAsync(chat.Id, "action3 pressed"); })); mainMenu.AddSubItem(new MenuItemWithAction("/actionSpam", async(chat) => { for (int i = 0; i < 10; i++) { await bot.SendTextMessageAsync(chat.Id, $"Bot spam you #{i+1}"); } })); mainMenu.AddSubItem(new MenuItemWithAction("/HideMainMenu", (chat) => { menu.HideMainMenu(chat.Id, "Main menu closed. To show main menu send '/start'"); })); menu = new TelegramMenu(bot, mainMenu); bot.OnMessage += BotOnOnMessage; bot.StartReceiving(); Console.ReadLine(); bot.StopReceiving(); }
public TelegramMenu(TelegramBotClient botClient, MenuItemWithSubItems mainMenu) { this.BotClient = botClient ?? throw new ArgumentNullException(nameof(botClient)); this.MainMenu = mainMenu ?? throw new ArgumentNullException(nameof(mainMenu)); this.ValidateAndInit(mainMenu); }