예제 #1
0
        private async Task <Microsoft.Bot.Connector.DirectLine.Activity> PostToAgentBotAsync2(Microsoft.Bot.Connector.DirectLine.Activity activityFromUser)
        {
            var directLineSecret = Configuration.ConfigurationHelper.GetString("AgentBot_DirectLine_Secret");
            var agentStatusDB    = Configuration.ConfigurationHelper.GetString("BotStatusDBConnectionString");
            var agentStorage     = new AgentStatusStorage(agentStatusDB);
            var agent            = await agentStorage.QueryAgentStatusAsync(activityFromUser.Recipient.Id);

            using (var client = new ConnectorClient(new Uri("https://smba.trafficmanager.net/apis")))
            {
                var recipient = new Microsoft.Bot.Connector.ChannelAccount(/*agent.AgentIdInChannel*/ "29:1Gk7vrlkdaMoN6fCtrycxkJfHcPS8zvi49Gukq4XuZAo");
                var from      = new Microsoft.Bot.Connector.ChannelAccount("");
                //var conversatoin = await client.Conversations.CreateDirectConversationAsync(from, recipient);
                var message = new Microsoft.Bot.Connector.Activity
                {
                    Text         = activityFromUser.Text,
                    From         = from,
                    Conversation = new Microsoft.Bot.Connector.ConversationAccount
                    {
                        Id = agent.ConversationId
                    },
                    Recipient = recipient
                };
                var response = await client.Conversations.SendToConversationAsync(message);

                return(null);
            }
        }
