TranscriptLoggerMiddleware set turnContext.Activity.From.Role (#1057)

This commit is contained in:
tracyboehrer 2021-03-15 10:15:26 -05:00 коммит произвёл GitHub
Родитель 06c3fdc0ed
Коммит 47777086d5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 43 добавлений и 2 удалений

Просмотреть файл

@ -4,6 +4,7 @@
package com.microsoft.bot.builder; package com.microsoft.bot.builder;
import com.microsoft.bot.schema.Activity; import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityEventNames;
import com.microsoft.bot.schema.ActivityTypes; import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.ChannelAccount; import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.RoleTypes; import com.microsoft.bot.schema.RoleTypes;
@ -13,6 +14,7 @@ import java.time.ZoneId;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.lang3.StringUtils;
/** /**
* When added, this middleware will log incoming and outgoing activities to a * When added, this middleware will log incoming and outgoing activities to a
@ -57,7 +59,22 @@ public class TranscriptLoggerMiddleware implements Middleware {
public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) { public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
// log incoming activity at beginning of turn // log incoming activity at beginning of turn
if (context.getActivity() != null) { 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 // hook up onSend pipeline

Просмотреть файл

@ -282,7 +282,7 @@ public class TranscriptMiddlewareTest {
// message. As demonstrated by the asserts after this TestFlow block // message. As demonstrated by the asserts after this TestFlow block
// the role attribute is present on the activity as it is passed to // the role attribute is present on the activity as it is passed to
// the transcript, but still missing inside the flow // 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(); conversationId[0] = context.getActivity().getConversation().getId();
context.sendActivity("echo:" + context.getActivity().getText()).join(); context.sendActivity("echo:" + context.getActivity().getText()).join();
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);

Просмотреть файл

@ -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";
}