Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Chat Service

Frank Tarsillo edited this page Dec 6, 2016 · 7 revisions

The chat service provides real time capabilities to manage chat sessions dynamically through use of standard Chat objects and registered listeners. The service can detect unique chat sessions on the network and publish the associated chat objects through registered listeners. The service will keep a cache of chat objects (lazy loaded) that are accessible through the service. Chat objects are automatically enriched with all chat participant detail (SymUser).

The chat service is accessible through a instantiated SymphonyClient instance.

//From SymphonyClient symClient...
//...
symClient.getChatService();

Chat Service Listener

The chat service listener can be registered to the chat service in order to receive callbacks on new chat objects created and removed. Again, chat objects received are fully enriched with user data. The chat service listener only provides events on the creation or termination of new chat objects.

class MyChatManager implements ChatServiceListener{

SymphonyClient symClient;

   public MyChatManager(SymphonyClient symClient){
   this.symClient = symClient;

   //Will notify the bot of new Chat conversations.
   symClient.getChatService().addListener(this);

   }


   @Override
   public onNewChat(Chat chat){
   //New chat object received dynamically
   }

   @Override
   public onChatRemoved(Chat chat){
   //Service removed chat  
   }

}

Chat Object

The chat object manages all properties of the chat conversation or in Symphony terminology the stream. Each chat object can support many listeners that act as callbacks for conversation messages.

The chat object includes the following attributes

  • RemoteUsers (Set of SymUser) - All users that are participating in the conversation (excluding the BOT User)
  • LocalUser (SymUser) - The BOT user
  • Stream (Stream) - The stream object representing the conversation
  • StreamId (String) - The stream ID that represents the conversation (Note: setting stream object will set stream ID)
  • LastMessage (SymMessage) - Last message published in the conversation
  • ChatListeners (Set of ChatListener) - Listeners that receive the published message

Chat (Object) Listener

Each chat object can have one more listeners defined to actually receive new message events. When the chat service publishes a onNewChat(Chat) a ChatListener can be registered to listen for new messages.

As a continuation of the stub examples above..

...
  @Override
   public onNewChat(Chat chat){
   //New chat object received dynamically

   //Register listener
   chat.addListener(new MyChatListener());

   }

   @Override
   public onChatRemoved(Chat chat){
   //Service removed chat  
   }

   class MyChatListener implements ChatListener{
   
   @Override
   void onChatMessage(SymMessage message){
      System.out.println(message.getMessage());
   }

   }

}

Adding a chat session

The chat service supports initiating a chat conversation from the BOT to a destination user(s). Simply create a chat object, populate remotusers (Set of SymUser) and add to the service. The service will validate all users and associated streams. This means that if you populate a stream and remote users for a given chat object, the defined stream can be replaced during verification.

//Creates a Chat session with that will receive messages
Chat chat = new Chat();
chat.setLocalUser(symClient.getLocalUser());
Set<SymUser> remoteUsers = new HashSet<>();
remoteUsers.add(symClient.getUsersClient().getUserFromEmail("first.last@comapany.com");
chat.setRemoteUsers(remoteUsers);

//Lets add a listener as described in previous sections. 
chat.addListener(new MyChatListener());

symClient.getChatService().addChat(chat);

Next topic: Room Service