{"id":21942928,"url":"https://github.com/green-api/whatsapp-chatbot-java","last_synced_at":"2025-08-04T03:37:25.055Z","repository":{"id":207658603,"uuid":"716067317","full_name":"green-api/whatsapp-chatbot-java","owner":"green-api","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-06T16:34:11.000Z","size":345,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-03T10:04:08.582Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/green-api.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-08T11:51:17.000Z","updated_at":"2025-03-10T06:32:07.000Z","dependencies_parsed_at":"2024-02-13T20:26:35.770Z","dependency_job_id":"4e048f9b-f0a9-4878-8790-3e33a8ab1ed1","html_url":"https://github.com/green-api/whatsapp-chatbot-java","commit_stats":null,"previous_names":["green-api/whatsapp-chatbot-java"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/green-api/whatsapp-chatbot-java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/green-api%2Fwhatsapp-chatbot-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/green-api%2Fwhatsapp-chatbot-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/green-api%2Fwhatsapp-chatbot-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/green-api%2Fwhatsapp-chatbot-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/green-api","download_url":"https://codeload.github.com/green-api/whatsapp-chatbot-java/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/green-api%2Fwhatsapp-chatbot-java/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268644972,"owners_count":24283407,"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","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-11-29T03:27:19.513Z","updated_at":"2025-08-04T03:37:25.028Z","avatar_url":"https://github.com/green-api.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# whatsapp-chatbot-java\n\n| Support links                                                                                                                                                                                                                                                                                                                                                                                                                           | Guides \u0026 News                                                                                                                                  |\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|\n| [![Support](https://img.shields.io/badge/support--mail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white)](mailto:support@greenapi.com) [![Support](https://img.shields.io/badge/Telegram-2CA5E0?style=for-the-badge\u0026logo=telegram\u0026logoColor=white)](https://t.me/greenapi_support_eng_bot) [![Support](https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white)](https://wa.me/77273122366) | [![Guides](https://img.shields.io/badge/YouTube-%23FF0000.svg?style=for-the-badge\u0026logo=YouTube\u0026logoColor=white)](https://www.youtube.com/@greenapi-en) [![News](https://img.shields.io/badge/Telegram-2CA5E0?style=for-the-badge\u0026logo=telegram\u0026logoColor=white)](https://t.me/green_api) [![News](https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white)](https://whatsapp.com/channel/0029VaLj6J4LNSa2B5Jx6s3h) |\n\n[Ссылка русскоязычную инструкцию](src/main/java/com/greenapi/chatbot/docs/README_RU.md)\n\nwhatsapp-chatbot-java - library for integration with WhatsApp messenger via API\nservice [green-api.com](https://greenapi.com/). To use the library, you need to obtain a registration token\nand account ID in [personal account](https://console.greenapi.com/). There is a free developer account plan.\n\n## API\n\nDocumentation for the REST API can be found at [link](https://greenapi.com/en/docs/api/). The library is a wrapper for the REST API,\ntherefore the documentation in the link above also applies to the library itself.\n\n## Authorization\n\nTo send a message or perform other GREEN API methods, the WhatsApp account in the phone app must be in\nauthorized state. To authorize your account, go to [personal account](https://console.green-api.com/) and\nscan the QR code using the WhatsApp application.\n\n## Installation\n\nMaven\n\n```\n\u003cdependency\u003e\n     \u003cgroupId\u003ecom.green-api\u003c/groupId\u003e\n     \u003cartifactId\u003ewhatsapp-chatbot-java\u003c/artifactId\u003e\n     \u003cversion\u003e{{version}}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nGradle\n\n```\nimplementation group: 'com.green-api', name: 'whatsapp-chatbot-java', version: 'version'\n```\n\n## Setup\n\nBefore launching the bot you should enable incoming notifications in instance settings by using \u003ca href=\"https://green-api.com/en/docs/api/account/SetSettings/\"\u003eSetSettings method\u003c/a\u003e.\n\n```\n\"incomingWebhook\": \"yes\",\n\"outgoingMessageWebhook\": \"yes\",\n\"outgoingAPIMessageWebhook\": \"yes\",\n```\n\n## Examples\n\n### How to initialize an object\n\nOnce you have imported the library into your project, you need to configure your application.\nTo do this, you will need to create your own configuration class and add the following beans to it:\n\n```java\n@Configuration\npublic class BotDefaultConfigExample {\n\n     @Bean\n     public RestTemplate restTemplate() {\n         return new RestTemplateBuilder().build();\n     }\n\n     @Bean\n     public StateManager stateManager() {\n         return new StateManagerHashMapImpl();\n     }\n\n     @Bean\n     public BotFactory botFactory(RestTemplate restTemplate, StateManager stateManager) {\n         return new BotFactory(restTemplate, stateManager);\n     }\n}\n```\n\n`RestTemplate` is a standard Spring class that allows you to send http requests.\nYou can configure it yourself or use the default implementation as in the example above.\n\n`StateManager` is a library class that is responsible for managing the bot's state.\nBy default, session data is stored in a HashMap, but you can implement your own implementation of the StateManager interface.\nIf you want to add some default values to the state, you can add them at the configuration stage\nfor example like this:\n\n```java\n\n@Configuration\npublic class BotDefaultConfigExample {\n     @Bean\n     public StateManager stateManager() {\n         var stateData = new HashMap\u003cString, Object\u003e();\n         stateData.put(\"defaultParameter\", \"value\");\n\n         return new StateManagerHashMapImpl(stateData);\n     }\n}\n```\n\n\u003e IMPORTANT: The \"scene\" parameter is reserved in stateData; it stores the current scene for each session.\n\u003e Since this parameter is used by the library to switch between scenes, it is not recommended to override it at the configuration stage.\n\nNext, you need to register the host to which the bot will send requests in application.yml or application.property:\n\nIf your instances start with 7103:\n```yaml\ngreen-api:\n   host: https://api.greenapi.com\n   hostMedia: https://media.greenapi.com\n```\nIf not:\n```yaml\ngreen-api:\n   host: https://api.green-api.com\n   hostMedia: https://media.green-api.com\n```\n\n`BotFactory` is a library class that is responsible for configuring the bot object.\nUsing this class and the `createBot()` method you can initialize a bot object:\n\n```java\n\n@SpringBootApplication\npublic class BotStarterClassExample {\n\n     public static void main(String[] args) {\n         var context = SpringApplication.run(BotStarterClassExample.class, args);\n         var botFactory = context.getBean(BotFactory.class);\n\n         var bot = botFactory.createBot(\n             \"{{instanceId}}\",\n             \"{{token}}\",\n             new HandlerExample());\n\n         bot.setStartScene(new FullStartScene());\n     }\n}\n```\n\nThe `createBot()` method has four parameters:\n`instanceId` and `token` must be taken from the parameters of your instance in your personal account.\n`handler` and `startScene` are objects of your classes in which you will need to implement the logic of your bot.\n\n`handler` is a class object that inherits from the abstract class `BotHandler`. You can override in it\nmethods that are responsible for processing webhooks about the state of the instance and device `StateInstanceChanged` and `DeviceInfo`.\nIf you don't want to handle these types of notifications, you can simply omit the handler in the constructor, and use the default implementation:\n\n```java\npublic class BotStarterClassExample {\n     public static void main(String[] args) {\n         var context = SpringApplication.run(BotStarterClassExample.class, args);\n         var botFactory = context.getBean(BotFactory.class);\n\n         var bot = botFactory.createBot(\n             \"{{instanceId}}\",\n             \"{{token}}\");\n\n         bot.setStartScene(new FullStartScene());\n\n         bot.startReceivingNotifications();\n     }\n}\n```\n\n`startScene` is the starting scene from which communication with the bot begins. It is set by the setter `bot.setStartScene(new YourStartScene());`\nA scene is a class object that inherits from the abstract class `Scene`. Inside the scenes, webhooks are processed and your business logic is executed.\nYour bot will consist of several scenes that are executed one after another in the sequence you specify.\nOnly one scene can be active at a time per state.\nSample scenes will be shown below.\n\n### How to set up an instance\n\nTo start receiving incoming notifications, you need to configure your instance. Open your personal account page\nvia [link](https://console.greenapi.com/). Select an instance from the list and click on it. Click **Change**. IN\nThe **Notifications** categories include everything you need to receive.\n\n### How to start receiving and responding to messages\n\nAfter creating your `BotHandler` successor, you need to create the first scene.\nTo do this, create a class that inherits from `Scene` and override the handler method of the desired type.\nMost often you will need `processIncomingMessage()` which handles webhooks about incoming messages.\nCome up with a clear name for the class; I recommend marking the starting scenes with the postfix `StartScene`.\n\nThe `processIncomingMessage()` method, like other handlers, returns the updated state of the bot.\nIf the state has not changed, it is enough to return the `currentState` object.\n\nLink to example: [BaseStartScene.java](src/main/java/com/greenapi/chatbot/examples/base/BaseStartScene.java).\n\n```java\npublic class BaseStartScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n         answerWithText(incomingMessage, \"Hello!\", \"message\");\n\n         return currentState;\n     }\n}\n```\n\nTo start the bot, you need to call the `bot.startReceivingNotifications();` function.\nIn this example, the bot only has one scene and will only respond to the `message`.\n\n```java\n@SpringBootApplication\npublic class BotStarterClassExample {\n\n    public static void main(String[] args) {\n        var context = SpringApplication.run(BotStarterClassExample.class, args);\n        var botFactory = context.getBean(BotFactory.class);\n\n        var bot = botFactory.createBot(\n            \"{{instanceId}}\",\n            \"{{token}}\",\n            new HandlerExample(),\n            new BaseStartScene());\n\n        bot.startReceivingNotifications();\n    }\n}\n```\n\n### How to receive other notifications and handle the notification body\n\nYou can receive not only incoming messages, but also outgoing ones, as well as their statuses and any other types of web hooks.\nTo do this, simply override the method you need.\n\nIn this scene, the bot receives all incoming messages and outputs them to the console. Other types of web hooks are ignored, their handlers\nadded for clarity.\n\nLink to example: [EventStartScene.java](src/main/java/com/greenapi/chatbot/examples/event/EventStartScene.java).\n\n```java\n@Log4j2\npublic class EventStartScene extends Scene {\n\n     // To process incoming messages.\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n         log.info(incomingMessage); // Output messages\n\n         return currentState;\n     }\n    \n     // To process outgoing messages\n     @Override\n     public State processOutgoingMessage(MessageWebhook outgoingMessage, State currentState) {\n         return super.processOutgoingMessage(outgoingMessage, currentState);\n     }\n    \n     // To process the statuses of outgoing messages\n     @Override\n     public State processOutgoingMessageStatus(OutgoingMessageStatus outgoingMessageStatus, State currentState) {\n         return super.processOutgoingMessageStatus(outgoingMessageStatus, currentState);\n     }\n\n     // To process incoming calls\n     @Override\n     public State processIncomingCall(IncomingCall incomingCall, State currentState) {\n         return super.processIncomingCall(incomingCall, currentState);\n     }\n\n     // To handle chat blocking\n     @Override\n     public State processIncomingBlock(IncomingBlock incomingBlock, State currentState) {\n         return super.processIncomingBlock(incomingBlock, currentState);\n     }\n}\n```\n\n### Receive webhooks via HTTP API\n\nYou can get incoming webhooks (messages, statuses) via HTTP API requests in the similar way as the rest of the Green API methods are implemented. Herewith, the chronological order of the webhooks following is guaranteed in the sequence in which they were received FIFO. All incoming webhooks are stored in the queue and are expected to be received within 24 hours.\n\nTo get incoming webhooks, you have to sequentially call two methods \u003ca href=\"https://green-api.com/en/docs/api/receiving/technology-http-api/ReceiveNotification/\"\u003eReceiveNotification\u003c/a\u003e and \u003ca href=\"https://green-api.com/en/docs/api/receiving/technology-http-api/DeleteNotofication/\"\u003eDeleteNotification\u003c/a\u003e. ReceiveNotification method receives an incoming webhook. DeleteNotification method confirms successful webhook receipt and processing. To learn more about the methods, refer to respective ReceiveNotification and DeleteNotification sections.\n\n### How to filter incoming messages\n\nFiltering by webhook type occurs automatically by overriding the necessary methods, as described in the paragraph above, but how to filter messages?\n\nSince each notification is automatically cast to a java object, you can filter messages by any field of the object yourself.\nA description of the structure of notification objects can be found at this link: [Documentation](https://greenapi.com/en/docs/api/receiving/notifications-format/type-webhook/)\nFor convenience, all java objects and fields are named similarly to the documentation:\n\n| Java object                           | Webhook's json object                                                                                                                                       |\n|---------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `TextMessageWebhook`                  | [TextMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/TextMessage/)                                                |\n| `TemplateMessageWebhook`              | [TemplateMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/TemplateMessage/)                                        |\n| `StickerMessageWebhook`               | [StickerMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/StickerMessage/)                                          |\n| `ReactionMessageWebhook`              | [ReactionMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ReactionMessage/)                                        |\n| `QuotedMessageWebhook`                | [QuotedMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/QuotedMessage/)                                            |\n| `PollUpdateMessageWebhook`            | [PollUpdateMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/PollUpdateMessage/)                                    |\n| `PollMessageWebhook`                  | [PollMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/PollMessage/)                                                |\n| `LocationMessageWebhook`              | [LocationMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/LocationMessage/)                                        |\n| `ListMessageWebhook`                  | [ListMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ListMessage/)                                                |\n| `GroupInviteMessageWebhook`           | [GroupInviteMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/GroupInviteMessage/)                                  |\n| `FileMessageWebhook`                  | [imageMessage, videoMessage, documentMessage, audioMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ImageMessage/) |\n| `ExtendedTextMessageWebhook`          | [ExtendedTextMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ExtendedTextMessage/)                                |\n| `ButtonsMessageWebhook`               | [ButtonsMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ButtonsMessage/)                                          |\n| `ContactMessageWebhook`               | [ContactMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ContactMessage/)                                          |\n| `ContactsArrayMessageWebhook`         | [ContactMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/incoming-message/ContactsArrayMessage/)                                    |\n| `TemplateButtonsReplyMessageWebhook`  | [TemplateButtonsReplyMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/selected-buttons/TemplateButtonsReplyMessage/)                |\n| `ButtonsResponseMessageWebhook`       | [ButtonsResponseMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/selected-buttons/ButtonsResponseMessage/)                          |\n| `ListResponseMessageWebhook`          | [ListResponseMessage](https://greenapi.com/en/docs/api/receiving/notifications-format/selected-buttons/ListResponseMessage/)                                |\n\nYou can independently check and convert the received notification to the type you need.\n\nLink to example: [MediaStartScene.java](src/main/java/com/greenapi/chatbot/examples/media/MediaStartScene.java).\n\n```java\npublic class MediaStartScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n         if (incomingMessage instanceof ContactMessageWebhook) {\n             answerWithText(incomingMessage, \"This is a contact message\");\n\n         } else if (incomingMessage instanceof LocationMessageWebhook) {\n             answerWithText(incomingMessage, \"This is location message\");\n\n         } else if (incomingMessage instanceof FileMessageWebhook) {\n             answerWithText(incomingMessage, \"This is a message with a file\");\n         }\n\n         return currentState;\n     }\n}\n```\n\nAlso, for convenience and to improve code readability, the methods of the `Scene` class are overloaded; they have built-in the most popular filters for the text of the incoming message.\nExamples of using methods of this class will be described below. If you want to make a condition without executing the methods of the `Scene` class,\nyou can use the methods of the `Filter` class of this library, which return a `boolean` value:\n\n| Filter Method                                                                         | Description                                                                        |\n|---------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|\n| `Filter.isSenderIdExpected(MessageWebhook messageWebhook, String expectedSenderId)`   | Returns `true` if `expectedSenderId` is equal to the sender ID in `messageWebhook` |\n| `Filter.isMessageTextRegex(MessageWebhook messageWebhook, Pattern regexPattern)`      | Returns `true` if `regexPattern` matches the text in `messageWebhook`              |\n| `Filter.isMessageTextExpected(MessageWebhook messageWebhook, String expectedMessage)` | Returns `true` if `expectedMessage` equals the text in `messageWebhook`            |\n\n#### Example\n\nIn this example, the bot will send a message and a file in response to the `rates` command.\nSending a message using the `answerWithText()` method is triggered only in response to the `rates` command due to the fact that the third parameter to the `answerWithText` method\nthe string `\"rates\"` is passed. This mechanism is implemented in all methods of the `Scene` class. If you want to reply to all messages, without a filter, simply omit the third parameter.\nYou can also pass a regex pattern as the third parameter instead of a string.\n\nSending a file using the `answerWithUploadFile()` method is triggered only in response to the `rates` command due to the fact that the method is located in the `if` block in the condition of which the method is executed\n`Filter.isMessageTextExpected(incomingMessage, \"rates\")`.\n\nLink to example: [FiltersStartScene.java](src/main/java/com/greenapi/chatbot/examples/filters/FiltersStartScene.java).\n\n```java\npublic class FiltersStartScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n\n         answerWithText(incomingMessage, \"You see this because you wrote \\\"rates\\\"\", \"rates\"); //filtering by overloaded method\n\n         if (Filter.isMessageTextExpected(incomingMessage, \"rates\")) { //filtering using the Filter class method\n             answerWithUploadFile(incomingMessage, new File(\"src/main/resources/data/rates.png\"));\n         }\n\n         return currentState;\n     }\n}\n```\n\n### How to manage scenes\n\nThere are special methods in the `Scene` class to manage scenes. They must be executed after the `return` keyword inside the active scene.\nThese methods change the `scene` parameter in the chat state, the next message from the selected chat will go into a new scene.\n\n| Scene Methods                                            | Description                                            |\n|----------------------------------------------------------|--------------------------------------------------------|\n| `activateNextScene(State currentState, Scene nextScene)` | Activates the next `nextScene` for the current chat.   |\n| `activateStartScene(State currentState)`                 | Activates the start scene for the current user.        |\n\nA complete list of methods available on the stage is described at the end of the file.\n\n### How to manage user state\n\nTo control the user's state, simply make changes to the `currentState` object inside the scene and return it using `return`.\nAt the end of each scene, the state is automatically updated.\n\nTo manage state directly, you need to use the `stateManager` object, which is an instance of your implementation of the `StateManager` interface.\nThis object is available in any scene, as it is one of its fields.\nThe manager has methods that perform basic crud operations on the state. You also have the option to save chat data in its state.\n\n| StateManager Methods | Description                                                                                  |\n|----------------------|----------------------------------------------------------------------------------------------|\n| `get()`              | Returns the state of the selected chat.                                                      |\n| `create()`           | Creates a new state for chat.                                                                |\n| `update()`           | Updates the state (the method is not present in the default implementation of the interface) |\n| `delete()`           | Removes the user's state.                                                                    |\n| `getStateData()`     | Returns state data                                                                           |\n| `setStateData()`     | If the state exists, overwrites the state data                                               |\n| `updateStateData()`  | If the state exists, updates the state data (put)                                            |\n| `deleteStateData()`  | If the state exists, then clears the state data (sets default values)                        |\n\n\u003eThe state identifier is the chat ID (the chatId field, not to be confused with senderId).\n\nAs an example, a bot was created to register a user.\n\nLink to example: [state](src/main/java/com/greenapi/chatbot/examples/state).\n\n```java\npublic class StateStartScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n         answerWithText(incomingMessage, \"Hello. Tell me your username.\");\n\n         return activateNextScene(currentState, new InputUsernameScene());\n     }\n}\n```\n\n```java\npublic class InputUsernameScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n         var stateData = currentState.getData();\n\n         var username = getText(incomingMessage);\n         if (username != null \u0026\u0026 username.length() \u003c= 20 \u0026\u0026 username.length() \u003e= 5) {\n             stateData.put(\"username\", username);\n             currentState.setData(stateData);\n\n             answerWithText(incomingMessage, \"Please, send password\");\n\n             activateNextScene(currentState, new InputPasswordScene());\n\n         } else {\n             answerWithText(incomingMessage, \"invalid username\");\n         }\n\n         return currentState;\n     }\n}\n```\n\n```java\npublic class InputPasswordScene extends Scene {\n    @Override\n    public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n        var stateData = currentState.getData();\n\n        var password = getText(incomingMessage);\n        if (password != null \u0026\u0026 password.length() \u003c= 20 \u0026\u0026 password.length() \u003e= 8) {\n            stateData.put(\"password\", password);\n            currentState.setData(stateData);\n\n            answerWithText(incomingMessage, String.format(\"\"\"\n                Successful account creation.\n                Your username: %s.\n                Your password: %s.\n                \"\"\", stateData.get(\"username\"), password));\n\n            return activateStartScene(currentState);\n\n        } else {\n            answerWithText(incomingMessage, \"invalid password\");\n        }\n\n        return currentState;\n    }\n}\n```\n\n### Bot example\n\nAs an example, a bot was created that demonstrates sending methods of the Scene class.\n\nThe bot is started with the command - /start\nAfter launching, you need to select a method from the menu, and the bot will execute it.\n\nLink to example: [full](src/main/java/com/greenapi/chatbot/examples/full).\n\nThe start scene waits for the `/start` command, after which it sends the menu and activates the next `ChooseScene`.\n\n```java\npublic class FullStartScene extends Scene {\n     @Override\n     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n\n         var greetingText =\n             \"\"\"\n                 Please, choose Scene's method and I execute it.\n                            \n                 1. answerWithText();\n                 2. answerWithUrlFile();\n                 3. answerWithPoll();\n                 4. answerWithLocation();\n                 5. answerWithContact();\n                 6. Exit.\n                 \"\"\";\n\n         var resp = answerWithText(incomingMessage, greetingText, \"/start\");\n         if (resp == null) {\n             var sendMessageResp = answerWithText(incomingMessage, \"Hi, this is test bot.\\nPlease, send me a command - /start\");\n\n             return currentState;\n         }\n\n         return activateNextScene(currentState, new ChooseScene());\n     }\n}\n```\n\nThe second scene waits for the user's response and starts the execution of the desired method.\nIf `answerWithUrlFile()` is selected, the `InputLinkScene` scene is launched\n\n```java\npublic class ChooseScene extends Scene {\n\n    @Override\n    public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n        var text = getText(incomingMessage);\n        if (text == null) {\n            answerWithText(incomingMessage, \"PLease send a text message!\");\n            return currentState;\n        }\n\n        switch (text) {\n            case \"1\" -\u003e {\n                answerWithText(incomingMessage, \"Hi! This is answerWithText!\");\n                return currentState;\n            }\n            case \"2\" -\u003e {\n                answerWithText(incomingMessage, \"Send me the link on File:\");\n                return activateNextScene(currentState, new InputLinkScene());\n            }\n            case \"3\" -\u003e {\n                var options = new ArrayList\u003cOption\u003e();\n                options.add(new Option(\"Red\"));\n                options.add(new Option(\"Blue\"));\n                options.add(new Option(\"Green\"));\n                options.add(new Option(\"Pink\"));\n                answerWithPoll(incomingMessage, \"choose color\", options, false);\n                return currentState;\n            }\n            case \"4\" -\u003e {\n                answerWithLocation(incomingMessage, \"Home\", \"Cdad. de La Paz 2969, Buenos Aires\", -34.5553558, -58.4642510);\n                return currentState;\n            }\n            case \"5\" -\u003e {\n                var contact = Contact.builder()\n                    .firstName(\"first\")\n                    .lastName(\"last\")\n                    .middleName(\"middle\")\n                    .company(\"Green API\")\n                    .phoneContact(11111111111L)\n                    .build();\n                answerWithContact(incomingMessage, contact);\n                return currentState;\n            }\n            case \"6\" -\u003e {\n                answerWithText(incomingMessage, \"Goodbye!\");\n                return activateStartScene(currentState);\n            }\n            default -\u003e {\n                answerWithText(incomingMessage, \"Please send numbers - 1, 2, 3, 4, 5 or 6\");\n                return currentState;\n            }\n        }\n    }\n}\n```\n\nThis scene waits for the user to send a link to the file.\nIf the link is correct, sends the file and returns to the `ChooseScene`.\n\n```java\npublic class InputLinkScene extends Scene {\n    @Override\n    public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {\n        var fileUrl = getText(incomingMessage);\n\n        if (fileUrl != null) {\n            try {\n                answerWithUrlFile(incomingMessage, \"This is your file!\", fileUrl, \"testFile\");\n            } catch (Exception e) {\n                answerWithText(incomingMessage, \"invalid link! Please send me a link, for example https://greenapi.com\");\n            }\n        } else {\n            answerWithText(incomingMessage, \"Please send me a link!\");\n\n            return currentState;\n        }\n\n        return activateNextScene(currentState, new ChooseScene());\n    }\n}\n```\n\n### List of Scene class methods\n\n| Scene's Methods                                                                                                             | Description                                                                                    |\n|-----------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|\n| `activateNextScene(State currentState, Scene nextScene)`                                                                    | Activates the next `nextScene` for the current chat.                                           |\n| `activateStartScene(State currentState)`                                                                                    | Activates the start scene for the current user.                                                |\n| `getText(MessageWebhook messageWebhook)`                                                                                    | Returns the text of the message if it is text, otherwise returns `null`                        |\n| `answerWithText(MessageWebhook messageWebhook, String text)`                                                                | Replies with text to an incoming message.                                                      |\n| `answerWithUploadFile(MessageWebhook messageWebhook, String caption, File file)`                                            | Downloads and sends a file in response to an incoming message. `Caption` is an optional field. |\n| `answerWithUrlFile(MessageWebhook messageWebhook, String caption, String url, String fileName)`                             | Sends a file from a url in response to an incoming message. `Caption` is an optional field.    |\n| `answerWithLocation(MessageWebhook messageWebhook, String nameLocation, String address, Double latitude, Double longitude)` | Sends geolocation in response to an incoming message.                                          |\n| `answerWithPoll(MessageWebhook messageWebhook, String message, List\u003cOption\u003e options, Boolean multipleAnswers)`              | Sends a poll in response to an incoming message.                                               |\n| `answerWithContact(MessageWebhook messageWebhook, Contact contact)`                                                         | Sends a contact in response to an incoming message.                                            |\n\n\u003e In the overloaded version, message response methods may contain additional parameters `expectedMessage` and `regexPattern`.\n\u003e If the text of the incoming message matches the condition, the method will be executed and return the method response according to the documentation, if not, then the method will return `null`.\n\n## Documentation on service methods\n\n[Documentation on service methods](https://greenapi.com/en/docs/api/)\n\n## License\n\nLicensed under [\nCreative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0)\n](https://creativecommons.org/licenses/by-nd/4.0/).\n[LICENSE](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreen-api%2Fwhatsapp-chatbot-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreen-api%2Fwhatsapp-chatbot-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreen-api%2Fwhatsapp-chatbot-java/lists"}