Skip to content

[Ming] Support the whatsapp send Media Carousel Template message #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
45 changes: 45 additions & 0 deletions src/main/java/com/whatsapp/api/domain/messages/CarouselCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.whatsapp.api.domain.messages;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

/**
* The carousel card
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CarouselCard {

@JsonProperty("card_index")
private Integer cardIndex;

@JsonProperty("components")
private List<Component<?>> components;

public CarouselCard(Integer cardIndex) {
this.cardIndex = cardIndex;
}

public CarouselCard addComponent(Component<?> component) {
if (this.components == null) {
this.components = new ArrayList<>();
}
this.components.add(component);
return this;
}

public CarouselCard setComponents(List<Component<?>> components) {
this.components = components;
return this;
}

public Integer getCardIndex() {
return cardIndex;
}

public List<Component<?>> getComponents() {
return components;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.whatsapp.api.domain.messages;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.messages.type.ComponentType;

import java.util.ArrayList;
import java.util.List;


/**
* The type Carousel component, to send template messages
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates/media-card-carousel-templates">Api reference</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CarouselComponent extends Component<CarouselComponent> {

@JsonProperty("cards")
public List<CarouselCard> cards;

/**
* Instantiates a new Carousel component, to send template messages
*/
public CarouselComponent() {
super(ComponentType.CAROUSEL);
}

public CarouselComponent setCards(List<CarouselCard> cards) {
this.cards = cards;
return this;
}

public CarouselComponent addCard(CarouselCard card) {
if (this.cards == null) {
this.cards = new ArrayList<>();
}
this.cards.add(card);
return this;
}

public List<CarouselCard> getCards() {
return cards;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = ButtonComponent.class, name = "button"), //
@JsonSubTypes.Type(value = HeaderComponent.class, name = "header"), //
@JsonSubTypes.Type(value = BodyComponent.class, name = "body")})//
@JsonSubTypes.Type(value = BodyComponent.class, name = "body"),
@JsonSubTypes.Type(value = CarouselComponent.class, name = "carousel")})//
public abstract class Component<T extends Component<T>> {
@JsonProperty("type")
private final ComponentType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public enum ComponentType {
/**
* Button component type.
*/
BUTTON("button");
BUTTON("button"),

/**
* Carousel component type.
*/
CAROUSEL("carousel");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.whatsapp.api.examples;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whatsapp.api.TestConstants;
import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.messages.*;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.type.ButtonSubType;
import com.whatsapp.api.domain.templates.type.LanguageType;
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;

import static com.whatsapp.api.TestConstants.PHONE_NUMBER_1;
import static com.whatsapp.api.TestConstants.PHONE_NUMBER_ID;

public class SendCarouselTemplateMessageExample {
public static void main(String[] args) throws JsonProcessingException {
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);

WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();

var message = MessageBuilder.builder()//
.setTo(PHONE_NUMBER_1)//
.buildTemplateMessage(//
new TemplateMessage()//
.setLanguage(new Language(LanguageType.EN))//
.setName("carousel3_tour_1")//
.addComponent(new CarouselComponent()
.addCard(new CarouselCard(0)
.addComponent(
new HeaderComponent().addParameter(
new ImageParameter().setImage(new Image().setLink("https://ik.imgkit.net/3vlqs5axxjf/TW-Asia/ik-seo/uploadedImages/Industry/Destinations(1)/AdobeStock_302223688_Editorial_Use_Only/Hear-Singapore-roar-with-its-2023-tourism-performa.jpeg"))
))
.addComponent(
new BodyComponent().addParameter(new TextParameter("Tokyo"))
)
.addComponent(
new ButtonComponent().setIndex(1).setSubType(ButtonSubType.URL).addParameter(new TextParameter("url_123"))
)
)
.addCard(new CarouselCard(1)
.addComponent(
new HeaderComponent().addParameter(
new ImageParameter().setImage(new Image().setLink("https://ik.imgkit.net/3vlqs5axxjf/TW-Asia/ik-seo/uploadedImages/Industry/Destinations(1)/AdobeStock_302223688_Editorial_Use_Only/Hear-Singapore-roar-with-its-2023-tourism-performa.jpeg"))
))
.addComponent(
new BodyComponent().addParameter(new TextParameter("China"))
)
.addComponent(
new ButtonComponent().setIndex(1).setSubType(ButtonSubType.URL).addParameter(new TextParameter("url_234"))
)
)
.addCard(new CarouselCard(2)
.addComponent(
new HeaderComponent().addParameter(
new ImageParameter().setImage(new Image().setLink("https://ik.imgkit.net/3vlqs5axxjf/TW-Asia/ik-seo/uploadedImages/Industry/Destinations(1)/AdobeStock_302223688_Editorial_Use_Only/Hear-Singapore-roar-with-its-2023-tourism-performa.jpeg"))
))
.addComponent(
new BodyComponent().addParameter(new TextParameter("Australia"))
)
.addComponent(
new ButtonComponent().setIndex(1).setSubType(ButtonSubType.URL).addParameter(new TextParameter("url_456"))
)
)
)
);
System.out.println(new ObjectMapper().writeValueAsString(message));

whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);
}
}