whatsapp-api-webhook-server-java is a library for integration with WhatsApp messenger using the API service green-api.com. You should get a registration token and an account ID in your personal cabinet to use the library. There is a free developer account tariff.
The documentation for the REST API can be found at the link. The library is a wrapper for the REST API, so the documentation at the link above also applies.
To send a message or perform other Green API methods, the WhatsApp account in the phone app must be authorized. To authorize the account, go to your cabinet and scan the QR code using the WhatsApp app.
Update the system:
sudo apt update
sudo apt upgrade -y
Java 17 must be installed on the server. Инструкция по установке Java.
sudo apt install openjdk-17-jdk
Set up the firewall:
Allow the SSH connection:
sudo ufw allow ssh
Base rules:
sudo ufw default deny incoming
sudo ufw default allow outgoing
Allow HTTP and HTTPS connections:
sudo ufw allow http
sudo ufw allow https
Enable the firewall:
sudo ufw enable
Install the library into your server project. The library is available in the Maven central repository.
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-client-java', version: 'version'
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:
server:
port: 8080
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 |
For JAR file:
java -jar your_app.jar
If using Maven, run from the project directory:
./mvnw spring-boot:run
Licensed under Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) terms. Please see file LICENSE.