{"id":13764845,"url":"https://github.com/AnyTimeTraveler/telegram-menu-library","last_synced_at":"2025-05-10T20:31:15.007Z","repository":{"id":54881422,"uuid":"220574788","full_name":"AnyTimeTraveler/telegram-menu-library","owner":"AnyTimeTraveler","description":"A library that makes creating menus much easier","archived":false,"fork":false,"pushed_at":"2023-01-04T00:30:37.000Z","size":1809,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-17T01:32:25.392Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AnyTimeTraveler.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-09T01:33:51.000Z","updated_at":"2024-04-21T18:22:30.000Z","dependencies_parsed_at":"2023-02-01T14:46:15.235Z","dependency_job_id":null,"html_url":"https://github.com/AnyTimeTraveler/telegram-menu-library","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnyTimeTraveler%2Ftelegram-menu-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnyTimeTraveler%2Ftelegram-menu-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnyTimeTraveler%2Ftelegram-menu-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnyTimeTraveler%2Ftelegram-menu-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AnyTimeTraveler","download_url":"https://codeload.github.com/AnyTimeTraveler/telegram-menu-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253480402,"owners_count":21915246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T16:00:30.273Z","updated_at":"2025-05-10T20:31:09.988Z","avatar_url":"https://github.com/AnyTimeTraveler.png","language":"Java","funding_links":[],"categories":["Bots"],"sub_categories":["Bot Libs"],"readme":"\n# Telegram Menu Library\n\n### A library for making menus for your telegram bots!\n\n![](media/telegram_media_library_demo.gif)\n\n\n## Get it from Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.simonscode\u003c/groupId\u003e\n    \u003cartifactId\u003etelegram-menu-library\u003c/artifactId\u003e\n    \u003cversion\u003e2.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Usage\n\n### 1. Create a menu:\n\n```java\nSimpleMenu myMenu = new SimpleMenu(\"Menu Title\");\n```\n\n### 2. Add buttons to it\n\nThere are three types of buttons:\n\n| Class          | Function              |\n|----------------|-----------------------|\n| CallbackButton | Running any Java code |\n| URLButton      | Go to URLs            |\n| GotoButton     | Go to other menus     |\n\n#### CallbackButton\n\nYou can create a `Callback` which takes a `CallbackFunction` as parameter.\nThe `CallbackFunction` can contain any Java code.\nIt will be executed when the button is pressed.\n\n```java\nmyMenu.addButton(new CallbackButton(\"Callback Button\", new Callback((bot, callbackQuery, parameters) -\u003e {\n    System.out.println(\"This code will execute when the button is clicked\");\n})));\n```\n\n#### GotoButton\n\nThis button will replace the message and the buttons below it with the message and buttons of the `myOtherMenu`.\nYou can use this to navigate between menus.\n\n```java\nmyMenu.addButton(new GotoButton(\"Back\", myOtherMenu));\n```\n\n#### URLButton\n\nA button that allows points to a Website.\n\n```java\nmyMenu.addButton(new URLButton(\"URL Button\", \"https://www.simonscode.org/\"));\n```\n\n### Button Placement\n\nBy default, all buttons are going to be put in the same row.\nIf you want to put a button in the row below, then you have to call `.nextRow()` before adding the button.\n\n```java\nmyMenu.addButton(new CallbackButton(\"Top Left\",new NullCallback()));\nmyMenu.addButton(new CallbackButton(\"Top Right\",new NullCallback()));\nmyMenu.nextRow();\nmyMenu.addButton(new CallbackButton(\"Bottom\",new NullCallback()));\n```\n\n### 3. Send the first menu to telegram\n\nYou can generate a new message from the menu and send it to any chat.\n\n```java\nSendMessage sendMessage = myMenu.generateSendMessage();\nsendMessage.setChatId(update.getMessage().getChatId());\nbot.execute(sendMessage);\n```\n\n### 4. Starting the bot\n\nBefore handling any update, you should call this function.\nIt is responsible for replying with menus and executing callbacks.\n\nThe function will return a boolean. If the update was handled, then it will be true.\n\n```java\n@Override\npublic void onUpdateReceived(Update update){\n    if (UpdateHook.onUpdateReceived(this,update)) {\n        // update has been taken care of\n    } else {\n        // update is a command or anything not relating to menus\n    }\n}\n```\n\n### 5. (optional) Removing the menu\n\nWhen the menu isn't needed anymore, it can be discarded like this:\n\n```java\nmyMenu.unregister(true);\n```\n\nTrue or false specifies, if the menus it points to should also be discarded.\n\nYou should only need to delete menus when you create new menus regularly.\n\n## A full example\n\nThe code is also in the file [ReadmeCode.java](src/test/java/org/simonscode/telegrammenulibrary/ReadmeCode.java)\n\n```java\nSimpleMenu firstMenu = new SimpleMenu(\"Example Menu Title\");\nSimpleMenu secondMenu = new SimpleMenu(\"Second Menu: Text that will be displayed in the telegram message\");\n\n// Add buttons to the first menu\nfirstMenu.addButton(new CallbackButton(\"Callback Button\", new Callback((bot, callbackQuery, parameters) -\u003e {\n    System.out.println(\"This code will execute when the button is clicked\");\n})));\n\n// Put the following buttons in the next row, otherwise buttons will be laid out horizontally\nfirstMenu.nextRow();\n\n// Buttons also allow you to go to other menus.\n// That way, you can create a tree of submenus. Check the RecipeExample for a nesting example.\nfirstMenu.addButton(new GotoButton(\"Go to Second Menu\", secondMenu));\n\n// You can also chain the functions together, since each returns the menu.\n\n// Laying the buttons out in this shape:\n//\n//  +-------+-------+\n//  |   1   |   2   |\n//  +-------+-------+\n//  |   3   |   4   |\n//  +-------+-------+\n//  |       5       |\n//  +---------------+\n\nsecondMenu\n        .addButton(new GotoButton(\"Back\",firstMenu))\n        .addButton(new GotoButton(\"Also Back\",firstMenu))\n        .nextRow()\n        .addButton(new GotoButton(\"Still Back\",firstMenu))\n        .addButton(new GotoButton(\"Again, back...\",firstMenu))\n        .nextRow()\n        .addButton(new CallbackButton(\"Send a message, replying to this message\", new Callback((bot, callbackQuery, parameters) -\u003e {\n            SendMessage reply = new SendMessage(callbackQuery.getMessage().getChatId().toString(), \"This is a reply message\");\n            reply.setReplyToMessageId(callbackQuery.getMessage().getMessageId());\n            bot.execute(reply);\n        })));\n\n\n// Run the test bot with the first menu.\nTestBot.startBotWithMenu(firstMenu);\n```\n\n![](media/readme_code.jpg)\n\n## Callback Parameters\n\nYou don't have to create a different callback for changing a parameter.\n\nYou can add the same callback with different parameters like this:\n```java\nmyMenu.addButton(new CallbackButton(\"Small\", TwoSubmenus::setMessageText, \"Small\"))\nmyMenu.addButton(new CallbackButton(\"buttons\", TwoSubmenus::setMessageText, \"buttons\"))\nmyMenu.addButton(new CallbackButton(\"in\", TwoSubmenus::setMessageText, \"in\"))\nmyMenu.addButton(new CallbackButton(\"one\", TwoSubmenus::setMessageText, \"one\"))\nmyMenu.addButton(new CallbackButton(\"row.\", TwoSubmenus::setMessageText, \"row.\"));\n```\n\nAll these buttons execute the same callback function `TwoSubmenus::setMessageText`.\n\nThe full example is in the file [TwoSubmenus.java](src/test/java/org/simonscode/telegrammenulibrary/TwoSubmenus.java)\n\n## Important info\n\nThe order that you create menus is important!\n\nIf you restart the bot and swap the order in which you construct the menus and callbacks,\nthen your old menus might not link to the right places anymore.\n\nThere is also the `HorizontalMenu` and `VerticalMenu`, which organize the buttons in a row and column respectively.\n\n## Examples\n\n - [ReadmeCode.java](src/test/java/org/simonscode/telegrammenulibrary/ReadmeCode.java) contains the example from above\n - [RecipeExample.java](src/test/java/org/simonscode/telegrammenulibrary/RecipeExample.java) shows how unregistering menus works\n - [TwoSubmenus.java](src/test/java/org/simonscode/telegrammenulibrary/TwoSubmenus.java) demonstrates two submenus with \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAnyTimeTraveler%2Ftelegram-menu-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAnyTimeTraveler%2Ftelegram-menu-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAnyTimeTraveler%2Ftelegram-menu-library/lists"}