Bot example#
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.
Bot example#
As an example, a bot was created that demonstrates sending methods of the Scene class. It consists of 3 scenes: FullStartScene, ChooseScene and InputLinkScene.
Link to example: full. The start scene FullStartScene waits for the /start command, after which it sends the menu and activates the next scene ChooseScene.
public class FullStartScene extends Scene {
@Override
public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {
var greetingText =
"""
Please, choose Scene's method and I execute it.
1. answerWithText();
2. answerWithUrlFile();
3. answerWithPoll();
4. answerWithLocation();
5. answerWithContact();
6. Exit.
""";
var resp = answerWithText(incomingMessage, greetingText, "/start");
if (resp == null) {
var sendMessageResp = answerWithText(incomingMessage, "Hi, this is test bot.\nPlease, send me a command - /start");
return currentState;
}
return activateNextScene(currentState, new ChooseScene());
}
}
The second scene waits for the user's response and starts the execution of the desired method. If you select 2. answerWithUrlFile(), the InputLinkScene scene is launched
public class ChooseScene extends Scene {
@Override
public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {
if (Filter.isMessageTextExpected(incomingMessage, "1")) {
answerWithText(incomingMessage, "Hi! This is answerWithText!");
return currentState;
} else if (Filter.isMessageTextExpected(incomingMessage, "2")) {
answerWithText(incomingMessage, "Send me the link on File:");
return activateNextScene(currentState, new InputLinkScene());
} else if (Filter.isMessageTextExpected(incomingMessage, "3")) {
var options = new ArrayList<Option>();
options.add(new Option("Red"));
options.add(new Option("Blue"));
options.add(new Option("Green"));
options.add(new Option("Pink"));
answerWithPoll(incomingMessage, "choose color", options, false);
return currentState;
} else if (Filter.isMessageTextExpected(incomingMessage, "4")) {
answerWithLocation(incomingMessage, "Home", "Cdad. de La Paz 2969, Buenos Aires", -34.5553558, -58.4642510);
return currentState;
} else if (Filter.isMessageTextExpected(incomingMessage, "5")) {
var contact = Contact.builder()
.firstName("first")
.lastName("last")
.middleName("middle")
.company("GREEN-API")
.phoneContact(11111111111L)
.build();
answerWithContact(incomingMessage, contact);
return currentState;
} else if (Filter.isMessageTextExpected(incomingMessage, "6")) {
answerWithText(incomingMessage, "Goodbye!");
return activateStartScene(currentState);
}
answerWithText(incomingMessage, "Please send numbers - 1, 2, 3, 4, 5 or 6");
return currentState;
}
}
This scene waits for the user to send a link to the file. If the link is correct, sends the file and returns to the ChooseScene.
public class InputLinkScene extends Scene {
@Override
public State processIncomingMessage(MessageWebhook incomingMessage, State currentState) {
var fileUrl = getText(incomingMessage);
if (fileUrl != null) {
try {
answerWithUrlFile(incomingMessage, "This is your file!", fileUrl, "testFile");
} catch (Exception e) {
answerWithText(incomingMessage, "invalid link! Please send me a link, for example https://greenapi.com");
}
} else {
answerWithText(incomingMessage, "Please send me a link!");
return currentState;
}
return activateNextScene(currentState, new ChooseScene());
}
}
List of Scene class methods#
Methods of the Scene class | Description |
|---|---|
activateNextScene(State currentState, Scene nextScene) | Activates the next nextScene for the current chat. |
activateStartScene(State currentState) | Activates the start scene for the current user. |
getText(MessageWebhook messageWebhook) | Returns the text of the message if it is text, otherwise returns null |
answerWithText(MessageWebhook messageWebhook, String text) | Replies with text to an incoming message. |
answerWithUploadFile(MessageWebhook messageWebhook, String caption, File file) | Downloads and sends a file in response to an incoming message. Caption is an optional field. |
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. |
answerWithLocation(MessageWebhook messageWebhook, String nameLocation, String address, Double latitude, Double longitude) | Sends geolocation in response to an incoming message. |
answerWithPoll(MessageWebhook messageWebhook, String message, List<Option> options, Boolean multipleAnswers) | Sends a poll in response to an incoming message. |
answerWithContact(MessageWebhook messageWebhook, Contact contact) | Sends a contact in response to an incoming message. |
In the overloaded version, message response methods may contain additional
expectedMessageparameters andregexPattern. 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, the method will returnnull.
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 |