-
Notifications
You must be signed in to change notification settings - Fork 173
/
ServiceNowHandoffMiddleware.cs
112 lines (96 loc) · 5.64 KB
/
ServiceNowHandoffMiddleware.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
using System;
using System.Globalization;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Bot.Builder.Community.Components.Handoff.ServiceNow.Models;
using Bot.Builder.Community.Components.Handoff.Shared;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Bot.Builder.Community.Components.Handoff.ServiceNow
{
public class ServiceNowHandoffMiddleware : HandoffMiddleware
{
private readonly ConversationHandoffRecordMap _conversationHandoffRecordMap;
private readonly IServiceNowCredentialsProvider _creds;
private readonly BotFrameworkAuthentication _botFrameworkAuth;
public ServiceNowHandoffMiddleware(ConversationHandoffRecordMap conversationHandoffRecordMap, IServiceNowCredentialsProvider creds, BotFrameworkAuthentication botFrameworkAuth) : base(conversationHandoffRecordMap)
{
_conversationHandoffRecordMap = conversationHandoffRecordMap;
_creds = creds;
_botFrameworkAuth = botFrameworkAuth;
}
public override async Task RouteActivityToExistingHandoff(ITurnContext turnContext, HandoffRecord handoffRecord)
{
var serviceNowHandoffRecord = handoffRecord as ServiceNowHandoffRecord;
// Retrieve an oAuth token for ServiceNow which we'll pass on this turn
var claimsIdentity = (ClaimsIdentity)turnContext.TurnState.Get<IIdentity>(BotFrameworkAdapter.BotIdentityKey);
var userTokenClient = await _botFrameworkAuth.CreateUserTokenClientAsync(claimsIdentity, default(CancellationToken));
var tokenResponse = await userTokenClient.GetUserTokenAsync(turnContext.Activity.From.Id, _creds.ServiceNowAuthConnectionName, null, null, default(CancellationToken));
if (tokenResponse != null)
{
if (serviceNowHandoffRecord != null)
{
var messageText = turnContext.Activity.Text;
// If the incoming activity has a value, it may be a response from an Adaptive Card
// which we need to process accordingly.
if (turnContext.Activity.Value != null)
{
try
{
var activityValue = JObject.Parse(turnContext.Activity.Value.ToString());
if (activityValue.ContainsKey("dateVal") && activityValue.ContainsKey("timeVal"))
{
var dateTimeStr = $"{activityValue["dateVal"]} {activityValue["timeVal"]}";
if (DateTime.TryParse(dateTimeStr, out DateTime dateTime))
{
var baseDate = new DateTime(1970, 1, 1);
var diff = dateTime - baseDate;
messageText = diff.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
}
}
}
catch(JsonReaderException ex)
{
// Value is not valid Json so continue
}
}
var message = ServiceNowConnector.MakeServiceNowMessage(0,
serviceNowHandoffRecord.RemoteConversationId,
messageText,
serviceNowHandoffRecord.ConversationRecord.Timezone,
serviceNowHandoffRecord.ConversationRecord.UserId,
serviceNowHandoffRecord.ConversationRecord.EmailId);
await ServiceNowConnector.SendMessageToConversationAsync(
serviceNowHandoffRecord.ConversationRecord.ServiceNowTenant,
tokenResponse.Token,
message).ConfigureAwait(false);
var traceActivity = Activity.CreateTraceActivity("ServiceNowVirtualAgent", label: "ServiceNowHandoff->Activity forwarded to ServiceNow");
await turnContext.SendActivityAsync(traceActivity);
}
}
else
{
var traceActivity = Activity.CreateTraceActivity("ServiceNowVirtualAgent", label: "ServiceNowHandoff->No ServiceNow authentication token available.");
await turnContext.SendActivityAsync(traceActivity);
throw new Exception("No ServiceNow authentication token available for this user");
}
}
public override async Task<HandoffRecord> Escalate(ITurnContext turnContext, IEventActivity handoffEvent)
{
var serviceNowTenant = _creds.ServiceNowTenant;
var serviceNowAuthConnectionName = _creds.ServiceNowAuthConnectionName;
var conversationRecord = await ServiceNowConnector.EscalateToAgentAsync(turnContext, handoffEvent, serviceNowTenant, serviceNowAuthConnectionName, _conversationHandoffRecordMap);
// Forward the activating activity onto ServiceNow
var handoffRecord = new ServiceNowHandoffRecord(turnContext.Activity.GetConversationReference(), conversationRecord);
await RouteActivityToExistingHandoff(turnContext, handoffRecord);
var traceActivity = Activity.CreateTraceActivity("ServiceNowVirtualAgent", label: "ServiceNowHandoff->Handoff initiated");
await turnContext.SendActivityAsync(traceActivity);
return handoffRecord;
}
}
}