public async Task StartAsync(IDialogContext context)
        {
            Logger.Info("StartAsync...");
            var incoming = context.Activity as Activity;
            var jsonText = JsonConvert.SerializeObject(incoming.ChannelData);

            Logger.Info($"JsonText={jsonText}");
            var channelData = JsonConvert.DeserializeObject <OCSBot.Shared.Models.DirectLineChannelData>(jsonText);

            Logger.Info($"ChannelData.DirectLineBotID={channelData.DirectLineBotID}");
            var storage           = new AgentStatusStorage(ConfigurationHelper.GetString("BotStatusDBConnectionString"));
            var localConversation = (await storage.FindMyConversationActivityAsync(channelData.UserID)).FirstOrDefault();

            Logger.Info($"localConversation={localConversation}");

            var resumptionCookie = UrlToken.Decode <ResumptionCookie>(localConversation.LocalActivity);
            var localActivity    = resumptionCookie.GetMessage();
            var message          = ((Activity)context.Activity).Text.Replace("reply:", "");

            message = $"[{incoming.From.Name}]{message}";
            var reply = localActivity.CreateReply(message);

            Logger.Info($"reply={JsonConvert.SerializeObject(reply)}");
            var localConnector = new ConnectorClient(new Uri(localActivity.ServiceUrl),
                                                     new MicrosoftAppCredentials(
                                                         ConfigurationHelper.GetString("MicrosoftAppId"),
                                                         ConfigurationHelper.GetString("MicrosoftAppPassword")),
                                                     true);

            Microsoft.Bot.Connector.Conversations localConversations = new Microsoft.Bot.Connector.Conversations(localConnector);
            localConversations.ReplyToActivity(reply);
            Logger.Info("Done");
        }
        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");
        }
        public async Task <bool> HumanProcess(IDialogContext context)
        {
            //TODO:end human investigating process
            var statusDB = Configuration.ConfigurationHelper.GetString("BotStatusDBConnectionString");
            var db       = new AgentStatusStorage(statusDB);
            var agents   = await db.FindAvailableAgentsAsync();

            var agent = agents.FirstOrDefault();

            Logger.Info($"Human Investigating:{IsHumanInvestigating}");

            if (agent != null)
            {
                //from bot to agent
                //find agent's conversation record
                var agentConversation   = (await db.FindMyConversationActivityAsync(agent.Id)).FirstOrDefault();
                var localConversation   = (await db.FindMyConversationActivityAsync(context.Activity.From.Id)).FirstOrDefault();
                ResumptionCookie cookie = new ResumptionCookie((Microsoft.Bot.Connector.IMessageActivity)context.Activity);
                if (localConversation == null)
                {
                    localConversation = new ConversationRecord();
                }
                localConversation.UserID              = context.Activity.From.Id;
                localConversation.LocalUserName       = context.Activity.From.Name;
                localConversation.LocalBotId          = context.Activity.Recipient.Id;
                localConversation.LocalChannelID      = context.Activity.ChannelId;
                localConversation.LocalConversationID = context.Activity.Conversation.Id;
                localConversation.LocalActivity       = UrlToken.Encode <ResumptionCookie>(cookie);

                var resp2 = await PostToAgentBotAsync(new Microsoft.Bot.Connector.DirectLine.Activity()
                {
                    Type = Microsoft.Bot.Connector.DirectLine.ActivityTypes.Message,
                    From = new Microsoft.Bot.Connector.DirectLine.ChannelAccount(
                        /*id: agentConversation.LocalBotId,*/
                        id: context.Activity.From.Id,
                        name: context.Activity.From.Name
                        ),
                    Recipient = new Microsoft.Bot.Connector.DirectLine.ChannelAccount(
                        id: agentConversation.UserID),
                    ChannelId = agent.ChannelId,
                    Text      = $"{context.Activity.AsMessageActivity().Text}"
                });

                await db.UpdateConversationActivityAsync(localConversation);


                SetHumanInvestigating(true, context.Activity.From.Id, context.Activity.From.Name);

                return(true);
            }
            else
            {
                var connector = new ConnectorClient(new Uri(context.Activity.ServiceUrl));
                connector.Conversations.ReplyToActivity(context.Activity.Conversation.Id,
                                                        context.Activity.Id,
                                                        new Microsoft.Bot.Connector.Activity
                {
                    Type      = Microsoft.Bot.Connector.DirectLine.ActivityTypes.Message,
                    Text      = Messages.BOT_NO_AGENTS_AVAILABLE,
                    From      = context.Activity.Recipient,
                    Recipient = context.Activity.From,
                    ChannelId = context.Activity.ChannelId
                });
                SetHumanInvestigating(false, null, null);
                return(false);
            }
        }