forked from microsoft/botbuilder-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscriptLoggerMiddleware.java
More file actions
136 lines (110 loc) · 4.4 KB
/
TranscriptLoggerMiddleware.java
File metadata and controls
136 lines (110 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.microsoft.bot.builder;
import com.microsoft.bot.schema.Activity;
import com.microsoft.bot.schema.ActivityTypes;
import com.microsoft.bot.schema.ChannelAccount;
import com.microsoft.bot.schema.RoleTypes;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* When added, this middleware will log incoming and outgoing activities to a TranscriptStore.
*/
public class TranscriptLoggerMiddleware implements Middleware {
/**
* The TranscriptLogger to log to.
*/
private TranscriptLogger transcriptLogger;
/**
* Activity queue.
*/
private Queue<Activity> transcript = new ConcurrentLinkedQueue<Activity>();
/**
* Initializes a new instance of the <see cref="TranscriptLoggerMiddleware"/> class.
*
* @param withTranscriptLogger The transcript logger to use.
*/
public TranscriptLoggerMiddleware(TranscriptLogger withTranscriptLogger) {
if (withTranscriptLogger == null) {
throw new IllegalArgumentException(
"TranscriptLoggerMiddleware requires a ITranscriptLogger implementation.");
}
transcriptLogger = withTranscriptLogger;
}
/**
* Records incoming and outgoing activities to the conversation store.
*
* @param context The context object for this turn.
* @param next The delegate to call to continue the bot middleware pipeline.
* @return A task that represents the work queued to execute.
*/
@Override
public CompletableFuture<Void> onTurn(TurnContext context, NextDelegate next) {
// log incoming activity at beginning of turn
if (context.getActivity() != null) {
logActivity(Activity.clone(context.getActivity()), true);
}
// hook up onSend pipeline
context.onSendActivities((ctx, activities, nextSend) -> {
// run full pipeline
return nextSend.get()
.thenApply(responses -> {
for (Activity activity : activities) {
logActivity(Activity.clone(activity), false);
}
return responses;
});
});
// hook up update activity pipeline
context.onUpdateActivity((ctx, activity, nextUpdate) -> {
// run full pipeline
return nextUpdate.get()
.thenApply(resourceResponse -> {
// add Message Update activity
Activity updateActivity = Activity.clone(activity);
updateActivity.setType(ActivityTypes.MESSAGE_UPDATE);
logActivity(updateActivity, false);
return resourceResponse;
});
});
// hook up delete activity pipeline
context.onDeleteActivity((ctx, reference, nextDel) -> {
// run full pipeline
return nextDel.get()
.thenApply(nextDelResult -> {
// add MessageDelete activity
// log as MessageDelete activity
Activity deleteActivity = new Activity(ActivityTypes.MESSAGE_DELETE) {{
setId(reference.getActivityId());
applyConversationReference(reference, false);
}};
logActivity(deleteActivity, false);
return null;
});
});
// process bot logic
return next.next()
.thenAccept(nextResult -> {
// flush transcript at end of turn
while (!transcript.isEmpty()) {
Activity activity = transcript.poll();
transcriptLogger.logActivity(activity);
}
});
}
private void logActivity(Activity activity, boolean incoming) {
if (activity.getTimestamp() == null) {
activity.setTimestamp(OffsetDateTime.now(ZoneId.of("UTC")));
}
if (activity.getFrom() == null) {
activity.setFrom(new ChannelAccount());
}
if (activity.getFrom().getRole() == null) {
activity.getFrom().setRole(incoming ? RoleTypes.USER : RoleTypes.BOT);
}
transcript.offer(activity);
}
}