private static void RenderHeroCard(DirectLineChannelData channelData) { const int Width = 70; Func <string, string> contentLine = (content) => string.Format($"{{0, -{Width}}}", string.Format("{0," + ((Width + content.Length) / 2).ToString() + "}", content)); Console.WriteLine("/{0}", new string('*', Width + 1)); Console.WriteLine("*{0}*", contentLine(channelData.Content.Title)); Console.WriteLine("*{0}*", new string(' ', Width)); Console.WriteLine("*{0}*", contentLine(channelData.Content.Text)); Console.WriteLine("{0}/", new string('*', Width + 1)); }
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 DispatchAsync(IDialogContext context, IAwaitable <Microsoft.Bot.Connector.IMessageActivity> item) { Microsoft.Bot.Connector.Activity activity = (Microsoft.Bot.Connector.Activity) await item; Logger.Info($"message received from {activity.From.Name}/{activity.From.Id} : {JsonConvert.SerializeObject(activity)}"); Logger.Info($"message received to {activity.Recipient.Name}/{activity.Recipient.Id}"); var storage = new AgentStatusStorage( ConfigurationHelper.GetString("BotStatusDBConnectionString")); if (activity.From.Name.EndsWith("@ocsuser")) { //Messages from OCS User, when message from OCS User sent to this method, it has to be coming from DirectLine AgentStatus agent = null; //retrieve ChannelData which includes channelId for our conversation //TODO:figure out a way that simplier JObject o = (JObject)activity.ChannelData; var os = JsonConvert.SerializeObject(o); DirectLineChannelData channelData = JsonConvert.DeserializeObject <DirectLineChannelData>(os); Logger.Info($"ChannelData = {channelData}"); //ConversationStatus conversation = null; Logger.Info($"RoundTrip = {channelData.RoundTrip}"); //first message send to agent, find an available agent //TODO:the agent has been selected in OCS Bot, need to make it sync // Instead of selecting another one here... agent = (await storage.FindAvailableAgentsAsync()).FirstOrDefault(); var convRecord = (await storage.FindConversationActivityAsync(a => a.UserID == agent.Id)).FirstOrDefault(); convRecord.RemoteConversationID = channelData.ConversationId; convRecord.RemoteBotId = activity.From.Id;//remote user id actually... convRecord.RemoteActivity = UrlToken.Encode <ResumptionCookie>( new ResumptionCookie((Microsoft.Bot.Connector.Activity)activity)); convRecord.RemoteUserId = channelData.UserID; convRecord.RemoteUserName = channelData.UserName; await storage.UpdateConversationActivityAsync(convRecord); Logger.Info($"agent:{agent}"); if (!agent.IsLoggedIn) { //Agent somehow is occupied (logout?) Logger.Info("Agent is occupied"); var reply = activity.CreateReply($"Agent is occupied"); ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); await connector.Conversations.ReplyToActivityAsync(reply); } else { //Agnet is available to answer questions, send message to agent Logger.Info("Sending to conversation..."); //TODO:Need to check if current agent is this user //agent.IsOccupied = true; //await storage.UpdateAgentStatusAsync(agent); //First retrieve last conversaion if exists //resumptionCookie = UrlToken.Decode<ResumptionCookie>(conversation.AgentResumptionCookie); var localResumptionCookie = UrlToken.Decode <ResumptionCookie>(convRecord.LocalActivity); Logger.Info($"AgentBot::LocalResumptionCookie:{localResumptionCookie}"); var localActivity = localResumptionCookie.GetMessage(); var localReply = localActivity.CreateReply($"[{activity.From.Name}]{activity.Text}"); var localConnector = new ConnectorClient(new Uri(localActivity.ServiceUrl), new MicrosoftAppCredentials( ConfigurationHelper.GetString("MicrosoftAppId"), ConfigurationHelper.GetString("MicrosoftAppPassword")), true); Microsoft.Bot.Connector.Conversations localConversation = new Microsoft.Bot.Connector.Conversations(localConnector); localConversation.ReplyToActivity(localReply); Logger.Info("done"); return; } } else { resumptionCookie = new ResumptionCookie(await item); await MessageReceivedAsync(context, item); } }