Skip to content

Commit

Permalink
Video viewing complete event has been added #560 (#570)
Browse files Browse the repository at this point in the history
* Add missing duration field in VideoMessageContent class.
Close #568

* Use Integer type to consistency with AudioMessageContent.

* fix javadoc

* Support videoPlayComplete event.

* resolve conflict
  • Loading branch information
tokuhirom authored Sep 4, 2020
1 parent 23bf94a commit aea8300
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
@JsonSubTypes.Type(ThingsEvent.class),
@JsonSubTypes.Type(MemberJoinedEvent.class),
@JsonSubTypes.Type(MemberLeftEvent.class),
@JsonSubTypes.Type(UnsendEvent.class)
@JsonSubTypes.Type(UnsendEvent.class),
@JsonSubTypes.Type(VideoPlayCompleteEvent.class)
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.bot.model.event;

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

import com.linecorp.bot.model.event.source.Source;

import lombok.Builder;
import lombok.Value;

/**
* Event for when a user finishes viewing a video at least once with the specified trackingId sent by the LINE
* Official Account.
*/
@JsonTypeName("videoPlayComplete")
@Value
@Builder(toBuilder = true)
@JsonDeserialize(builder = VideoPlayCompleteEvent.VideoPlayCompleteEventBuilder.class)
public class VideoPlayCompleteEvent implements Event, ReplyEvent {
@JsonPOJOBuilder(withPrefix = "")
public static class VideoPlayCompleteEventBuilder {
// Providing builder instead of public constructor. Class body is filled by lombok.
}

/**
* Token for replying to this event.
*/
String replyToken;

/**
* JSON object which contains the source of the event.
*/
Source source;

/**
* Time of the event.
*/
Instant timestamp;

/**
* Channel state.
* <dl>
* <dt>active</dt>
* <dd>The channel is active. You can send a reply message or push message from the bot server that received
* this webhook event.</dd>
* <dt>standby (under development)</dt>
* <dd>The channel is waiting. The bot server that received this webhook event shouldn't send any messages.
* </dd>
* </dl>
*/
EventMode mode;

VideoPlayComplete videoPlayComplete;

@Value
@Builder(toBuilder = true)
@JsonDeserialize(builder = VideoPlayCompleteEvent.VideoPlayComplete.VideoPlayCompleteBuilder.class)
public static class VideoPlayComplete {
@JsonPOJOBuilder(withPrefix = "")
public static class VideoPlayCompleteBuilder {
// Providing builder instead of public constructor. Class body is filled by lombok.
}

/**
* ID used to identify a video. Returns the same value as the trackingId assigned to the video message.
*/
String trackingId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ public class VideoMessage implements Message {

Sender sender;

/**
* ID used to identify a video.
*/
String trackingId;

/**
* Constructor without {@link #quickReply} parameter.
*
* <p>If you want use {@link QuickReply}, please use {@link #builder()} instead.
*/
public VideoMessage(final URI originalContentUrl, final URI previewImageUrl) {
this(originalContentUrl, previewImageUrl, null, null);
this(originalContentUrl, previewImageUrl, null, null, null);
}

@JsonPOJOBuilder(withPrefix = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,22 @@ public void testVideo() throws IOException {
});
}

@Test
public void testVideoPlayComplete() throws IOException {
parse("callback/video-play-complete.json", callbackRequest -> {
assertDestination(callbackRequest);
Event event = callbackRequest.getEvents().get(0);
assertThat(event.getSource()).isInstanceOf(UserSource.class);
assertThat(event).isInstanceOf(VideoPlayCompleteEvent.class);
assertThat(event.getMode())
.isEqualTo(EventMode.ACTIVE);

VideoPlayCompleteEvent videoPlayCompleteEvent = (VideoPlayCompleteEvent) event;
assertThat(videoPlayCompleteEvent.getVideoPlayComplete().getTrackingId())
.isEqualTo("track_id");
});
}

// Event, that has brand new eventType
@Test
public void testUnknown() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"destination": "Uab012345678901234567890123456789",
"events": [
{
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "videoPlayComplete",
"timestamp": 1462629479859,
"mode": "active",
"source": {
"type": "user",
"userId": "U4af4980629..."
},
"videoPlayComplete": {
"trackingId": "track_id"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
import com.linecorp.bot.model.event.MessageEvent;
import com.linecorp.bot.model.event.PostbackEvent;
import com.linecorp.bot.model.event.UnfollowEvent;
import com.linecorp.bot.model.event.UnknownEvent;
import com.linecorp.bot.model.event.UnsendEvent;
import com.linecorp.bot.model.event.VideoPlayCompleteEvent;
import com.linecorp.bot.model.event.message.AudioMessageContent;
import com.linecorp.bot.model.event.message.ContentProvider;
import com.linecorp.bot.model.event.message.FileMessageContent;
Expand Down Expand Up @@ -198,11 +200,24 @@ public void handleVideoMessageEvent(MessageEvent<VideoMessageContent> event) thr
mp4.path + "[0]",
previewImg.path.toString());
}
String trackingId = UUID.randomUUID().toString();
log.info("Sending video message with trackingId={}", trackingId);
reply(event.getReplyToken(),
new VideoMessage(mp4.getUri(), previewImg.uri));
VideoMessage.builder()
.originalContentUrl(mp4.getUri())
.previewImageUrl(previewImg.uri)
.trackingId(trackingId)
.build());
});
}

@EventMapping
public void handleVideoPlayCompleteEvent(VideoPlayCompleteEvent event) throws IOException {
log.info("Got video play complete: tracking id={}", event.getVideoPlayComplete().getTrackingId());
this.replyText(event.getReplyToken(),
"You played " + event.getVideoPlayComplete().getTrackingId());
}

@EventMapping
public void handleFileMessageEvent(MessageEvent<FileMessageContent> event) {
this.reply(event.getReplyToken(),
Expand All @@ -216,6 +231,11 @@ public void handleUnfollowEvent(UnfollowEvent event) {
log.info("unfollowed this bot: {}", event);
}

@EventMapping
public void handleUnknownEvent(UnknownEvent event) {
log.info("Got an unknown event!!!!! : {}", event);
}

@EventMapping
public void handleFollowEvent(FollowEvent event) {
String replyToken = event.getReplyToken();
Expand Down

0 comments on commit aea8300

Please sign in to comment.