Skip to content

How to start receiving and responding to messages#

Installation#

Before you begin, you need to install the library and configure the bot; this process is described in detail here: How to import the library and configure your bot.

How to start receiving and responding to messages#

After you have installed the library and configured the bot, you need to create the first scene. To do this, create a class that inherits from Scene and override the handler method of the desired type. Most often you will need processIncomingMessage() which handles webhooks about incoming messages. Come up with a clear name for the class; I recommend marking starting scenes with the postfix StartScene.

The processIncomingMessage() method, like other handlers, returns the updated state of the bot. If the state has not changed, it is enough to return the currentState object.

Link to example: BaseStartScene.java.

public class BaseStartScene extends Scene {
     @Override
     public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {
         answerWithText(incomingMessage, "Hello!", "message");

         return currentState;
     }
}

To start the bot, you need to call the bot.startReceivingNotifications(); function. In this example, the bot only has one scene and will only respond to the message.

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 BaseStartScene());

         bot.startReceivingNotifications();
     }
}

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