[go: up one dir, main page]

Skip to content

How to import the library and configure your bot#

Installation#

You can import the library from central repository. Examples for Maven and Gradle:

Maven

<dependency>
     <groupId>com.green-api</groupId>
     <artifactId>whatsapp-chatbot-java</artifactId>
     <version>{{version}}</version>
</dependency>

Gradle

implementation group: 'com.green-api', name: 'whatsapp-chatbot-java', version: 'version'

How to initialize an object#

Once you have imported the library into your project, you need to configure your application. To do this, you will need to create your own configuration class and add the following beans to it:

Link to example - BotDefaultConfigExample.java.

@Configuration
public class BotDefaultConfigExample {

     @Bean
     public RestTemplate restTemplate() {
         return new RestTemplateBuilder().build();
     }

     @Bean
     public StateManager stateManager() {
         return new StateManagerHashMapImpl();
     }

     @Bean
     public BotFactory botFactory(RestTemplate restTemplate, StateManager stateManager) {
         return new BotFactory(restTemplate, stateManager);
     }
}

RestTemplate is a standard Spring class that allows you to send http requests. You can configure it yourself or use the default implementation as in the example above.

StateManager is a library class that is responsible for managing the bot's state. By default, session data is stored in a HashMap, but you can implement your own implementation of the StateManager interface. If you want to add some default values to the state, you can add them at the configuration stage for example like this:

@Configuration
public class BotDefaultConfigExample {
     @Bean
     public StateManager stateManager() {
         var stateData = new HashMap<String, Object>();
         stateData.put("defaultParameter", "value");

         return new StateManagerHashMapImpl(stateData);
     }
}

IMPORTANT: The "scene" parameter is reserved in stateData; it stores the current scene for each session. Since this parameter is used by the library to switch between scenes, it is not recommended to override it on configuration stage.

Next, you need to register the host to which the bot will send requests in application.yml or application.property:

If your instances start with 7103:

green-api:
   host: https://api.greenapi.com
   hostMedia: https://media.greenapi.com

If not:

green-api:
   host: https://api.green-api.com
   hostMedia: https://media.green-api.com

BotFactory is a library class that is responsible for configuring the bot object. Using this class and the createBot() method you can initialize a bot object:

Link to example - BotStarterClassExample.java.

@SpringBootApplication
public class BotStarterClassExample {

     public static void main(String[] args) {
         var context = SpringApplication.run(BotStarterClassExample.class, args);
         var botFactory = context.getBean(BotFactory.class);

         var bot = botFactory.createBot(
             "{{instanceId}}",
             "{{token}}",
             new HandlerExample(),
             new FullStartScene());
     }
}

The createBot() method has four parameters: instanceId and token must be taken from the parameters of your instance in console. handler and startScene are objects of your classes in which you will need to implement the logic of your bot.

handler is a class object that inherits from the abstract class BotHandler. You can override in it methods, which are responsible for processing webhooks about the state of the instance and device StateInstanceChanged and DeviceInfo. If you don't want to handle these types of notifications, simply don't override the methods, leave the class empty, or call similar methods of the superclass.

public class HandlerExample extends BotHandler {

    @Override
    public void processStateInstanceChanged(StateInstanceChanged stateInstanceChanged) {
        super.processStateInstanceChanged(stateInstanceChanged);
    }

    @Override
    public void processDeviceInfo(DeviceInfo deviceInfo) {
        super.processDeviceInfo(deviceInfo);
    }
}

startScene is the starting scene from which communication with the bot begins. Scene is a class object that is inherited from abstract class Scene. Inside the scenes, webhooks are processed and your business logic is executed. Your bot will consist of a set of scenes that are executed one after another in the sequence you specify. Only one scene can be active at a time per state. Sample scenes will be shown below.

How to set up an instance#

To start receiving incoming notifications, you need to configure your instance. Open console page via link. Select an instance from the list and click on it. Click Change. IN The Notifications categories include everything you need to receive.

List of examples#

Description Example link
How to initialize an object BotStarterClassExample.java
Scene "Hello" BaseStartScene.java
Scene "Echo" EchoStartScene.java
How to receive other types of notifications EventStartScene.java
How to filter incoming messages FiltersStartScene.java
How to handle notification body MediaStartScene.java
How to work with bot state state
Example of a ready-made chat-bot full