Whatsapp business api SDK, written in java. This SDK implements the Official Whatsapp Cloud API and WhatsApp Business Management API. These allows you to:
- manage your WhatsApp Business Account assets, such as message templates and phone numbers;
- send messages to your contacts, such as simple text messages, messages with buttons...
1. Add the JitPack repository to your build file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
2. Add the following Maven dependency to your project's pom.xml
:
<dependency>
<groupId>com.github.Bindambc</groupId>
<artifactId>whatsapp-business-java-api</artifactId>
<version>0.0.3</version>
</dependency>
3. Install library into your Maven's local repository by running mvn install
Alternatively, you can clone this repository and run the examples.
There are two client classes that can be used to interact with the API:
WhatsappBusinessCloudApi
, a synchronous/blocking WhatsApp Business Platform Cloud API client;
Send and receive messages using a cloud-hosted version of the WhatsApp Business Platform. The Cloud API allows you to implement WhatsApp Business APIs without the cost of hosting of your own servers and also allows you to more easily scale your business messaging.
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestUtils.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
WhatsappBusinessManagementApi
, a synchronous/blocking WhatsApp Business Management API client;
The WhatsApp Business Management API allows you to programmatically manage your WhatsApp Business Account assets, such as message templates and phone numbers.
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);
WhatsappBusinessManagementApi whatsappBusinessCloudApi = factory.newBusinessManagementApi();
These can be instantiated through the corresponding factory method of WhatsappApiFactory
, by passing the token, which can be created following the instructions at whatsapp.
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestUtils.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildTextMessage(new TextMessage()//
.setBody(Formatter.bold("Hello world!") + "\nSome code here: \n" + Formatter.code("hello world code here"))//
.setPreviewUrl(false));
whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
Result:
Send a message with buttons (template):
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestUtils.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildTemplateMessage(//
new TemplateMessage()//
.setLanguage(new Language(com.whatsapp.api.domain.templates.Language.PT_BR))//
.setName("schedule_confirmation3")//
.addComponent(//
new Component(ComponentType.BODY)//
.addParameter(new TextParameter("Mauricio"))//
.addParameter(new TextParameter("04/11/2022"))//
.addParameter(new TextParameter("14:30")))//
);
whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
Result:
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestUtils.TOKEN);
WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();
var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildContactMessage(new ContactMessage()//
.addContacts(new ContactsItem()//
.addPhones(new PhonesItem()//
.setPhone(PHONE_NUMBER_1)//
.setType(AddressType.HOME))//
.setName(new Name()//
.setFormattedName("Mauricio Binda")//
.setFirstName("Mauricio"))//
));
whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
Result:
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);
WhatsappBusinessManagementApi whatsappBusinessCloudApi = factory.newBusinessManagementApi();
var template = new MessageTemplate();
template.setName("schedule_confirmation3")//
.setCategory(Category.TRANSACTIONAL)//
.setLanguage(Language.PT_BR)//
.addComponent(new HeaderComponent()//
.setText("Confirmação de Atendimento")//
.setFormat(HeaderFormat.TEXT))//
.addComponent(new BodyComponent()//
.setText("Olá " + Formatter.bold("{{1}}") + ", passando aqui para confirmar seu horário no dia " + Formatter.bold("{{2}}") + " as " + Formatter.bold("{{3}}h") + ".\nVocê confirma que comparecerá?")//
.setExample(new Example()//
.addBodyTextExamples("Maria", "04/11/2022", "13:30")//
))//
.addComponent(new ButtonComponent()//
.addButton(new QuickReplyButton("SIM"))//
.addButton(new QuickReplyButton("NÃO"))//
.addButton(new QuickReplyButton("REMARCAR")//
)
)//
.addComponent(new FooterComponent().setText("Utilize um dos botões abaixo para a confirmação"))
;
var response = whatsappBusinessCloudApi.createMessageTemplate(WABA_ID, template);
Webhooks are triggered when a customer performs an action or the status for a message a business sends a customer changes.
WebHook objects are mapped on WebHookPayload class
var objectMapper = new ObjectMapper();
var obj = objectMapper.readValue(fileContent, WebHookPayload.class);
You get a webhooks notification:
1- When a customer performs an action
- Sends a text message to the business
- Sends an image, video, audio, document, or sticker to the business
- Sends contact information to the business
- Sends location information to the business
- Clicks a reply button set up by the business
- Clicks a call-to-actions button on an Ad that Clicks to WhatsApp
- Clicks an item on a business' list
- Updates their profile information such as their phone number
- Asks for information about a specific product
- Orders products being sold by the business
2- When the status for a message received by a business changes (includes pricing information)
- delivered
- read
- sent
3- When WhatsApp Business Management API updates:
- Message Template Updates
- Phone Number Updates
- WABA Updates