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); }
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 message = ServiceNowConnector.MakeServiceNowMessage(0, serviceNowHandoffRecord.RemoteConversationId, turnContext.Activity.Text, serviceNowHandoffRecord.ConversationRecord.Timezone, turnContext.Activity.Locale, 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 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"); } }