예제 #2
0
        private async Task PostToOCSUser(IDialogContext context, Microsoft.Bot.Connector.Activity activity)
        {
            Logger.Info($"Agent [{activity.From.Id}] is replying");
            var         storage = new AgentStatusStorage(ConfigurationHelper.GetString("BotStatusDBConnectionString"));
            AgentStatus agent   = await storage.QueryAgentStatusAsync(activity.From.Id);

            ConversationRecord conv = (await storage.FindMyConversationActivityAsync(agent.Id)).FirstOrDefault();

            var uri = new Uri("https://directline.botframework.com");

            Logger.Info($"PostToOCSUser::{agent.Id}/{agent.Name}");
            DirectLineClientCredentials creds = new DirectLineClientCredentials(ConfigurationHelper.GetString("OCSBot_DirectLine_Secret"));   //lot into the bot framework

            Microsoft.Bot.Connector.DirectLine.DirectLineClient client = new Microsoft.Bot.Connector.DirectLine.DirectLineClient(uri, creds); //connect the client
            var conversation           = client.Conversations.StartConversation();
            DirectLineChannelData data = new DirectLineChannelData()
            {
                UserID          = conv.RemoteUserId,
                UserName        = conv.RemoteUserName,
                DirectLineBotID = conv.RemoteBotId
            };

            client.Conversations.PostActivity(conversation.ConversationId,
                                              new Microsoft.Bot.Connector.DirectLine.Activity
            {
                From = new Microsoft.Bot.Connector.DirectLine.ChannelAccount
                {
                    Id   = agent.Id,
                    Name = $"{agent.Name}@agent"
                },
                Type        = Microsoft.Bot.Connector.ActivityTypes.Message,
                Text        = activity.Text,
                ChannelData = data
            });

            //var remoteConnector = new ConnectorClient(
            //                            baseUri: new Uri(remoteActivity.ServiceUrl),
            //                            credentials: new MicrosoftAppCredentials(
            //                                            appId: ConfigurationHelper.GetString("MicrosoftAppId"),
            //                                            password: ConfigurationHelper.GetString("MicrosoftAppPassword")
            //                                        ),
            //                            addJwtTokenRefresher: true
            //                            );
            //Logger.Info($"remoteActivity={JsonConvert.SerializeObject(remoteActivity)}");
            //remoteConnector.Conversations.SendToConversation(reply);

            ////reply.From.Name += activity.From.Name + "@agent";
            //Logger.Info($"reply created:{JsonConvert.SerializeObject(reply)}");
            //remoteConnector.Conversations.ReplyToActivity(reply);
            //Logger.Info($"replied");
        }
        private async Task <Microsoft.Bot.Connector.DirectLine.Activity> PostToAgentBotAsync(Microsoft.Bot.Connector.DirectLine.Activity activityFromUser)
        {
            var directLineSecret = Configuration.ConfigurationHelper.GetString("AgentBot_DirectLine_Secret");
            var agentStatusDB    = Configuration.ConfigurationHelper.GetString("BotStatusDBConnectionString");
            var dc           = new DirectLineClient(directLineSecret);
            var agentStorage = new AgentStatusStorage(agentStatusDB);
            var agent        = await agentStorage.QueryAgentStatusAsync(activityFromUser.Recipient.Id);

            ConversationStatus convStatus = null;

            //var agentConversations = await agentStorage.QueryConversationStatusAsync(agent.Id);

            try
            {
                var uri = new Uri("https://directline.botframework.com");
                DirectLineClientCredentials creds  = new DirectLineClientCredentials(directLineSecret);                                //lot into the bot framework
                DirectLineClient            client = new DirectLineClient(uri, creds);                                                 //connect the client
                Microsoft.Bot.Connector.DirectLine.Conversations convs = new Microsoft.Bot.Connector.DirectLine.Conversations(client); //get the list of conversations belonging to the bot? Or does this start a new collection of conversations?

                Microsoft.Bot.Connector.DirectLine.Conversation conversation = null;
                if (string.IsNullOrEmpty(_agentConversationId))
                {
                    conversation         = dc.Conversations.StartConversation();
                    _agentConversationId = conversation.ConversationId;
                }
                else
                {
                    conversation = new Microsoft.Bot.Connector.DirectLine.Conversation()
                    {
                        ConversationId = _agentConversationId,
                    };
                }
                Logger.Info($"activityFromUser - From.Name:{activityFromUser.From.Name} - From.Id:{activityFromUser.From.Id}");
                Logger.Info($"activityFromUser - Recipient.Name:{activityFromUser.Recipient.Name} - Recipient.Id:{activityFromUser.Recipient.Name}");
                var toAgent = new Microsoft.Bot.Connector.DirectLine.Activity
                {
                    Type = Microsoft.Bot.Connector.DirectLine.ActivityTypes.Message,
                    Text = activityFromUser.Text,
                    From = new Microsoft.Bot.Connector.DirectLine.ChannelAccount
                    {
                        Id   = activityFromUser.From.Id,/*activityFromUser.From.Id,*/
                        Name = $"{activityFromUser.From.Name}@ocsuser"
                    },
                    Recipient   = activityFromUser.Recipient,
                    ChannelId   = agent.ChannelId,
                    ChannelData = new DirectLineChannelData
                    {
                        RoundTrip      = 0,
                        ConversationId = _agentConversationId,
                        UserID         = activityFromUser.From.Id,
                        UserName       = activityFromUser.From.Name
                    }
                };

                var resp = await dc.Conversations.PostActivityAsync(
                    conversation.ConversationId,
                    toAgent);

                Logger.Info($"OCSBot::Dialog:PostToAgent() - {JsonConvert.SerializeObject(toAgent)}");
                //convStatus = (await agentStorage.QueryConversationStatusAsync(agent.Id)).OrderByDescending(o => o.Timestamp).FirstOrDefault();
                //convStatus.OCSDirectlineConversationId = conversation.ConversationId;
                //convStatus.OCSEndUserId = activityFromUser.From.Id;
                //convStatus.OCSEndUserName = activityFromUser.From.Name;
                //convStatus.OCSBotName = activityFromUser.Recipient.Name;
                //convStatus.OCSBotId = activityFromUser.Recipient.Id;
                //await agentStorage.UpdateConversationStatusAsync(convStatus);
                return(null);
            }
            catch (Exception exp)
            {
                Logger.Info($"OCSBot::PostToAgent() - Exception while posting to Agent:{exp.Message}");
                throw;
            }
        }