Using the Chat SDK API providers

The Chat SDK API providers encapsulate all of the functionality of the Chat SDK from setting visitor information to more advanced functionality like observing certain events that can occur during a chat.

If you wish to build your own UI, you can use these providers to implement functionality such as sending messages on behalf of the visitor.

Using the API providers alongside the Chat SDK

Please be aware, the Chat SDK has its own connection management, based on the lifecycle of the Activity provided by the Messaging SDK. This is important if you are using the Chat API providers alongside the Chat SDK. The ownership of the connection life-cycle outside of the Chat screen, is in your own hands.

Before using the API providers, make sure you call connect, and set up a connection status observer, to handle the connection state appropriately. Remember to call disconnect before any flow that brings the user back to the chat screen.

Accessing the providers

Providers can be accessed from the Chat singleton:

Chat.INSTANCE.providers().chatProvider();
Chat.INSTANCE.providers().profileProvider();

Observing events

The providers allow you to observe certain events that can occur. The methods take an ObservationToken and an Observer. Example:

ObservationScope observationScope = new ObservationScope();
Chat.INSTANCE.providers().chatProvider().observeChatState(observationScope, new Observer<ChatState>() {
            @Override
            public void update(ChatState chatState) {
                //Do something with chat state
            }
        });

It's important that if you start observing these events that you also remember to stop observing them:

observationScope.cancel()

Getting the availability of agents

You can check the availability of agents and departments:

accountProvider().getAccount(new ZendeskCallback<Account>() {
        @Override
        public void onSuccess(Account account) {
            // Insert your account handling code here
        }

        @Override
        public void onError(ErrorResponse errorResponse) {
            // Handle error in getting Account here
        }
});

For details about the Account class, see the reference docs.

You can also observe changes to the availability of agents:

accountProvider().observeAccount(ObservationScope observationScope,
                                 Observer<Account> observer);

Observing changes to Chat settings

You can be notified of any changes that are made to the settings in the Chat dashboard:

settingsProvider().observeChatSettings(ObservationScope observationScope, Observer<ChatSettings> observer);

For details about the ChatSettings class, see the reference docs.

Observing the connection state

You can listen for connection state changes:

connectionProvider.observeConnectionState(ObservationScope observationScope,
                                          Observer<ConnectionState> observer);

Observing the chat state

You can observe the state of the chat, and gather information about the session:

chatProvider.observeChatState(ObservationScope observationScope,
                              Observer<ChatState> observer);

The ChatState object has the following properties:

Property Description
agents List of agents in the chat.
logs Array of ChatLog items that represent the messages in the conversation.
queuePosition The visitor's current queue position.
isChatting Whether the visitor has started chatting with an agent.
department Department requested by the visitor.
chatSessionStatus The phase of the current chat session. See the reference docs.
rating The rating of the current session given by the visitor.
comment The rating comment of the current session given by the visitor.

For details about the ChatState class, see the reference docs.

Sending a message

chatProvider().sendMessage(String message)

You can try resending or deleting a failed message with the following methods, which take the chatLogId as an argument:

chatProvider().deleteFailedMessage(String failedChatLogId);
chatProvider().resendFailedMessage(String failedChatLogId);

Sending an attachment

An attachment can be sent by providing a File, and the FileUploadListener will provide the progress of the upload.

chatProvider().sendFile(File file, FileUploadListener fileUploadListener);

You can resend or delete a file with the following methods, which take the chatLogId as an argument:

chatProvider().resendFailedFile(String chatLogId, FileUploadListener fileUploadListener);
chatProvider().deleteFailedMessage(String failedChatLogId);

Ending a chat

You can end a chat at any point and use the callback to verify whether it was successful.

chatProvider().endChat(ZendeskCallback<Void> callback)

Adding and removing tags

To append a specific tag:

profileProvider().addVisitorTags(List<String> tags, ZendeskCallback<Void> callback);

To remove a tag that has already been added:

profileProvider().removeVisitorTags(List<String> tags, ZendeskCallback<Void> callback);

If you need to clear all tags that have been added to a chat, you will need to keep track of each possible tag.

Sending a chat rating

chatProvider().sendChatRating(ChatRating chatRating, ZendeskCallback<Void> callback);

Sending a chat comment

chatProvider().sendChatComment(String chatComment, ZendeskCallback<Void> callback);

Sending an offline message

This method should only be used to send an offline message to an offline account or department. A new chat is created every time this method is called. If your Chat account is linked to a Support account, a ticket will automatically be created for each message.

VisitorInfo visitorInfo = VisitorInfo.builder()
                                 .withName(String name)
                                 .withEmail(String email);

OfflineForm offlineForm = OfflineForm.builder(String message)
                                     .withVisitorInfo(visitorInfo)
                                     .withDepartmentName(String departmentName);

chatProvider().sendOfflineForm(OfflineForm offlineForm, ZendeskCallback<Void> callback);

Checking for info about an ongoing chat

You can check if there's already an ongoing conversation before starting the chat.

Note: This API proactively connects to web sockets for authenticated users. This connection is kept alive. You should call disconnect() on the ConnectionProvider if you don't need to keep it open.

chatProvider().getChatInfo(ZendeskCallback<ChatInfo> callback);