Skip to content

Commit aea8300

Browse files
authored
Video viewing complete event has been added #560 (#570)
* Add missing duration field in VideoMessageContent class. Close #568 * Use Integer type to consistency with AudioMessageContent. * fix javadoc * Support videoPlayComplete event. * resolve conflict
1 parent 23bf94a commit aea8300

File tree

6 files changed

+152
-3
lines changed

6 files changed

+152
-3
lines changed

line-bot-model/src/main/java/com/linecorp/bot/model/event/Event.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
@JsonSubTypes.Type(ThingsEvent.class),
3939
@JsonSubTypes.Type(MemberJoinedEvent.class),
4040
@JsonSubTypes.Type(MemberLeftEvent.class),
41-
@JsonSubTypes.Type(UnsendEvent.class)
41+
@JsonSubTypes.Type(UnsendEvent.class),
42+
@JsonSubTypes.Type(VideoPlayCompleteEvent.class)
4243
})
4344
@JsonTypeInfo(
4445
use = JsonTypeInfo.Id.NAME,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.event;
18+
19+
import java.time.Instant;
20+
21+
import com.fasterxml.jackson.annotation.JsonTypeName;
22+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
24+
25+
import com.linecorp.bot.model.event.source.Source;
26+
27+
import lombok.Builder;
28+
import lombok.Value;
29+
30+
/**
31+
* Event for when a user finishes viewing a video at least once with the specified trackingId sent by the LINE
32+
* Official Account.
33+
*/
34+
@JsonTypeName("videoPlayComplete")
35+
@Value
36+
@Builder(toBuilder = true)
37+
@JsonDeserialize(builder = VideoPlayCompleteEvent.VideoPlayCompleteEventBuilder.class)
38+
public class VideoPlayCompleteEvent implements Event, ReplyEvent {
39+
@JsonPOJOBuilder(withPrefix = "")
40+
public static class VideoPlayCompleteEventBuilder {
41+
// Providing builder instead of public constructor. Class body is filled by lombok.
42+
}
43+
44+
/**
45+
* Token for replying to this event.
46+
*/
47+
String replyToken;
48+
49+
/**
50+
* JSON object which contains the source of the event.
51+
*/
52+
Source source;
53+
54+
/**
55+
* Time of the event.
56+
*/
57+
Instant timestamp;
58+
59+
/**
60+
* Channel state.
61+
* <dl>
62+
* <dt>active</dt>
63+
* <dd>The channel is active. You can send a reply message or push message from the bot server that received
64+
* this webhook event.</dd>
65+
* <dt>standby (under development)</dt>
66+
* <dd>The channel is waiting. The bot server that received this webhook event shouldn't send any messages.
67+
* </dd>
68+
* </dl>
69+
*/
70+
EventMode mode;
71+
72+
VideoPlayComplete videoPlayComplete;
73+
74+
@Value
75+
@Builder(toBuilder = true)
76+
@JsonDeserialize(builder = VideoPlayCompleteEvent.VideoPlayComplete.VideoPlayCompleteBuilder.class)
77+
public static class VideoPlayComplete {
78+
@JsonPOJOBuilder(withPrefix = "")
79+
public static class VideoPlayCompleteBuilder {
80+
// Providing builder instead of public constructor. Class body is filled by lombok.
81+
}
82+
83+
/**
84+
* ID used to identify a video. Returns the same value as the trackingId assigned to the video message.
85+
*/
86+
String trackingId;
87+
}
88+
89+
}

line-bot-model/src/main/java/com/linecorp/bot/model/message/VideoMessage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,18 @@ public class VideoMessage implements Message {
6565

6666
Sender sender;
6767

68+
/**
69+
* ID used to identify a video.
70+
*/
71+
String trackingId;
72+
6873
/**
6974
* Constructor without {@link #quickReply} parameter.
7075
*
7176
* <p>If you want use {@link QuickReply}, please use {@link #builder()} instead.
7277
*/
7378
public VideoMessage(final URI originalContentUrl, final URI previewImageUrl) {
74-
this(originalContentUrl, previewImageUrl, null, null);
79+
this(originalContentUrl, previewImageUrl, null, null, null);
7580
}
7681

7782
@JsonPOJOBuilder(withPrefix = "")

line-bot-model/src/test/java/com/linecorp/bot/model/event/CallbackRequestTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,22 @@ public void testVideo() throws IOException {
596596
});
597597
}
598598

