Skip to content

Commit

Permalink
fix send template messages bug ((Bindambc#100) The parameter template…
Browse files Browse the repository at this point in the history
…['language']['code'] is required.)
  • Loading branch information
Bindambc committed Mar 8, 2023
1 parent dec4ca1 commit 362caf5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/main/java/com/whatsapp/api/domain/messages/Language.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
package com.whatsapp.api.domain.messages;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.whatsapp.api.domain.templates.type.LanguageType;

/**
* The type Language.
* @param code Language code. See {@link LanguageType}
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public record Language(LanguageType code) {
/**
* Instantiates a new Language.
*
* @param code the code
*/
public Language {
}

/**
* Gets code.
*
* @return the code
*/
@Override
public LanguageType code() {
return code;
}
public record Language(@JsonProperty("code") LanguageType code) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public static void main(String[] args) {
);

whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.media.FileType;
import com.whatsapp.api.domain.messages.AudioMessage;
import com.whatsapp.api.domain.messages.Component;
import com.whatsapp.api.domain.messages.DocumentMessage;
import com.whatsapp.api.domain.messages.ImageMessage;
import com.whatsapp.api.domain.messages.Language;
import com.whatsapp.api.domain.messages.Message.MessageBuilder;
import com.whatsapp.api.domain.messages.StickerMessage;
import com.whatsapp.api.domain.messages.TemplateMessage;
import com.whatsapp.api.domain.messages.TextMessage;
import com.whatsapp.api.domain.messages.TextParameter;
import com.whatsapp.api.domain.messages.VideoMessage;
import com.whatsapp.api.domain.templates.type.ComponentType;
import com.whatsapp.api.domain.templates.type.LanguageType;
import com.whatsapp.api.exception.WhatsappApiException;
import com.whatsapp.api.exception.utils.Formatter;
import mockwebserver3.MockResponse;
Expand Down Expand Up @@ -90,6 +96,41 @@ void testSendTextMessage() throws IOException, URISyntaxException, InterruptedEx
Assertions.assertEquals("wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww", response.messages().get(0).id());
}

@Test
void testSendTemplateTextMessage() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/message.json")));

WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TestConstants.TOKEN);

WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();


var templateMessage = new TemplateMessage()//
.setLanguage(new Language(LanguageType.PT_BR))//
.setName("number_confirmation")//
.addComponent(//
new Component(ComponentType.BODY)//
.addParameter(new TextParameter("18754269072")//
));

var message = MessageBuilder.builder()//
.setTo(TestConstants.PHONE_NUMBER_1)//
.buildTemplateMessage(templateMessage);

whatsappBusinessCloudApi.sendMessage(PHONE_NUMBER_ID, message);

RecordedRequest recordedRequest = mockWebServer.takeRequest();
Assertions.assertEquals("POST", recordedRequest.getMethod());
Assertions.assertEquals("/" + API_VERSION + "/" + PHONE_NUMBER_ID + "/messages", recordedRequest.getPath());

var expectedBody = """
{"messaging_product":"whatsapp","recipient_type":"individual","to":"%s","type":"template","template":{"components":[{"type":"BODY","parameters":[{"type":"text","text":"18754269072"}]}],"name":"number_confirmation","language":{"code":"pt_BR"}}}""";

Assertions.assertEquals(String.format(expectedBody, PHONE_NUMBER_1), recordedRequest.getBody().readUtf8());

}


@Test
void testSendAudioMessage() throws IOException, URISyntaxException, InterruptedException {
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(fromResource("/message.json")));
Expand Down

0 comments on commit 362caf5

Please sign in to comment.