Ejemplo n.º 1
0
 public HandoffMiddleware(ConversationState conversationState, ConversationMap conversationMap, ILivePersonCredentialsProvider creds)
 {
     _conversationState = conversationState;
     _conversationMap   = conversationMap;
     _creds             = creds;
 }
Ejemplo n.º 2
0
        static public async Task <LivePersonConversationRecord> EscalateToAgenAsync(ITurnContext turnContext, IEventActivity handoffEvent, string account, string clientId, string clientSecret, ConversationMap conversationMap)
        {
            var sentinelDomain = await GetDomainAsync(account, "sentinel").ConfigureAwait(false);

            var appJWT = await GetAppJWTAsync(account, sentinelDomain, clientId, clientSecret).ConfigureAwait(false);

            var consumer = new ConsumerId {
                ext_consumer_id = turnContext.Activity.From.Id
            };

            var idpDomain = await GetDomainAsync(account, "idp").ConfigureAwait(false);

            var consumerJWS = await GetConsumerJWSAsync(account, idpDomain, appJWT, consumer).ConfigureAwait(false);

            var context = handoffEvent.Value as JObject;

            // This can be null:
            var skill = context?.Value <string>("Skill");
            var engagementAttributes = (context["EngagementAttributes"] as JArray)?.ToObject <EngagementAttribute[]>();

            var msgDomain = await GetDomainAsync(account, "asyncMessagingEnt").ConfigureAwait(false);

            var conversations = new Conversation[] {
                new Conversation {
                    kind = "req",
                    id   = "1,",
                    type = "userprofile.SetUserProfile",
                    body = new Body {
                        authenticatedData = new Authenticateddata {
                            lp_sdes = engagementAttributes?.Select(ea => ea.ToLivePersonEngagementAttribute()).ToArray()
                        }
                    }
                },
                new Conversation {
                    kind    = "req",
                    id      = "2,",
                    type    = "cm.ConsumerRequestConversation",
                    skillId = skill,
                    body    = new Body {
                        brandId = account
                    }
                },
            };

            var conversationId = await StartConversationAsync(account, msgDomain, appJWT, consumerJWS, conversations).ConfigureAwait(false);

            System.Diagnostics.Debug.WriteLine($"Started LP conversation id {conversationId}");

            conversationMap.ConversationRecords.TryAdd(conversationId, new ConversationRecord {
                ConversationReference = turnContext.Activity.GetConversationReference()
            });

            var messageId = 1;

            // First, play out the transcript
            var handoffActivity = handoffEvent as Activity;

            if (handoffActivity.Attachments != null)
            {
                foreach (var attachment in handoffActivity.Attachments)
                {
                    if (attachment.Name == "Transcript")
                    {
                        var transcript = attachment.Content as Transcript;
                        foreach (var activity in transcript.Activities)
                        {
                            var message2 = MakeLivePersonMessage(messageId++,
                                                                 conversationId,
                                                                 $"{activity.From.Name}: {activity.Text}");
                            await SendMessageToConversationAsync(account, msgDomain, appJWT, consumerJWS, message2).ConfigureAwait(false);
                        }
                    }
                }
            }

            return(new LivePersonConversationRecord {
                ConversationId = conversationId, AppJWT = appJWT, ConsumerJWS = consumerJWS, MessageDomain = msgDomain
            });
        }