Skip to content

How to process incoming notifications#





implementation group: '', name: 'whatsapp-api-client-java', version: 'version'


How to initialize an object#

You can configure your bean, use application.yml, or instantiate an object via the constructor.

Via configuration:

public class GreenApiConf {

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

    public GreenApi greenApi(RestTemplate restTemplate) {
        return new GreenApi(

Via application.yml:

To use a ready-made bean that is created based on application.yml parameters, specify the parameters of your instance in the application.yml file as follows:

   instanceId: {{yourInstance}}
   token: {{yourToken}}

Make sure you have a RestTemplate bean with your configuration, like this:

public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){

And add "com.greenapi.client" to the base scanning packages using the @ComponentScan annotation:

@ComponentScan(basePackages = {"com.greenapi.client", "com.example"})
public class Application {

    public static void main(String[] args) {
        var context =, args);

Via constructor:

var restTemplate = new RestTemplateBuilder().build();

var greenApi1 = new GreenApi(

var greenApi2 = new GreenApi(

How to receive incoming notifications#

To start receiving notifications, you need to pass a handler function to webhookConsumer.start(). Handler function must implement the WebhookHandler interface. When a new notification is received, your handler function will be completed. To stop receiving notifications, you need to call the webhookConsumer.stop() function.

WebhookConsumer is a class responsible for processing messages; for its correct functioning you need GreenApi and NotificationMapper objects. You can pass them to it through beans or through a constructor.

NotificationMapper is a bean responsible for converting a JSON object into a java object. For this he uses ObjectMapper from the com.fasterxml.jackson library which should be available as a bean in the configuration or set via the constructor.

WebhookHandler is an interface. You can write any class to handle notifications, just implement the interface and do your logic in the handle() method or use a lambda expression.

public interface WebhookHandler {
    void handle(Notification notification);

Link to example:

public class WebhookExample {
    public static void main(String[] args) {
        var context =, args);

        var webhookConsumer = (WebhookConsumer) context.getBean("webhookConsumer");
        webhookConsumer.start(notification -> System.out.println("New webhook received: " + notification));

Since each notification is automatically cast to a java object, you can filter the notification by any field yourself. A description of the structure of notification objects can be found at this link: Documentation For convenience, all types of hooks and messages are named similarly to the documentation:

Java object Webhook's json object
TextMessageWebhook.class TextMessage
TemplateMessageWebhook.class TemplateMessage
StickerMessageWebhook.class StickerMessage
ReactionMessageWebhook.class ReactionMessage
QuotedMessageWebhook.class QuotedMessage
PollUpdateMessageWebhook.class PollUpdateMessage
PollMessageWebhook.class PollMessage
LocationMessageWebhook.class LocationMessage
ListMessageWebhook.class ListMessage
GroupInviteMessageWebhook.class GroupInviteMessage
FileMessageWebhook.class imageMessage, videoMessage, documentMessage, audioMessage
ExtendedTextMessageWebhook.class ExtendedTextMessage
ButtonsMessageWebhook.class ButtonsMessage
ContactMessageWebhook.class ContactMessage
ContactsArrayMessageWebhook.class ContactMessage
TemplateButtonsReplyMessageWebhook.class TemplateButtonsReplyMessage
ButtonsResponseMessageWebhook.class ButtonsResponseMessage
ListResponseMessageWebhook.class ListResponseMessage

Example's list#

Description Link to example
How to send message
How to create a group and send message
How to send a file by uploading from the disk
How to send a file by URL
How to send a file by UploadFile + SendFileByUrl
How to send poll
How to receive incoming notifications