Skip to content

Commit 7199b5d

Browse files
authored
Set webhook URL Close (#592)
Close #582
1 parent 13352a1 commit 7199b5d

File tree

8 files changed

+143
-1
lines changed

8 files changed

+143
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
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.SetWebhookEndpointRequest;
3334
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3435
import com.linecorp.bot.model.response.BotApiResponse;
3536
import com.linecorp.bot.model.response.BotInfoResponse;
@@ -42,6 +43,7 @@
4243
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
4344
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
4445
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
46+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
4547
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
4648
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4749
import com.linecorp.bot.model.richmenu.RichMenu;
@@ -388,6 +390,13 @@ public interface LineMessagingClient {
388390
*/
389391
CompletableFuture<GetWebhookEndpointResponse> getWebhookEndpoint();
390392

393+
/**
394+
* Sets webhook endpoint URL.
395+
*
396+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#set-webhook-endpoint-url">Set webhook URL</a>
397+
*/
398+
CompletableFuture<SetWebhookEndpointResponse> setWebhookEndpoint(SetWebhookEndpointRequest request);
399+
391400
/**
392401
* Tests webhook endpoint.
393402
*

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
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.SetWebhookEndpointRequest;
3435
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3536
import com.linecorp.bot.model.response.BotApiResponse;
3637
import com.linecorp.bot.model.response.BotInfoResponse;
@@ -43,6 +44,7 @@
4344
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
4445
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
4546
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
47+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
4648
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
4749
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4850
import com.linecorp.bot.model.richmenu.RichMenu;
@@ -293,6 +295,11 @@ public CompletableFuture<GetWebhookEndpointResponse> getWebhookEndpoint() {
293295
return toFuture(retrofitImpl.getWebhookEndpoint());
294296
}
295297

298+
@Override
299+
public CompletableFuture<SetWebhookEndpointResponse> setWebhookEndpoint(SetWebhookEndpointRequest request) {
300+
return toFuture(retrofitImpl.setWebhookEndpoint(request));
301+
}
302+
296303
@Override
297304
public CompletableFuture<TestWebhookEndpointResponse> testWebhookEndpoint(
298305
TestWebhookEndpointRequest request) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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.SetWebhookEndpointRequest;
3031
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3132
import com.linecorp.bot.model.response.BotInfoResponse;
3233
import com.linecorp.bot.model.response.GetMessageEventResponse;
@@ -38,6 +39,7 @@
3839
import com.linecorp.bot.model.response.NarrowcastProgressResponse;
3940
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
4041
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
42+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
4143
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
4244
import com.linecorp.bot.model.response.demographics.GetFriendsDemographicsResponse;
4345
import com.linecorp.bot.model.richmenu.RichMenu;
@@ -54,6 +56,7 @@
5456
import retrofit2.http.GET;
5557
import retrofit2.http.Header;
5658
import retrofit2.http.POST;
59+
import retrofit2.http.PUT;
5760
import retrofit2.http.Path;
5861
import retrofit2.http.Query;
5962

@@ -389,6 +392,9 @@ Call<Void> linkRichMenuToUser(
389392
@GET("v2/bot/channel/webhook/endpoint")
390393
Call<GetWebhookEndpointResponse> getWebhookEndpoint();
391394

395+
@PUT("v2/bot/channel/webhook/endpoint")
396+
Call<SetWebhookEndpointResponse> setWebhookEndpoint(@Body SetWebhookEndpointRequest request);
397+
392398
@POST("v2/bot/channel/webhook/test")
393399
Call<TestWebhookEndpointResponse> testWebhookEndpoint(@Body TestWebhookEndpointRequest request);
394400
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.linecorp.bot.model.narrowcast.filter.GenderDemographicFilter.Gender;
5656
import com.linecorp.bot.model.profile.MembersIdsResponse;
5757
import com.linecorp.bot.model.profile.UserProfileResponse;
58+
import com.linecorp.bot.model.request.SetWebhookEndpointRequest;
5859
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
5960
import com.linecorp.bot.model.response.BotApiResponse;
6061
import com.linecorp.bot.model.response.BotInfoResponse;
@@ -68,6 +69,7 @@
6869
import com.linecorp.bot.model.response.NarrowcastProgressResponse.Phase;
6970
import com.linecorp.bot.model.response.NumberOfMessagesResponse;
7071
import com.linecorp.bot.model.response.QuotaConsumptionResponse;
72+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
7173
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
7274
import com.linecorp.bot.model.richmenu.RichMenu;
7375
import com.linecorp.bot.model.richmenu.RichMenuBulkLinkRequest;
@@ -680,6 +682,21 @@ public void getWebhookEndpoint() throws Exception {
680682
assertThat(actual).isEqualTo(response);
681683
}
682684

685+
@Test
686+
public void setWebhookEndpoint() throws Exception {
687+
final SetWebhookEndpointResponse response = SetWebhookEndpointResponse
688+
.builder()
689+
.build();
690+
final SetWebhookEndpointRequest request = SetWebhookEndpointRequest
691+
.builder()
692+
.endpoint(URI.create("http://example.com/my/great/endpoint"))
693+
.build();
694+
whenCall(retrofitMock.setWebhookEndpoint(request), response);
695+
final SetWebhookEndpointResponse actual = target.setWebhookEndpoint(request).get();
696+
verify(retrofitMock, only()).setWebhookEndpoint(request);
697+
assertThat(actual).isEqualTo(response);
698+
}
699+
683700
@Test
684701
public void testWebhookEndpoint() throws Exception {
685702
final TestWebhookEndpointResponse response = TestWebhookEndpointResponse
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.SetWebhookEndpointRequest.SetWebhookEndpointRequestBuilder;
25+
26+
import lombok.Builder;
27+
import lombok.Value;
28+
29+
@Value
30+
@Builder
31+
@JsonDeserialize(builder = SetWebhookEndpointRequestBuilder.class)
32+
public class SetWebhookEndpointRequest {
33+
/**
34+
* A valid webhook URL.
35+
*/
36+
URI endpoint;
37+
38+
@JsonPOJOBuilder(withPrefix = "")
39+
public static class SetWebhookEndpointRequestBuilder {
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 com.fasterxml.jackson.databind.annotation.JsonDeserialize;
20+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
21+
22+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse.SetWebhookEndpointResponseBuilder;
23+
24+
import lombok.Builder;
25+
import lombok.Value;
26+
27+
@Value
28+
@Builder
29+
@JsonDeserialize(builder = SetWebhookEndpointResponseBuilder.class)
30+
public class SetWebhookEndpointResponse {
31+
32+
@JsonPOJOBuilder(withPrefix = "")
33+
public static class SetWebhookEndpointResponseBuilder {
34+
}
35+
}

sample-manage-audience/src/main/java/com/linecorp/bot/messagingapidemoapp/controller/BotController.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828

2929
import com.linecorp.bot.client.LineMessagingClient;
3030
import com.linecorp.bot.client.exception.NotFoundException;
31+
import com.linecorp.bot.model.request.SetWebhookEndpointRequest;
3132
import com.linecorp.bot.model.request.TestWebhookEndpointRequest;
3233
import com.linecorp.bot.model.request.TestWebhookEndpointRequest.TestWebhookEndpointRequestBuilder;
3334
import com.linecorp.bot.model.response.BotInfoResponse;
3435
import com.linecorp.bot.model.response.GetWebhookEndpointResponse;
36+
import com.linecorp.bot.model.response.SetWebhookEndpointResponse;
3537
import com.linecorp.bot.model.response.TestWebhookEndpointResponse;
3638

3739
import lombok.AllArgsConstructor;
@@ -63,9 +65,22 @@ public String webhook(Model model) throws InterruptedException, ExecutionExcepti
6365
return "bot/get_webhook";
6466
}
6567

68+
@PostMapping("/bot/set_webhook")
69+
public String setWebhook(Model model,
70+
@RequestParam("url") URI uri) {
71+
SetWebhookEndpointRequest request = SetWebhookEndpointRequest.builder()
72+
.endpoint(uri)
73+
.build();
74+
SetWebhookEndpointResponse response = client.setWebhookEndpoint(
75+
request
76+
).join();
77+
model.addAttribute("response", response);
78+
return "redirect:/bot/webhook";
79+
}
80+
6681
@PostMapping("/bot/test_webhook")
6782
public String testWebhook(Model model,
68-
@RequestParam("url") URI uri) throws InterruptedException, ExecutionException {
83+
@RequestParam(value = "url", required = false) URI uri) {
6984
TestWebhookEndpointRequestBuilder builder = TestWebhookEndpointRequest.builder();
7085
if (uri != null) {
7186
builder.endpoint(uri);

sample-manage-audience/src/main/resources/templates/bot/get_webhook.ftlh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
</table>
2121
</#if>
2222

23+
<h2>Set webhook</h2>
24+
<form action="/bot/set_webhook" method="post">
25+
<table>
26+
<tr>
27+
<th><label for="webhookTestUrl">URL</label></th>
28+
<td><input id="webhookTestUrl" type="url" name="url" required>
29+
<button type="submit">Set</button>
30+
</td>
31+
</tr>
32+
</table>
33+
</form>
34+
2335
<h2>Test webhook</h2>
2436
<form method="post" action="/bot/test_webhook">
2537
<table>

0 commit comments

Comments
 (0)