private static void DisplayList(HubsterAuthorizer authorizer, HubsterDirectClientBusiness client, EstablishedConversationModel conversation) { var apiResponse = client.Activity.Get(authorizer, conversation.ConversationId.Value, 0, IntegrationType.Agent); if (apiResponse.StatusCode != HttpStatusCode.OK) { var issues = JsonConvert.SerializeObject(apiResponse.Errors, Formatting.Indented); Display(issues, ConsoleColor.Yellow); return; } Console.Clear(); if (apiResponse.Content.Any()) { foreach (var activity in apiResponse.Content) { Display(activity); } } else { Display("There are no interactions to display.", ConsoleColor.Yellow); } }
public static void Run() { var auth = new HubsterAuthClient(hostUrl: "http://host.docker.internal:5000", onAuthRequest: (authClient) => { // typically this will be a call to some backend service that will return back the token var apiResponse = authClient.GetClientToken("hubster.engine.api.00000000000000000000000000000001", "9c5Vbnd0vZGlqTdBzhz9hb9cQ0M="); return(apiResponse); }); var origin = "http://localhost"; var client = new HubsterDirectClientBusiness(origin, "http://host.docker.internal:5002", "http://host.docker.internal:5005"); var authorizer = new HubsterAuthorizer(auth); while (true) { var startChatting = Commands(authorizer, client); if (startChatting == false) { return; } Console.Clear(); var username = GetUserName(_lastConverstion); var eventResponse = client.Events.Start(options => { options.Authorizer = authorizer; options.Origin = origin; options.IntegrationId = Guid.Parse(_agentIntegrationId); options.OnConnected = () => Display("Connected", ConsoleColor.Cyan); options.OnDisconnected = () => Display("Disconnected", ConsoleColor.Yellow); options.OnActivity = (activity) => Display(activity); options.OnError = (error) => Display(error.Description, ConsoleColor.Yellow); }); if (eventResponse.StatusCode != HttpStatusCode.OK) { Display(eventResponse); Console.ReadKey(); return; } ConversationLoop(authorizer, client, _lastConverstion, username); client.Events.Stop(eventResponse.Content); } }
private static void ConversationLoop(HubsterAuthorizer authorizer, HubsterDirectClientBusiness client, EstablishedConversationModel conversation, string username) { while (true) { Console.Write($"Sending message to: [CUSTOMER - '{username}']: "); var message = Console.ReadLine(); if (message.ToLower() == "q" || message.ToLower() == "quit") { return; } if (message.ToLower() == "clear") { Console.Clear(); continue; } if (string.IsNullOrWhiteSpace(message)) { continue; } var apiResponse = client.Activity.Send(authorizer, conversation.ConversationId.Value, new DirectActivityModel { Sender = new DirectSourceModel { IntegrationId = _agentIntegrationId }, Message = new DirectTextMessageModel { Text = message }, }); if (apiResponse.StatusCode != HttpStatusCode.OK) { Display(apiResponse); } } }
static void SendActivityToCustomer(HubsterAuthorizer authorizer) { var direct = new HubsterDirectClientBusiness("http://localhost", "http://localhost", "http://localhost:8251"); var conResponse = GetEstablishedConversation(authorizer); if (conResponse.StatusCode == HttpStatusCode.OK) { var actResponse = direct.Activity.Send(authorizer, conResponse.Content.ConversationId.Value, new DirectActivityModel { Sender = new DirectSourceModel { IntegrationId = "00000000-0000-0000-0000-000000000002", }, Message = new DirectTextMessageModel { Text = "Hello from Business TestPlayGround " }, }); } }
private static bool Commands(HubsterAuthorizer authorizer, HubsterDirectClientBusiness client) { Console.Clear(); while (true) { var lastUser = GetUserName(_lastConverstion); Console.WriteLine($"Select a commands:"); Console.WriteLine($"1. List available conversations."); Console.Write($"2. Start chatting with "); DisplaySimpleText($"'{lastUser ?? "You must first select a conversation!"}'", ConsoleColor.Yellow); Console.WriteLine(); Console.WriteLine($"3. List interactions."); Console.WriteLine($"4. Clear screen (clear)."); Console.WriteLine($"5. Quit (q or quit)."); var selection = Console.ReadLine().ToLower(); if (string.IsNullOrWhiteSpace(selection) || selection == "4" || selection == "clear") { Console.Clear(); continue; } if (selection == "5" || selection == "q" || selection == "quit") { return(false); } if (int.TryParse(selection, out int result) == false) { Console.Clear(); continue; } if (result == 1) { if (PickAvailableConversaton(authorizer, client) == true) { Console.Clear(); } } if (result == 2) { if (_lastConverstion == null) { Console.WriteLine(); Display("Use must first select an available conversation before you can commence chatting.", ConsoleColor.Yellow); Console.WriteLine(); continue; } return(true); } if (result == 3) { if (_lastConverstion == null) { Console.WriteLine(); Display("Use must first select an available conversation before you can list interactions", ConsoleColor.Yellow); Console.WriteLine(); continue; } DisplayList(authorizer, client, _lastConverstion); Console.WriteLine(); Console.WriteLine("Press any key to continue"); Console.ReadKey(); continue; } } }
private static bool PickAvailableConversaton(HubsterAuthorizer authorizer, HubsterDirectClientBusiness client) { var apiResponse = client.Conversation.GetAllEstablishedByHubId(authorizer, Guid.Parse(_agentHubId)); if (apiResponse.StatusCode != HttpStatusCode.OK) { Console.WriteLine(); Display(apiResponse); Console.WriteLine(); return(false); } if (apiResponse.Content.Any()) { while (true) { Console.Clear(); Console.WriteLine("Pick one of the following established conversations:"); var i = 1; foreach (var conversation in apiResponse.Content.OrderByDescending(x => x.OpenedDateTime)) { Console.WriteLine($"{i++}. {conversation.ConversationId} - {GetUserName(conversation)}"); } Console.WriteLine($"{i}. Clear Screen (clear)"); Console.WriteLine($"{i+1}. Quit (q or quit)"); var selection = Console.ReadLine().ToLower(); if (string.IsNullOrWhiteSpace(selection) || selection == i.ToString() || selection == "clear") { Console.Clear(); continue; } if (selection == (i + 1).ToString() || selection == "q" || selection == "quit") { Console.Clear(); return(false); } if (int.TryParse(selection, out int result) == false) { Console.Clear(); continue; } if (result < 0 || result > i) { Console.Clear(); continue; } _lastConverstion = apiResponse.Content.ToList()[result - 1]; return(true); } } else { Display("There are no established conversations to display.", ConsoleColor.Yellow); return(false); } }