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 DirectLineClient createNewClientConversation(out string convID) { string secret = "IK1o_f1fBYc.cwA.LZs.ZrbtNGSTAYzcLqc6DPbq_0bBOV_zzyMSYn7KCaoVdpI"; Uri uri = new Uri("https://directline.botframework.com"); var creds = new DirectLineClientCredentials(secret); DirectLineClient client = new DirectLineClient(uri, creds); Conversations convs = new Conversations(client); var conv = convs.StartConversation(); convID = conv.ConversationId; return(client); }
public async Task StartBotConversation() { try { //var tokenResponse = await new DirectLineClient(Constants.DirectLineSecret).Tokens.GenerateTokenForNewConversationAsync(); //client = new DirectLineClient(tokenResponse.Token); var credential = new DirectLineClientCredentials(Constants.DirectLineSecret, Constants.BotEndPoint); client = new DirectLineClient(credential); conversation = await client.Conversations.StartConversationAsync(); webSocket = new WebSocket(conversation.StreamUrl); webSocket.OnMessage += WebSocket_OnMessage; webSocket.OnClose += WebSocket_OnClose; webSocket.OnError += WebSocket_OnError; webSocket.ConnectAsync(); try { botConnectionRenewer.Change(TimeSpan.FromMinutes(5), TimeSpan.FromMilliseconds(-1)); } catch (Exception ex) { Console.WriteLine(string.Format("Failed to reschedule the timer to renew access token. Details: {0}", ex.Message)); } } catch (Exception) { botConnectionRenewer.Change(Timeout.Infinite, Timeout.Infinite); if (webSocket != null) { webSocket.OnMessage -= WebSocket_OnMessage; webSocket.OnClose -= WebSocket_OnClose; webSocket.OnError -= WebSocket_OnError; } Device.BeginInvokeOnMainThread(() => { App.Current.MainPage.DisplayAlert("Error", "Problem occurred while connecting with Bot. " + Constants.ErrorString, "OK"); }); } }
private async void button1_Click(object sender, EventArgs e) { try { string secretout = appid + ":" + secret; var uri = new Uri("https://directline.botframework.com"); string newsecret = "zHhveP4jk4o.cwA.cIA.QERY2t7safaD557XAYfyFp0WZrpQji3s2nI7QbPunSQ"; DirectLineClientCredentials creds = new DirectLineClientCredentials(newsecret); DirectLineClient client = new DirectLineClient(uri, creds); Microsoft.Bot.Connector.DirectLine.Conversations convs = new Microsoft.Bot.Connector.DirectLine.Conversations(client); string waterMark; var conv = convs.NewConversation(); var set = convs.GetMessages(conv.ConversationId); waterMark = set.Watermark; Microsoft.Bot.Connector.DirectLine.Models.Message message = new Microsoft.Bot.Connector.DirectLine.Models.Message(conversationId: conv.ConversationId, text: textBox1.Text); //Console.WriteLine(message.Text); convs.PostMessage(conv.ConversationId, message); set = convs.GetMessages(conv.ConversationId, waterMark); PrintResponse(set); waterMark = set.Watermark; string x = await MakeRequest(textBox1.Text); //System.Threading.Thread.Sleep(15000); if (x != String.Empty) { textBox2.Text += " Return JSON: " + x + System.Environment.NewLine; } } catch (Exception ex) { throw ex; } }
public async Task StartBotConversation() { try { userAccount = new ChannelAccount(Constants.Email); var credential = new DirectLineClientCredentials(Constants.DirectLineSecret, Constants.BotEndPoint); client = new DirectLineClient(credential); conversation = await client.Conversations.StartConversationAsync(); readMessageThread = new Thread(() => ReadBotMessagesAsync(client, conversation.ConversationId)); readMessageThread.Start(); } catch (Exception) { Device.BeginInvokeOnMainThread(() => { App.Current.MainPage.DisplayAlert("Error", "Problem occurred while connecting with Bot. " + Constants.ErrorString, "OK"); }); } }
static async Task Main(string[] args) { var creds = new DirectLineClientCredentials(botDirectLineSecret); var client = new DirectLineClient(creds); var conversation = await client.Conversations.StartConversationAsync(); using (var webSocketClient = new WebSocket(conversation.StreamUrl)) { webSocketClient.OnMessage += WebSocketClient_OnMessage; webSocketClient.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12; webSocketClient.Connect(); // Optional, helps provide additional context on the user for some skills/scenarios await SendStartupEvents(client, conversation); while (true) { var input = System.Console.ReadLine().Trim(); if (input.ToLower() == "exit") { break; } else { if (input.Length > 0) { var userMessage = new Activity { From = new ChannelAccount(fromUserId, fromUserName), Text = input, Type = ActivityTypes.Message }; await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage); } } } } }
static async Task Main(string[] args) { DirectLineClientCredentials creds = new DirectLineClientCredentials(botDirectLineSecret); DirectLineClient client = new DirectLineClient(creds); Conversation conversation = await client.Conversations.StartConversationAsync(); using (var webSocketClient = new WebSocket(conversation.StreamUrl)) { webSocketClient.OnMessage += WebSocketClient_OnMessage; webSocketClient.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12; webSocketClient.Connect(); // The Contoso Assistant Bot requires a "startup" event to get going, this is NOT needed for other bots await SendStartupEvent(client, conversation); while (true) { string input = Console.ReadLine().Trim(); if (input.ToLower() == "exit") { break; } else { if (input.Length > 0) { Activity userMessage = new Activity { From = new ChannelAccount(fromUser), Text = input, Type = ActivityTypes.Message }; await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage); } } } } }
private void button1_Click(object sender, EventArgs e) { try { string secretout = appid + ":" + secret; var uri = new Uri("https://directline.botframework.com"); string newsecret = "WQcy4GowAQY.cwA.k3g.znNIK2YL4KHjYFlNMd0iND2ag67ONPy7jbOnUsKWhZk"; DirectLineClientCredentials creds = new DirectLineClientCredentials(newsecret); DirectLineClient client = new DirectLineClient(uri, creds); Conversations convs = new Conversations(client); string waterMark; var conv = convs.NewConversation(); var set = convs.GetMessages(conv.ConversationId); waterMark = set.Watermark; Microsoft.Bot.Connector.DirectLine.Models.Message message = new Microsoft.Bot.Connector.DirectLine.Models.Message(conversationId: conv.ConversationId, text: textBox3.Text); //Console.WriteLine(message.Text); convs.PostMessage(conv.ConversationId, message); set = convs.GetMessages(conv.ConversationId, waterMark); PrintResponse(set); waterMark = set.Watermark; MakeRequest(); if (retval != String.Empty) { textBox1.Text += " Return JSON: " + retval + System.Environment.NewLine; } } catch (Exception ex) { throw ex; } }
private void button3_Click(object sender, EventArgs e) { try { string secretout = appid + ":" + secret; //var uri = new Uri("https://directline.botframework.com"); var uri = new Uri(ConfigurationManager.AppSettings["DirectLineURL"].ToString()); //string newsecret = "zHhveP4jk4o.cwA.cIA.QERY2t7safaD557XAYfyFp0WZrpQji3s2nI7QbPunSQ"; string newsecret = ConfigurationManager.AppSettings["DirectLineSecret"].ToString(); DirectLineClientCredentials creds = new DirectLineClientCredentials(newsecret); DirectLineClient client = new DirectLineClient(uri, creds); Microsoft.Bot.Connector.DirectLine.Conversations convs = new Microsoft.Bot.Connector.DirectLine.Conversations(client); string waterMark; var conv = convs.NewConversation(); var set = convs.GetMessages(conv.ConversationId); waterMark = set.Watermark; Microsoft.Bot.Connector.DirectLine.Models.Message message = new Microsoft.Bot.Connector.DirectLine.Models.Message(conversationId: conv.ConversationId, text: textBox1.Text); //Console.WriteLine(message.Text); convs.PostMessage(conv.ConversationId, message); set = convs.GetMessages(conv.ConversationId, waterMark); PrintResponse(set); waterMark = set.Watermark; } catch (Exception ex) { throw ex; } }
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; } }