7
7
using Microsoft . Bot . Schema ;
8
8
using Microsoft . Extensions . Configuration ;
9
9
using Microsoft . Extensions . Logging ;
10
+ using Newtonsoft . Json . Linq ;
10
11
using System ;
11
12
using System . Collections . Generic ;
12
13
using System . Text ;
@@ -30,9 +31,18 @@ namespace ACSConnector
30
31
/// </remarks>
31
32
public class ACSAdapter : BotFrameworkHttpAdapter , IACSAdapter
32
33
{
33
- public ACSAdapter ( IConfiguration configuration , ILogger < BotFrameworkHttpAdapter > logger , HandoffMiddleware handoffMiddleware , TranscriptLoggingMiddleware loggingMiddleware )
34
+ private readonly ConversationState _conversationState ;
35
+
36
+ public ACSAdapter (
37
+ IConfiguration configuration ,
38
+ ILogger < BotFrameworkHttpAdapter > logger ,
39
+ HandoffMiddleware handoffMiddleware ,
40
+ ConversationState conversationState ,
41
+ TranscriptLoggingMiddleware loggingMiddleware )
34
42
: base ( configuration , logger )
35
43
{
44
+ _conversationState = conversationState ?? throw new ArgumentNullException ( nameof ( conversationState ) ) ;
45
+
36
46
Use ( handoffMiddleware ) ;
37
47
Use ( loggingMiddleware ) ;
38
48
@@ -72,12 +82,50 @@ public async Task ProcessActivityAsync(Activity activity, string msAppId, Conver
72
82
await ContinueConversationAsync (
73
83
msAppId ,
74
84
conversationRef ,
75
- ( ITurnContext proactiveContext , CancellationToken ct ) =>
85
+ async ( ITurnContext proactiveContext , CancellationToken ct ) =>
76
86
{
77
87
using ( var contextWithActivity = new TurnContext ( this , activity ) )
78
88
{
79
89
contextWithActivity . TurnState . Add ( proactiveContext . TurnState . Get < IConnectorClient > ( ) ) ;
80
- return base . RunPipelineAsync ( contextWithActivity , callback , cancellationToken ) ;
90
+ await base . RunPipelineAsync ( contextWithActivity , callback , cancellationToken ) ;
91
+
92
+ if ( contextWithActivity . Activity . Name == "handoff.status" )
93
+ {
94
+ Activity replyActivity ;
95
+ var state = ( contextWithActivity . Activity . Value as JObject ) ? . Value < string > ( "state" ) ;
96
+ if ( state == "typing" )
97
+ {
98
+ replyActivity = new Activity
99
+ {
100
+ Type = ActivityTypes . Typing ,
101
+ Text = "agent is typing" ,
102
+ } ;
103
+ }
104
+ else if ( state == "accepted" )
105
+ {
106
+ replyActivity = MessageFactory . Text ( "An agent has accepted the conversation and will respond shortly." ) ;
107
+ await _conversationState . SaveChangesAsync ( contextWithActivity ) ;
108
+ }
109
+ else if ( state == "closed" )
110
+ {
111
+ replyActivity = MessageFactory . Text ( "The agent has ended the conversation and you're now reconnected with the digital assistant" ) ;
112
+
113
+ // Route the conversation based on whether it's been escalated
114
+ var conversationStateAccessors = _conversationState . CreateProperty < EscalationRecord > ( nameof ( EscalationRecord ) ) ;
115
+ var escalationRecord = await conversationStateAccessors . GetAsync ( contextWithActivity , ( ) => new EscalationRecord ( ) ) . ConfigureAwait ( false ) ;
116
+
117
+ // End the escalation
118
+ escalationRecord . EndEscalation ( ) ;
119
+ await _conversationState . SaveChangesAsync ( contextWithActivity ) . ConfigureAwait ( false ) ;
120
+ }
121
+ else
122
+ {
123
+ replyActivity = MessageFactory . Text ( $ "Conversation status changed to '{ state } '") ;
124
+ }
125
+
126
+ await contextWithActivity . SendActivityAsync ( replyActivity ) ;
127
+ }
128
+
81
129
}
82
130
} ,
83
131
cancellationToken ) . ConfigureAwait ( false ) ;
0 commit comments