33
44package 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 ;
96import com .microsoft .bot .schema .Activity ;
107import com .microsoft .bot .schema .ActivityTypes ;
118import com .microsoft .bot .schema .ChannelAccount ;
12- import org . apache . commons . lang3 . StringUtils ;
9+ import com . microsoft . bot . schema . RoleTypes ;
1310
1411import java .time .OffsetDateTime ;
1512import java .time .ZoneId ;
2219 * When added, this middleware will log incoming and outgoing activities to a TranscriptStore.
2320 */
2421public 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}
0 commit comments