Skip to content

Commit

Permalink
Merge pull request Bindambc#111 from Bindambc/35-two-step-verification
Browse files Browse the repository at this point in the history
Send Two-Step verification code
  • Loading branch information
Bindambc authored Aug 18, 2023
2 parents ae7959b + 5f67768 commit c6b488b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/whatsapp/api/domain/phone/TwoStepCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.whatsapp.api.domain.phone;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The type Two-Step Code
*
* @param pin Required. A 6-digit PIN you wish to use for two-step verification.
*/
public record TwoStepCode(
@JsonProperty("pin") String pin
) {
}
15 changes: 13 additions & 2 deletions src/main/java/com/whatsapp/api/impl/WhatsappBusinessCloudApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.whatsapp.api.domain.messages.Message;
import com.whatsapp.api.domain.messages.ReadMessage;
import com.whatsapp.api.domain.messages.response.MessageResponse;
import com.whatsapp.api.domain.phone.TwoStepCode;
import com.whatsapp.api.domain.response.Response;
import com.whatsapp.api.service.WhatsappBusinessCloudApiService;
import okhttp3.MediaType;
Expand All @@ -27,7 +28,6 @@ public class WhatsappBusinessCloudApi {

private final WhatsappBusinessCloudApiService whatsappBusinessCloudApiService;


/**
* Instantiates a new Whatsapp business cloud api.
*
Expand Down Expand Up @@ -73,7 +73,6 @@ public UploadResponse uploadMedia(String phoneNumberId, String fileName, FileTyp

var messageProduct = Part.createFormData("messaging_product", "whatsapp");


return executeSync(whatsappBusinessCloudApiService.uploadMedia(phoneNumberId, body, messageProduct));
}

Expand Down Expand Up @@ -126,5 +125,17 @@ public Response markMessageAsRead(String phoneNumberId, ReadMessage message) {
return executeSync(whatsappBusinessCloudApiService.markMessageAsRead(phoneNumberId, message));
}

/**
* Business Solution Providers (BSPs) must authenticate themselves with
* an access token with the whatsapp_business_management permission.
*
* @param phoneNumberId Represents a specific phone number.
* @param twoStepCode The {@link TwoStepCode} object.
* @return the response
* @see <a href="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/two-step-verification">official documentation</a>
*/
public Response twoStepVerification(String phoneNumberId, TwoStepCode twoStepCode) {
return executeSync(whatsappBusinessCloudApiService.twoStepVerification(phoneNumberId, twoStepCode));
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.whatsapp.api.service;


import com.whatsapp.api.domain.media.Media;
import com.whatsapp.api.domain.media.UploadResponse;
import com.whatsapp.api.domain.messages.Message;
import com.whatsapp.api.domain.messages.ReadMessage;
import com.whatsapp.api.domain.messages.response.MessageResponse;
import com.whatsapp.api.domain.phone.TwoStepCode;
import com.whatsapp.api.domain.response.Response;
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
Expand All @@ -23,13 +23,11 @@

import static com.whatsapp.api.configuration.WhatsappApiConfig.API_VERSION;


/**
* The interface Whatsapp business cloud api service.
*/
public interface WhatsappBusinessCloudApiService {


/**
* Send message call.
*
Expand All @@ -50,7 +48,8 @@ public interface WhatsappBusinessCloudApiService {
*/
@Multipart
@POST("/" + API_VERSION + "/{Phone-Number-ID}/media")
Call<UploadResponse> uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file, @Part MultipartBody.Part messageProduct);
Call<UploadResponse> uploadMedia(@Path("Phone-Number-ID") String phoneNumberId, @Part MultipartBody.Part file,
@Part MultipartBody.Part messageProduct);

/**
* Retrieve media url call.
Expand All @@ -61,7 +60,6 @@ public interface WhatsappBusinessCloudApiService {
@GET("/" + API_VERSION + "/{media-id}")
Call<Media> retrieveMediaUrl(@Path("media-id") String mediaId);


/**
* Download media file call.
*
Expand Down Expand Up @@ -92,4 +90,14 @@ public interface WhatsappBusinessCloudApiService {
@POST("/" + API_VERSION + "/{Phone-Number-ID}/messages")
Call<Response> markMessageAsRead(@Path("Phone-Number-ID") String phoneNumberId, @Body ReadMessage message);

/**
* Two-step verification call.
*
* @param phoneNumberId the phone number id
* @param twoStepCode the two-step code
* @return the call
*/
@POST("/" + API_VERSION + "/{Phone-Number-ID}")
Call<Response> twoStepVerification(@Path("Phone-Number-ID") String phoneNumberId, @Body TwoStepCode twoStepCode);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.whatsapp.api.examples;

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

import com.whatsapp.api.WhatsappApiFactory;
import com.whatsapp.api.domain.phone.TwoStepCode;
import com.whatsapp.api.impl.WhatsappBusinessCloudApi;

public class TwoStepVerificationExample {

public static void main(String[] args) {
WhatsappApiFactory factory = WhatsappApiFactory.newInstance(TOKEN);

WhatsappBusinessCloudApi whatsappBusinessCloudApi = factory.newBusinessCloudApi();

final var twoStepCode = new TwoStepCode("123456");

var response = whatsappBusinessCloudApi.twoStepVerification(PHONE_NUMBER_ID, twoStepCode);

System.out.println(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.whatsapp.api.domain.messages.type.InteractiveMessageType;
import com.whatsapp.api.domain.messages.type.PhoneType;
import com.whatsapp.api.domain.messages.type.UrlType;
import com.whatsapp.api.domain.phone.TwoStepCode;
import com.whatsapp.api.domain.templates.type.LanguageType;
import com.whatsapp.api.exception.WhatsappApiException;
import com.whatsapp.api.utils.Formatter;
Expand Down Expand Up @@ -1126,12 +1127,12 @@ void testLocationMessage() throws IOException, URISyntaxException, InterruptedEx

@Test
void testMarkAsReadMessage() throws IOException, URISyntaxException, InterruptedException, JSONException {
String responseBody = """
{
"success": true
}
""";

String responseBody = """
{
"success": true
}
""";
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseBody));

var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage17.json");
Expand All @@ -1149,6 +1150,29 @@ void testMarkAsReadMessage() throws IOException, URISyntaxException, Interrupted
Assertions.assertTrue(response.success());
}

@Test
void testTwoStepVerification() throws IOException, URISyntaxException, InterruptedException, JSONException {

}
String responseBody = """
{
"success": true
}
""";
mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(responseBody));

var expectedJson = fromResource(EXPECTED_FOLDER + "expectedMessage18.json");

var twoStepCode = new TwoStepCode("123456");

var response = whatsappBusinessCloudApi.twoStepVerification(PHONE_NUMBER_ID, twoStepCode);

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

JSONAssert.assertEquals(expectedJson, recordedRequest.getBody().readUtf8(), JSONCompareMode.STRICT);

Assertions.assertTrue(response.success());
}

}
3 changes: 3 additions & 0 deletions src/test/resources/expected/message/expectedMessage18.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pin": "123456"
}

0 comments on commit c6b488b

Please sign in to comment.