599+
@Test
600+
public void testVideoPlayComplete() throws IOException {
601+
parse("callback/video-play-complete.json", callbackRequest -> {
602+
assertDestination(callbackRequest);
603+
Event event = callbackRequest.getEvents().get(0);
604+
assertThat(event.getSource()).isInstanceOf(UserSource.class);
605+
assertThat(event).isInstanceOf(VideoPlayCompleteEvent.class);
606+
assertThat(event.getMode())
607+
.isEqualTo(EventMode.ACTIVE);
608+
609+
VideoPlayCompleteEvent videoPlayCompleteEvent = (VideoPlayCompleteEvent) event;
610+
assertThat(videoPlayCompleteEvent.getVideoPlayComplete().getTrackingId())
611+
.isEqualTo("track_id");
612+
});
613+
}
614+
599615
// Event, that has brand new eventType
600616
@Test
601617
public void testUnknown() throws IOException {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"destination": "Uab012345678901234567890123456789",
3+
"events": [
4+
{
5+
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
6+
"type": "videoPlayComplete",
7+
"timestamp": 1462629479859,
8+
"mode": "active",
9+
"source": {
10+
"type": "user",
11+
"userId": "U4af4980629..."
12+
},
13+
"videoPlayComplete": {
14+
"trackingId": "track_id"
15+
}
16+
}
17+
]
18+
}

sample-spring-boot-kitchensink/src/main/java/com/example/bot/spring/KitchenSinkController.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
import com.linecorp.bot.model.event.MessageEvent;
5757
import com.linecorp.bot.model.event.PostbackEvent;
5858
import com.linecorp.bot.model.event.UnfollowEvent;
59+
import com.linecorp.bot.model.event.UnknownEvent;
5960
import com.linecorp.bot.model.event.UnsendEvent;
61+
import com.linecorp.bot.model.event.VideoPlayCompleteEvent;
6062
import com.linecorp.bot.model.event.message.AudioMessageContent;
6163
import com.linecorp.bot.model.event.message.ContentProvider;
6264
import com.linecorp.bot.model.event.message.FileMessageContent;
@@ -198,11 +200,24 @@ public void handleVideoMessageEvent(MessageEvent<VideoMessageContent> event) thr
198200
mp4.path + "[0]",
199201
previewImg.path.toString());
200202
}
203+
String trackingId = UUID.randomUUID().toString();
204+
log.info("Sending video message with trackingId={}", trackingId);
201205
reply(event.getReplyToken(),
202-
new VideoMessage(mp4.getUri(), previewImg.uri));
206+
VideoMessage.builder()
207+
.originalContentUrl(mp4.getUri())
208+
.previewImageUrl(previewImg.uri)
209+
.trackingId(trackingId)
210+
.build());
203211
});
204212
}
205213

214+
@EventMapping
215+
public void handleVideoPlayCompleteEvent(VideoPlayCompleteEvent event) throws IOException {
216+
log.info("Got video play complete: tracking id={}", event.getVideoPlayComplete().getTrackingId());
217+
this.replyText(event.getReplyToken(),
218+
"You played " + event.getVideoPlayComplete().getTrackingId());
219+
}
220+
206221
@EventMapping
207222
public void handleFileMessageEvent(MessageEvent<FileMessageContent> event) {
208223
this.reply(event.getReplyToken(),
@@ -216,6 +231,11 @@ public void handleUnfollowEvent(UnfollowEvent event) {
216231
log.info("unfollowed this bot: {}", event);
217232
}
218233

234+
@EventMapping
235+
public void handleUnknownEvent(UnknownEvent event) {
236+
log.info("Got an unknown event!!!!! : {}", event);
237+
}
238+
219239
@EventMapping
220240
public void handleFollowEvent(FollowEvent event) {
221241
String replyToken = event.getReplyToken();

0 commit comments

Comments
 (0)