diff --git a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TranscriptLoggerMiddleware.java b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TranscriptLoggerMiddleware.java index e3ea52a12..bf77ddb2c 100644 --- a/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TranscriptLoggerMiddleware.java +++ b/libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TranscriptLoggerMiddleware.java @@ -4,6 +4,7 @@ package com.microsoft.bot.builder; import com.microsoft.bot.schema.Activity; +import com.microsoft.bot.schema.ActivityEventNames; import com.microsoft.bot.schema.ActivityTypes; import com.microsoft.bot.schema.ChannelAccount; import com.microsoft.bot.schema.RoleTypes; @@ -13,6 +14,7 @@ import java.util.Queue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentLinkedQueue; +import org.apache.commons.lang3.StringUtils; /** * When added, this middleware will log incoming and outgoing activities to a @@ -57,7 +59,22 @@ public TranscriptLoggerMiddleware(TranscriptLogger withTranscriptLogger) { public CompletableFuture onTurn(TurnContext context, NextDelegate next) { // log incoming activity at beginning of turn if (context.getActivity() != null) { - logActivity(Activity.clone(context.getActivity()), true); + if (context.getActivity().getFrom() == null) { + context.getActivity().setFrom(new ChannelAccount()); + } + + if (context.getActivity().getFrom().getProperties().get("role") == null + && context.getActivity().getFrom().getRole() == null + ) { + context.getActivity().getFrom().setRole(RoleTypes.USER); + } + + // We should not log ContinueConversation events used by skills to initialize the middleware. + if (!(context.getActivity().isType(ActivityTypes.EVENT) + && StringUtils.equals(context.getActivity().getName(), ActivityEventNames.CONTINUE_CONVERSATION)) + ) { + logActivity(Activity.clone(context.getActivity()), true); + } } // hook up onSend pipeline diff --git a/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TranscriptMiddlewareTest.java b/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TranscriptMiddlewareTest.java index c1bfcd788..0945d80dd 100644 --- a/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TranscriptMiddlewareTest.java +++ b/libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TranscriptMiddlewareTest.java @@ -282,7 +282,7 @@ public final void Transcript_RolesAreFilled() { // message. As demonstrated by the asserts after this TestFlow block // the role attribute is present on the activity as it is passed to // the transcript, but still missing inside the flow - Assert.assertNull(context.getActivity().getFrom().getRole()); + Assert.assertNotNull(context.getActivity().getFrom().getRole()); conversationId[0] = context.getActivity().getConversation().getId(); context.sendActivity("echo:" + context.getActivity().getText()).join(); return CompletableFuture.completedFuture(null); diff --git a/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/ActivityEventNames.java b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/ActivityEventNames.java new file mode 100644 index 000000000..3c1e81494 --- /dev/null +++ b/libraries/bot-schema/src/main/java/com/microsoft/bot/schema/ActivityEventNames.java @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.microsoft.bot.schema; + +/** + * Define values for common event names used by activities of type + * ActivityTypes.Event. + */ +public final class ActivityEventNames { + private ActivityEventNames() { + + } + + /** + * The event name for continuing a conversation. + */ + public static final String CONTINUE_CONVERSATION = "ContinueConversation"; + + /** + * The event name for creating a conversation. + */ + public static final String CREATE_CONVERSATION = "CreateConversation"; +}