How to run the web server#
Import#
Maven
<dependency>
<groupId>com.green-api</groupId>
<artifactId>whatsapp-api-webhook-server-java</artifactId>
<version>{{version}}</version>
</dependency>
Gradle
implementation group: 'com.green-api', name: 'whatsapp-api-webhook-server-java', version: '0.0.1'
Examples#
How to initialize an object#
Set server options in application.yml
. The WebhookToken attribute is optional and does not need to be assigned a value, but it must be in application.yml
. If you don't want to set a password, you can simply leave the webhookToken parameter without a value.
green-api:
webhookToken: 1a2b3c4d5e
server:
port: 8080
How to run the web server#
Applications will start listening the port immediately after running the main
method; for this, do not forget to add the @ComponentScan(basePackages = "com.greenapi.server")
annotation.
Link to example: WhatsappApiServerExample.java.
@SpringBootApplication
@ComponentScan(basePackages = "com.greenapi.server")
public class WhatsappApiServerExample {
public static void main(String[] args) {
SpringApplication.run(WhatsappApiServerExample.class, args);
}
}
The handler function class must implement the WebhookHandler
interface and be a bean named whatsappWebhookHandler
. To do this, set the @Component(value = "whatsappWebhookHandler")
annotation on the handler function class.
Link to example: WebhookHandlerExample.java.
@Component(value = "whatsappWebhookHandler")
public class WebhookHandlerExample implements WebhookHandler {
@SneakyThrows
@Override
public void handle(Notification notification) {
System.out.println("START " + notification);
Thread.sleep(20000);
System.out.println("END " + notification);
}
}
Webhook URL: {{your host}}/whatsapp/async/webhook
If you want your handle()
handler function to be executed asynchronously.
Webhook URL: {{your host}}/whatsapp/webhook
If you want your handle()
handler function to be called for each webhook sequentially in one thread.
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 | TextMessage |
TemplateMessageWebhook | TemplateMessage |
StickerMessageWebhook | StickerMessage |
ReactionMessageWebhook | ReactionMessage |
QuotedMessageWebhook | QuotedMessage |
PollUpdateMessageWebhook | PollUpdateMessage |
PollMessageWebhook | PollMessage |
LocationMessageWebhook | LocationMessage |
ListMessageWebhook | ListMessage |
GroupInviteMessageWebhook | GroupInviteMessage |
FileMessageWebhook | imageMessage, videoMessage, documentMessage, audioMessage |
ExtendedTextMessageWebhook | ExtendedTextMessage |
ButtonsMessageWebhook | ButtonsMessage |
ContactMessageWebhook | ContactMessage |
ContactsArrayMessageWebhook | ContactMessage |
TemplateButtonsReplyMessageWebhook | TemplateButtonsReplyMessage |
ButtonsResponseMessageWebhook | ButtonsResponseMessage |
ListResponseMessageWebhook | ListResponseMessage |
Running the application#
For JAR file:
java -jar your_app.jar
If using Maven, run from the project directory:
./mvnw spring-boot:run