Example #1
0
        public override async Task RouteActivityToExistingHandoff(ITurnContext turnContext, HandoffRecord handoffRecord)
        {
            var livePersonHandoffRecord = handoffRecord as LivePersonHandoffRecord;

            if (livePersonHandoffRecord != null)
            {
                var account = _creds.LpAccount;
                var message = LivePersonConnector.MakeLivePersonMessage(0,
                                                                        livePersonHandoffRecord.RemoteConversationId,
                                                                        turnContext.Activity.Text);

                await LivePersonConnector.SendMessageToConversationAsync(account,
                                                                         livePersonHandoffRecord.ConversationRecord.MessageDomain,
                                                                         livePersonHandoffRecord.ConversationRecord.AppJWT,
                                                                         livePersonHandoffRecord.ConversationRecord.ConsumerJWS,
                                                                         message).ConfigureAwait(false);
            }
        }
Example #2
0
        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 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");
            }
        }