-
Notifications
You must be signed in to change notification settings - Fork 66
/
MessageRouterResultHandler.cs
282 lines (239 loc) · 11.5 KB
/
MessageRouterResultHandler.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using IntermediatorBotSample.CommandHandling;
using IntermediatorBotSample.Resources;
using Microsoft.Bot.Schema;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Underscore.Bot.MessageRouting;
using Underscore.Bot.MessageRouting.DataStore;
using Underscore.Bot.MessageRouting.Models;
using Underscore.Bot.MessageRouting.Results;
namespace IntermediatorBotSample.MessageRouting
{
/// <summary>
/// Handles the message router results.
/// </summary>
public class MessageRouterResultHandler
{
private MessageRouter _messageRouter;
public MessageRouterResultHandler(MessageRouter messageRouter)
{
_messageRouter = messageRouter
?? throw new ArgumentNullException(
$"({nameof(messageRouter)}) cannot be null");
}
/// <summary>
/// Handles the given message router result.
/// </summary>
/// <param name="messageRouterResult">The result to handle.</param>
/// <returns>True, if the result was handled. False, if no action was taken.</returns>
public virtual async Task<bool> HandleResultAsync(AbstractMessageRouterResult messageRouterResult)
{
if (messageRouterResult != null)
{
if (messageRouterResult is ConnectionRequestResult)
{
return await HandleConnectionRequestResultAsync(messageRouterResult as ConnectionRequestResult);
}
if (messageRouterResult is ConnectionResult)
{
return await HandleConnectionResultAsync(messageRouterResult as ConnectionResult);
}
if (messageRouterResult is MessageRoutingResult)
{
return await HandleMessageRoutingResultAsync(messageRouterResult as MessageRoutingResult);
}
}
return false;
}
/// <summary>
/// Handles the given connection request result.
/// </summary>
/// <param name="connectionRequestResult">The result to handle.</param>
/// <returns>True, if the result was handled. False, if no action was taken.</returns>
protected virtual async Task<bool> HandleConnectionRequestResultAsync(
ConnectionRequestResult connectionRequestResult)
{
ConnectionRequest connectionRequest = connectionRequestResult?.ConnectionRequest;
if (connectionRequest == null || connectionRequest.Requestor == null)
{
System.Diagnostics.Debug.WriteLine("No client to inform about the connection request result");
return false;
}
switch (connectionRequestResult.Type)
{
case ConnectionRequestResultType.Created:
foreach (ConversationReference aggregationChannel
in _messageRouter.RoutingDataManager.GetAggregationChannels())
{
ConversationReference botConversationReference =
_messageRouter.RoutingDataManager.FindConversationReference(
aggregationChannel.ChannelId, aggregationChannel.Conversation.Id, null, true);
if (botConversationReference != null)
{
IMessageActivity messageActivity = Activity.CreateMessageActivity();
messageActivity.Conversation = aggregationChannel.Conversation;
messageActivity.Recipient = RoutingDataManager.GetChannelAccount(aggregationChannel);
messageActivity.Attachments = new List<Attachment>
{
CommandCardFactory.CreateConnectionRequestCard(
connectionRequest,
RoutingDataManager.GetChannelAccount(
botConversationReference)?.Name).ToAttachment()
};
await _messageRouter.SendMessageAsync(aggregationChannel, messageActivity);
}
}
await _messageRouter.SendMessageAsync(
connectionRequest.Requestor, Strings.NotifyClientWaitForRequestHandling);
return true;
case ConnectionRequestResultType.AlreadyExists:
await _messageRouter.SendMessageAsync(
connectionRequest.Requestor, Strings.NotifyClientDuplicateRequest);
return true;
case ConnectionRequestResultType.Rejected:
if (connectionRequestResult.Rejecter != null)
{
await _messageRouter.SendMessageAsync(
connectionRequestResult.Rejecter,
string.Format(Strings.NotifyOwnerRequestRejected, GetNameOrId(connectionRequest.Requestor)));
}
await _messageRouter.SendMessageAsync(
connectionRequest.Requestor, Strings.NotifyClientRequestRejected);
return true;
case ConnectionRequestResultType.NotSetup:
await _messageRouter.SendMessageAsync(
connectionRequest.Requestor, Strings.NoAgentsAvailable);
return true;
case ConnectionRequestResultType.Error:
if (connectionRequestResult.Rejecter != null)
{
await _messageRouter.SendMessageAsync(
connectionRequestResult.Rejecter,
string.Format(Strings.ConnectionRequestResultErrorWithResult, connectionRequestResult.ErrorMessage));
}
return true;
default:
break;
}
return false;
}
/// <summary>
/// Handles the given connection result.
/// </summary>
/// <param name="connectionResult">The result to handle.</param>
/// <returns>True, if the result was handled. False, if no action was taken.</returns>
protected virtual async Task<bool> HandleConnectionResultAsync(ConnectionResult connectionResult)
{
Connection connection = connectionResult.Connection;
switch (connectionResult.Type)
{
case ConnectionResultType.Connected:
if (connection != null)
{
if (connection.ConversationReference1 != null)
{
await _messageRouter.SendMessageAsync(
connection.ConversationReference1,
string.Format(Strings.NotifyOwnerConnected,
GetNameOrId(connection.ConversationReference2)));
}
if (connection.ConversationReference2 != null)
{
await _messageRouter.SendMessageAsync(
connection.ConversationReference2,
string.Format(Strings.NotifyOwnerConnected,
GetNameOrId(connection.ConversationReference1)));
}
}
return true;
case ConnectionResultType.Disconnected:
if (connection != null)
{
if (connection.ConversationReference1 != null)
{
await _messageRouter.SendMessageAsync(
connection.ConversationReference1,
string.Format(Strings.NotifyOwnerDisconnected,
GetNameOrId(connection.ConversationReference2)));
}
if (connection.ConversationReference2 != null)
{
await _messageRouter.SendMessageAsync(
connection.ConversationReference2,
string.Format(Strings.NotifyClientDisconnected,
GetNameOrId(connection.ConversationReference1)));
}
}
return true;
case ConnectionResultType.Error:
if (connection.ConversationReference1 != null)
{
await _messageRouter.SendMessageAsync(
connection.ConversationReference1,
string.Format(Strings.ConnectionResultErrorWithResult, connectionResult.ErrorMessage));
}
return true;
default:
break;
}
return false;
}
/// <summary>
/// Handles the given message routing result.
/// </summary>
/// <param name="messageRoutingResult">The result to handle.</param>
/// <returns>True, if the result was handled. False, if no action was taken.</returns>
protected virtual async Task<bool> HandleMessageRoutingResultAsync(
MessageRoutingResult messageRoutingResult)
{
ConversationReference agent = messageRoutingResult?.Connection?.ConversationReference1;
switch (messageRoutingResult.Type)
{
case MessageRoutingResultType.NoActionTaken:
case MessageRoutingResultType.MessageRouted:
// No need to do anything
break;
case MessageRoutingResultType.FailedToRouteMessage:
case MessageRoutingResultType.Error:
if (agent != null)
{
string errorMessage = string.IsNullOrWhiteSpace(messageRoutingResult.ErrorMessage)
? Strings.FailedToForwardMessage
: messageRoutingResult.ErrorMessage;
await _messageRouter.SendMessageAsync(agent, errorMessage);
}
return true;
default:
break;
}
return false;
}
/// <summary>
/// Tries to resolve the name of the given user/bot instance.
/// Will fallback to ID, if no name specified.
/// </summary>
/// <param name="conversationReference">The conversation reference, whose details to resolve.</param>
/// <returns>The name or the ID of the given user/bot instance.</returns>
protected virtual string GetNameOrId(ConversationReference conversationReference)
{
if (conversationReference != null)
{
ChannelAccount channelAccount =
RoutingDataManager.GetChannelAccount(conversationReference);
if (channelAccount != null)
{
if (!string.IsNullOrWhiteSpace(channelAccount.Name))
{
return channelAccount.Name;
}
if (!string.IsNullOrWhiteSpace(channelAccount.Id))
{
return channelAccount.Id;
}
}
}
return StringConstants.NoNameOrId;
}
}
}