public void Agent_subscription() { try { handler = new NotificationHandler(); // Multiple handler.AddSubscription($"v2.users.{_agentId}.presence", typeof(PresenceEventUserPresence)); handler.AddSubscription($"v2.users.{_agentId}.conversations", typeof(ConversationBasic)); handler.NotificationReceived += (data) => { //this.demoThread.Start(); if (data.GetType() == typeof(NotificationData <PresenceEventUserPresence>)) { var presence = (NotificationData <PresenceEventUserPresence>)data; AddLog($"New presence: {presence.EventBody.PresenceDefinition.SystemPresence}"); RefreshForm(presence.EventBody.PresenceDefinition.SystemPresence); } else if (data.GetType() == typeof(NotificationData <ConversationBasic>)) { var conversation = (NotificationData <ConversationBasic>)data; AddLog($"Conversation: {conversation.EventBody.Id} - {conversation.EventBody.Name}"); } }; AddLog("Websocket connected, awaiting messages..."); } catch (Exception ex) { AddLog($"Websocket exception " + ex.Message); } }
/// <summary> /// Subscribe the notification handler to the users' presence and routing statuses. /// </summary> /// <param name="userList"></param> /// <param name="handler"></param> private static void SubscribeToUserGroupPresence(List <User> userList, NotificationHandler handler) { foreach (var user in userList) { handler.AddSubscription($"v2.users.{user.Id}.presence", typeof(UserPresenceNotification)); handler.AddSubscription($"v2.users.{user.Id}.routingStatus", typeof(UserRoutingStatusNotification)); } // Listens for User Presence and Routing Status changes handler.NotificationReceived += (data) => { Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented)); if (data.GetType() == typeof(NotificationData <UserPresenceNotification>)) { var presence = (NotificationData <UserPresenceNotification>)data; Console.WriteLine($"New Presence: {presence.EventBody.PresenceDefinition.SystemPresence}"); Console.WriteLine("****************************************************************************"); } else if (data.GetType() == typeof(NotificationData <UserRoutingStatusNotification>)) { var status = (NotificationData <UserRoutingStatusNotification>)data; Console.WriteLine($"New Status: {status.EventBody.RoutingStatus.Status}"); Console.WriteLine("****************************************************************************"); } }; }
public void TestNotifications() { var handler = new NotificationHandler(); // Start the handler inside of the task to block this test until the notifications come in var tcs = new TaskCompletionSource <bool>(); var busyReceived = false; var availableReceived = false; Task.Factory.StartNew(() => { handler.NotificationReceived += (data) => { try { if (data.GetType() == typeof(NotificationData <PresenceEventUserPresence>)) { var presence = (NotificationData <PresenceEventUserPresence>)data; // Check to see what we got if (presence.EventBody.PresenceDefinition.Id == availablePresenceId) { availableReceived = true; } if (presence.EventBody.PresenceDefinition.Id == busyPresenceId) { busyReceived = true; } // Complete the async task if (busyReceived && availableReceived) { tcs.SetResult(true); } } } catch (InvalidOperationException ex) { // Suppress this error that happens occasionally: // An attempt was made to transition a task to a final state when it had already completed. } catch (Exception ex) { Console.WriteLine(ex); } }; }); // Register topic handler.AddSubscription($"v2.users.{userId}.presence", typeof(PresenceEventUserPresence)); // Change presences presenceApi.PatchUserPresence(userId, "PURECLOUD", new UserPresence() { PresenceDefinition = new PresenceDefinition(busyPresenceId) }); presenceApi.PatchUserPresence(userId, "PURECLOUD", new UserPresence() { PresenceDefinition = new PresenceDefinition(availablePresenceId) }); // The getter for Result will block until the task has completed var result = tcs.Task.Result; // Assert that both worked Assert.AreEqual(busyReceived, true); Assert.AreEqual(availableReceived, true); }
static void Main(string[] args) { var accessTokenInfo = Configuration.Default.ApiClient.PostToken( "clientId", "clientSecret"); Configuration.Default.AccessToken = accessTokenInfo.AccessToken; PureCloudRegionHosts region = PureCloudRegionHosts.us_east_1; Configuration.Default.ApiClient.setBasePath(region); var usersApi = new UsersApi(); var userId = "<REPLACE_WITH_TEST_USER_ID>"; var handler = new NotificationHandler(); var topic = $"v2.users.{userId}.conversations"; handler.AddSubscription(topic, typeof(ConversationEventTopicConversation)); Console.WriteLine($"Subscribed to {topic} topic"); handler.NotificationReceived += (data) => { if (data.GetType() == typeof(NotificationData <ConversationEventTopicConversation>)) { var conversation = (NotificationData <ConversationEventTopicConversation>)data; var conversationId = conversation.EventBody.Id; var eventRawJson = JsonConvert.SerializeObject(data, Formatting.Indented); var eventJsonAsDict = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(eventRawJson); Console.WriteLine("\n-----------------------------------------------"); Console.WriteLine($"Notification for Conversation ID: {conversation.EventBody.Id}"); var eventBodyJsonAsDict = eventJsonAsDict["EventBody"]; var participants = conversation.EventBody.Participants.ToArray(); for (var i = 0; i < participants.Length; i++) { var participant = participants[i]; var participantJsonAsDict = eventBodyJsonAsDict["participants"][i]; if (participant.Purpose == "agent") { Console.WriteLine($"Participant ID: {participant.UserId}"); Console.WriteLine($"Calls:"); var participantCalls = participant.Calls.ToArray(); for (var j = 0; j < participantCalls.Length; j++) { var call = participantCalls[j]; var participantCallJson = participantJsonAsDict["calls"][j]; var stateJson = participantCallJson["state"]; Console.WriteLine($" -Call Id: {call.Id}, State (as Deserialized): {(int)call.State}, State (in JSON): {stateJson}"); } var participantRawJson = JsonConvert.SerializeObject(participantJsonAsDict, Formatting.Indented); Console.WriteLine($"Participant RawJSON (from Event):\n{participantRawJson}"); } } Console.WriteLine("-----------------------------------------------"); } }; Console.WriteLine("Websocket connected, awaiting messages..."); Console.WriteLine("Press any key to stop and remove all subscriptions"); Console.ReadKey(true); handler.RemoveAllSubscriptions(); Console.WriteLine("All subscriptions removed, exiting..."); }