This SDK for Java was created based on the ZenviaAPI and Zenvia CPaaS SDK for Node.js.
- Text message content
- File message content
- Template message content
- Subscription handling
- Logging support
You need to create an API token in the Zenvia API console.
If using maven, add to your pom this SDK:
<dependency>
<groupId>com.zenvia</groupId>
<artifactId>zenvia-api-sdk-client-apache</artifactId>
<version>1.1.0</version>
</dependency>If you are using Gradle instead, add to your build.gradlethis SDK:
dependencies {
implementation group: 'com.zenvia', name: 'zenvia-api-sdk-client-apache', version: '1.1.0'
}You can also use the one that includes Spring Boot:
<dependency>
<groupId>com.zenvia</groupId>
<artifactId>zenvia-api-sdk-client-spring</artifactId>
<version>1.1.0</version>
</dependency>or in build.gradle:
dependencies {
implementation group: 'com.zenvia', name: 'zenvia-api-sdk-client-spring', version: '1.1.0'
}Add to your pom this SDK for the WebhookController over Jersey framework:
<dependency>
<groupId>com.zenvia</groupId>
<artifactId>zenvia-api-sdk-webhook-jersey</artifactId>
<version>1.1.0</version>
</dependency>or in build.gradle:
dependencies {
implementation group: 'com.zenvia', name: 'zenvia-api-sdk-webhook-jersey', version: '1.1.0'
}or the one for the WebhookController over Spring Web MVC framework:
<dependency>
<groupId>com.zenvia</groupId>
<artifactId>zenvia-api-sdk-webhook-webmvc</artifactId>
<version>1.1.0</version>
</dependency>or in build.gradle
dependencies {
implementation group: 'com.zenvia', name: 'zenvia-api-sdk-webhook-webmvc', version: '1.1.0'
}For those using Spring Boot, consider ours Starters to easily configure the Zenvia SDK in your project.
importcom.zenvia.api.sdk.contents.*;
importcom.zenvia.api.sdk.messages.Message;
importcom.zenvia.api.sdk.client.Channel;
importcom.zenvia.api.sdk.client.errors.ErrorResponse;
importcom.zenvia.api.sdk.client.exceptions.ApiException;
importcom.zenvia.api.sdk.client.exceptions.UnsuccessfulRequestException;
// When using zenvia-api-sdk-client-apacheimportcom.zenvia.api.sdk.client.apache.Client;
// When using zenvia-api-sdk-client-springimportcom.zenvia.api.sdk.client.spring.Client;
// Initialization with your API token (x-api-token)Clientclient = newClient("YOUR_API_TOKEN");
// Choosing the channelChannelwhatsapp = client.getChannel("whatsapp");
// Creating a text contentContentcontent = newTextContent("some text message here");
try {
Messageresponse = whatsapp.sendMessage("sender-identifier", "recipient-identifier", content);
// do something here
} catch(UnsuccessfulRequestExceptionexception) {
ErrorResponseresponse = exception.body;
// handle error here
} catch(ApiExceptionexception) {
// handle error here
}Use the sendMessage method to send text (TextContent), file (FileContent) or template (TemplateContent) messages to your contacts.
Clientclient = newClient("YOUR_API_TOKEN");
Channelsms = client.getChannel("sms");
TextContentcontent = newTextContent("some text message");
try {
Messageresponse = sms.sendMessage("sender-identifier", "recipient-identifier", content);
} catch(UnsuccessfulRequestExceptionexception) {
ErrorResponseresponse = exception.body;
}The response will be an Message object when successful, or an ErrorResponse object will be
available on the exception.
The content types can be:
| Name | Description |
|---|---|
| TextContent | Used to send text messages to your customer. |
| FileContent | Used to send file messages to your customer. |
| TemplateContent | Used to send template messages to your customer. |
The content support by channel is described below.
| Channel | TextContent | FileContent | TemplateContent |
|---|---|---|---|
| SMS | X | ||
| X | X | X | |
| X | X |
Use the WebhookController class to create your webhook to receive message and message status events. The default port is 8080.
If you inform the client, url, and channel fields, a subscription will be created if it does not exist for these configurations.
In the messageEventHandler field you will receive the message events and in the messageStatusEventHandler field you will receive the message status events.
Clientclient = newClient("YOUR_API_TOKEN");
WebhookControllerwebhook = newWebhookController(
newResourceConfig(), //or an instance of RequestMappingHandlerMapping when using Spring Wev MVC version
(MessageEventmessageEvent) -> {
System.out.println("Message event:" + messageEvent);
},
(MessageStatusEventmessageStatusEvent) -> {
System.out.println("Message status event:" + messageStatusEvent);
},
client,
"https://my-webhook.company.com",
ChannelType.whatsapp
);
webhook.init();Pull requests are always welcome!
Please see the Contributors' Guide for more information on contributing.