Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit e5dcc60

Browse files
authored
Merge pull request #139 from matthiasblaesing/transcript-middleware-azure
Using TranscriptLoggerMiddleware breaks Bot on Azure Infrastructure
2 parents 2ad8470 + c417b49 commit e5dcc60

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/TranscriptLoggerMiddleware.java

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33

44
package com.microsoft.bot.builder;
55

6-
import com.fasterxml.jackson.databind.JsonNode;
7-
import com.fasterxml.jackson.databind.ObjectMapper;
8-
import com.fasterxml.jackson.databind.SerializationFeature;
96
import com.microsoft.bot.schema.Activity;
107
import com.microsoft.bot.schema.ActivityTypes;
118
import com.microsoft.bot.schema.ChannelAccount;
12-
import org.apache.commons.lang3.StringUtils;
9+
import com.microsoft.bot.schema.RoleTypes;
1310

1411
import java.time.OffsetDateTime;
1512
import java.time.ZoneId;
@@ -22,16 +19,6 @@
2219
* When added, this middleware will log incoming and outgoing activities to a TranscriptStore.
2320
*/
2421
public class TranscriptLoggerMiddleware implements Middleware {
25-
/**
26-
* To/From JSON.
27-
*/
28-
private static ObjectMapper mapper;
29-
30-
static {
31-
mapper = new ObjectMapper()
32-
.enable(SerializationFeature.INDENT_OUTPUT)
33-
.findAndRegisterModules();
34-
}
3522

3623
/**
3724
* The TranscriptLogger to log to.
@@ -68,20 +55,7 @@ public TranscriptLoggerMiddleware(TranscriptLogger withTranscriptLogger) {
6855
public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
6956
// log incoming activity at beginning of turn
7057
if (context.getActivity() != null) {
71-
if (context.getActivity().getFrom() == null) {
72-
context.getActivity().setFrom(new ChannelAccount());
73-
}
74-
75-
JsonNode role = null;
76-
if (context.getActivity().getFrom().getProperties().containsKey("role")) {
77-
role = context.getActivity().getFrom().getProperties().get("role");
78-
}
79-
80-
if (role == null || StringUtils.isBlank(role.asText())) {
81-
context.getActivity().getFrom().getProperties().put("role", mapper.createObjectNode().with("user"));
82-
}
83-
84-
logActivity(Activity.clone(context.getActivity()));
58+
logActivity(Activity.clone(context.getActivity()), true);
8559
}
8660

8761
// hook up onSend pipeline
@@ -90,7 +64,7 @@ public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
9064
return nextSend.get()
9165
.thenApply(responses -> {
9266
for (Activity activity : activities) {
93-
logActivity(Activity.clone(activity));
67+
logActivity(Activity.clone(activity), false);
9468
}
9569

9670
return responses;
@@ -105,7 +79,7 @@ public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
10579
// add Message Update activity
10680
Activity updateActivity = Activity.clone(activity);
10781
updateActivity.setType(ActivityTypes.MESSAGE_UPDATE);
108-
logActivity(updateActivity);
82+
logActivity(updateActivity, false);
10983

11084
return resourceResponse;
11185
});
@@ -123,7 +97,7 @@ public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
12397
applyConversationReference(reference, false);
12498
}};
12599

126-
logActivity(deleteActivity);
100+
logActivity(deleteActivity, false);
127101

128102
return null;
129103
});
@@ -141,10 +115,19 @@ public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
141115
});
142116
}
143117

144-
private void logActivity(Activity activity) {
118+
private void logActivity(Activity activity, boolean incoming) {
145119
if (activity.getTimestamp() == null) {
146120
activity.setTimestamp(OffsetDateTime.now(ZoneId.of("UTC")));
147121
}
122+
123+
if (activity.getFrom() == null) {
124+
activity.setFrom(new ChannelAccount());
125+
}
126+
127+
if (activity.getFrom().getRole() == null) {
128+
activity.getFrom().setRole(incoming ? RoleTypes.USER : RoleTypes.BOT);
129+
}
130+
148131
transcript.offer(activity);
149132
}
150133
}

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TranscriptMiddlewareTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,34 @@ public void Transcript_TestDateLogUpdateActivities() {
223223
pagedResult = transcriptStore.getTranscriptActivities("test", conversationId[0], null, OffsetDateTime.MAX).join();
224224
Assert.assertEquals(0, pagedResult.getItems().size());
225225
}
226+
227+
@Test
228+
public final void Transcript_RolesAreFilled() {
229+
MemoryTranscriptStore transcriptStore = new MemoryTranscriptStore();
230+
TestAdapter adapter = (new TestAdapter()).use(new TranscriptLoggerMiddleware(transcriptStore));
231+
final String[] conversationId = {null};
232+
233+
new TestFlow(adapter, (context) -> {
234+
// The next assert implicitly tests the immutability of the incoming
235+
// message. As demonstrated by the asserts after this TestFlow block
236+
// the role attribute is present on the activity as it is passed to
237+
// the transcript, but still missing inside the flow
238+
Assert.assertNull(context.getActivity().getFrom().getRole());
239+
conversationId[0] = context.getActivity().getConversation().getId();
240+
context.sendActivity("echo:" + context.getActivity().getText()).join();
241+
return CompletableFuture.completedFuture(null);
242+
})
243+
.send("test")
244+
.startTest().join();
245+
246+
PagedResult<Activity> pagedResult = transcriptStore.getTranscriptActivities("test", conversationId[0]).join();
247+
Assert.assertEquals(2, pagedResult.getItems().size());
248+
Assert.assertNotNull(pagedResult.getItems().get(0).getFrom());
249+
Assert.assertEquals(RoleTypes.USER, pagedResult.getItems().get(0).getFrom().getRole());
250+
Assert.assertNotNull(pagedResult.getItems().get(1).getFrom());
251+
Assert.assertEquals(RoleTypes.BOT, pagedResult.getItems().get(1).getFrom().getRole());
252+
253+
System.out.printf("Complete");
254+
}
226255
}
227256

0 commit comments

Comments
 (0)