Skip to content

Commit 7792191

Browse files
authored
Get & test webhook URL #582 (#589)
1 parent 5cedfc1 commit 7792191

File tree

12 files changed

+461
-38
lines changed

12 files changed

+461
-38
lines changed

line-bot-api-client/src/main/java/com/linecorp/bot/client/LineMessagingClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@
3030
import com.linecorp.bot.model.group.GroupSummaryResponse;
3131
import com.linecorp.bot.model.profile.MembersIdsResponse;
3232
import com.linecorp.bot.model.profile.UserProfileResponse;
33+
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3334
import com.linecorp.bot.model.response.BotApiResponse;
3435
import com.linecorp.bot.model.response.BotInfoResponse;
3536
import com.linecorp.bot.model.response.GetMessageEventResponse;
3637
import com.linecorp.bot.model.response.GetNumberOfFollowersResponse;
3738
import com.linecorp.bot.model.response.GetNumberOfMessageDeliveriesResponse;
39+
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
3840
import com.linecorp.bot.model.response.IssueLinkTokenResponse;
3941
import com.linecorp.bot.model.response.MessageQuotaResponse;
4042
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
4143
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
4244
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
45+
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
4346
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4447
import com.linecorp.bot.model.richmenu.RichMenu;
4548
import com.linecorp.bot.model.richmenu.RichMenuIdResponse;
@@ -378,6 +381,20 @@ public interface LineMessagingClient {
378381
*/
379382
CompletableFuture<BotInfoResponse> getBotInfo();
380383

384+
/**
385+
* Gets webhook endpoint information.
386+
*
387+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#get-webhook-endpoint-information">Get webhook endpoint information</a>
388+
*/
389+
CompletableFuture<GetWebhookEndpointResponse> getWebhookEndpoint();
390+
391+
/**
392+
* Tests webhook endpoint.
393+
*
394+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#test-webhook-endpoint">Test webhook endpoint</a>
395+
*/
396+
CompletableFuture<TestWebhookEndpointResponse> testWebhookEndpoint(TestWebhookEndpointRequest request);
397+
381398
static LineMessagingClientBuilder builder(String channelToken) {
382399
return builder(FixedChannelTokenSupplier.of(channelToken));
383400
}

line-bot-api-client/src/main/java/com/linecorp/bot/client/LineMessagingClientImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@
3131
import com.linecorp.bot.model.group.GroupSummaryResponse;
3232
import com.linecorp.bot.model.profile.MembersIdsResponse;
3333
import com.linecorp.bot.model.profile.UserProfileResponse;
34+
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3435
import com.linecorp.bot.model.response.BotApiResponse;
3536
import com.linecorp.bot.model.response.BotInfoResponse;
3637
import com.linecorp.bot.model.response.GetMessageEventResponse;
3738
import com.linecorp.bot.model.response.GetNumberOfFollowersResponse;
3839
import com.linecorp.bot.model.response.GetNumberOfMessageDeliveriesResponse;
40+
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
3941
import com.linecorp.bot.model.response.IssueLinkTokenResponse;
4042
import com.linecorp.bot.model.response.MessageQuotaResponse;
4143
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
4244
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
4345
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
46+
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
4447
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4548
import com.linecorp.bot.model.richmenu.RichMenu;
4649
import com.linecorp.bot.model.richmenu.RichMenuBulkLinkRequest;
@@ -285,6 +288,17 @@ public CompletableFuture<BotInfoResponse> getBotInfo() {
285288
return toFuture(retrofitImpl.getBotInfo());
286289
}
287290

291+
@Override
292+
public CompletableFuture<GetWebhookEndpointResponse> getWebhookEndpoint() {
293+
return toFuture(retrofitImpl.getWebhookEndpoint());
294+
}
295+
296+
@Override
297+
public CompletableFuture<TestWebhookEndpointResponse> testWebhookEndpoint(
298+
TestWebhookEndpointRequest request) {
299+
return toFuture(retrofitImpl.testWebhookEndpoint(request));
300+
}
301+
288302
// TODO: Extract this method.
289303
static <T> CompletableFuture<T> toFuture(Call<T> callToWrap) {
290304
final CallbackAdaptor<T> completableFuture = new CallbackAdaptor<>();

line-bot-api-client/src/main/java/com/linecorp/bot/client/LineMessagingService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
import com.linecorp.bot.model.group.GroupSummaryResponse;
2828
import com.linecorp.bot.model.profile.MembersIdsResponse;
2929
import com.linecorp.bot.model.profile.UserProfileResponse;
30+
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3031
import com.linecorp.bot.model.response.BotInfoResponse;
3132
import com.linecorp.bot.model.response.GetMessageEventResponse;
3233
import com.linecorp.bot.model.response.GetNumberOfFollowersResponse;
3334
import com.linecorp.bot.model.response.GetNumberOfMessageDeliveriesResponse;
35+
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
3436
import com.linecorp.bot.model.response.IssueLinkTokenResponse;
3537
import com.linecorp.bot.model.response.MessageQuotaResponse;
3638
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
3739
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
3840
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
41+
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
3942
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4043
import com.linecorp.bot.model.richmenu.RichMenu;
4144
import com.linecorp.bot.model.richmenu.RichMenuBulkLinkRequest;
@@ -382,4 +385,10 @@ Call<Void> linkRichMenuToUser(
382385
*/
383386
@GET("v2/bot/info")
384387
Call<BotInfoResponse> getBotInfo();
388+
389+
@GET("v2/bot/channel/webhook/endpoint")
390+
Call<GetWebhookEndpointResponse> getWebhookEndpoint();
391+
392+
@POST("v2/bot/channel/webhook/test")
393+
Call<TestWebhookEndpointResponse> testWebhookEndpoint(@Body TestWebhookEndpointRequest request);
385394
}

line-bot-api-client/src/test/java/com/linecorp/bot/client/LineMessagingClientImplTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.io.IOException;
3131
import java.net.URI;
32+
import java.time.Instant;
3233
import java.util.Collections;
3334

3435
import org.junit.Rule;
@@ -54,17 +55,20 @@
5455
import com.linecorp.bot.model.narrowcast.filter.GenderDemographicFilter.Gender;
5556
import com.linecorp.bot.model.profile.MembersIdsResponse;
5657
import com.linecorp.bot.model.profile.UserProfileResponse;
58+
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
5759
import com.linecorp.bot.model.response.BotApiResponse;
5860
import com.linecorp.bot.model.response.BotInfoResponse;
5961
import com.linecorp.bot.model.response.GetNumberOfFollowersResponse;
6062
import com.linecorp.bot.model.response.GetNumberOfMessageDeliveriesResponse;
63+
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
6164
import com.linecorp.bot.model.response.IssueLinkTokenResponse;
6265
import com.linecorp.bot.model.response.MessageQuotaResponse;
6366
import com.linecorp.bot.model.response.MessageQuotaResponse.QuotaType;
6467
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
6568
import com.linecorp.bot.model.response.NarrowcastProgressResponse.Phase;
6669
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
6770
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
71+
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
6872
import com.linecorp.bot.model.richmenu.RichMenu;
6973
import com.linecorp.bot.model.richmenu.RichMenuBulkLinkRequest;
7074
import com.linecorp.bot.model.richmenu.RichMenuBulkUnlinkRequest;
@@ -663,6 +667,39 @@ public void getBotInfo() throws Exception {
663667
assertThat(actual).isEqualTo(response);
664668
}
665669

670+
@Test
671+
public void getWebhookEndpoint() throws Exception {
672+
final GetWebhookEndpointResponse response = GetWebhookEndpointResponse
673+
.builder()
674+
.endpoint(URI.create("https://line.me/webhook"))
675+
.active(true)
676+
.build();
677+
whenCall(retrofitMock.getWebhookEndpoint(), response);
678+
final GetWebhookEndpointResponse actual = target.getWebhookEndpoint().get();
679+
verify(retrofitMock, only()).getWebhookEndpoint();
680+
assertThat(actual).isEqualTo(response);
681+
}
682+
683+
@Test
684+
public void testWebhookEndpoint() throws Exception {
685+
final TestWebhookEndpointResponse response = TestWebhookEndpointResponse
686+
.builder()
687+
.success(true)
688+
.timestamp(Instant.now())
689+
.detail("abc")
690+
.reason("def")
691+
.statusCode(200)
692+
.build();
693+
final TestWebhookEndpointRequest request = TestWebhookEndpointRequest
694+
.builder()
695+
.endpoint(URI.create("http://example.com/my/great/endpoint"))
696+
.build();
697+
whenCall(retrofitMock.testWebhookEndpoint(request), response);
698+
final TestWebhookEndpointResponse actual = target.testWebhookEndpoint(request).get();
699+
verify(retrofitMock, only()).testWebhookEndpoint(request);
700+
assertThat(actual).isEqualTo(response);
701+
}
702+
666703
// Utility methods
667704

668705
private static <T> void whenCall(Call<T> call, T value) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.model.request;
18+
19+
import java.net.URI;
20+
21+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
23+
24+
import com.linecorp.bot.model.request.TestWebhookEndpointRequest.TestWebhookEndpointRequestBuilder;
25+
26+
import lombok.Builder;
27+
import lombok.Value;
28+
29+
@Value
30+
@Builder
31+
@JsonDeserialize(builder = TestWebhookEndpointRequestBuilder.class)
32+
public class TestWebhookEndpointRequest {
33+
/**
34+
* A valid webhook URL. If {@literal null}, sends a test webhook event to a webhook endpoint that is already
35+
* set to the channel.
36+
*/
37+
URI endpoint;
38+
39+
@JsonPOJOBuilder(withPrefix = "")
40+
public static class TestWebhookEndpointRequestBuilder {
41+
}
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2020 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.model.response;
18+
19+
import java.net.URI;
20+
21+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
23+
24+
import com.linecorp.bot.model.response.GetWebhookEndpointResponse.GetWebhookEndpointResponseBuilder;
25+
26+
import lombok.Builder;
27+
import lombok.Value;
28+
29+
@Value
30+
@Builder
31+
@JsonDeserialize(builder = GetWebhookEndpointResponseBuilder.class)
32+
public class GetWebhookEndpointResponse {
33+
/**
34+
* Webhook URL.
35+
*/
36+
URI endpoint;
37+
38+
/*
39+
* Webhook usage status. The LINE platform sends a webhook event to {@link #endpoint} only if
40+
* {@literal true}.
41+
*/
42+
boolean active;
43+
44+
@JsonPOJOBuilder(withPrefix = "")
45+
public static class GetWebhookEndpointResponseBuilder {
46+
}
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2020 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.bot.model.response;
18+
19+
import java.time.Instant;
20+
21+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
22+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
23+
24+
import com.linecorp.bot.model.response.TestWebhookEndpointResponse.TestWebhookEndpointResponseBuilder;
25+
26+
import lombok.Builder;
27+
import lombok.Value;
28+
29+
@Value
30+
@Builder
31+
@JsonDeserialize(builder = TestWebhookEndpointResponseBuilder.class)
32+
public class TestWebhookEndpointResponse {
33+
/**
34+
* Result of the communication from the LINE platform to the webhook URL.
35+
*/
36+
boolean success;
37+
38+
/**
39+
* Please refer to <a href="https://developers.line.biz/en/reference/messaging-api/#common-properties">
40+
* Common Properties</a>.
41+
*/
42+
Instant timestamp;
43+
44+
/**
45+
* The HTTP status code. If the webhook response isn't received, the status code is set to zero or a
46+
* negative number.
47+
*/
48+
int statusCode;
49+
50+
/**
51+
* Reason for the response.
52+
*/
53+
String reason;
54+
55+
/**
56+
* Details of the response.
57+
*/
58+
String detail;
59+
60+
@JsonPOJOBuilder(withPrefix = "")
61+
public static class TestWebhookEndpointResponseBuilder {
62+
}
63+
}

0 commit comments

Comments
 (0)