Chat Methods

All methods are current as of version 4.7.0.

UsedeskChatSdk methods

1. Set the chat configuration

fun setConfiguration(chatConfiguration: UsedeskChatConfiguration)

2. Get the configuration

fun requireConfiguration(): UsedeskChatConfiguration

3. Initialize the chat

fun init(
    context: Context,
    chatConfiguration: UsedeskChatConfiguration = requireConfiguration()
): UsedeskChat

If the configuration has already been set via setConfiguration, the second argument can be omitted.

Example:

val usedeskChat = UsedeskChatSdk.init(requireContext())
val usedeskChat = UsedeskChatSdk.init(requireContext(), chatConfiguration)

4. Get the initialized chat instance

fun getInstance(): UsedeskChat?

Returns null if the chat has not been initialized yet.

5. Get the instance with a check

fun requireInstance(): UsedeskChat

Same as getInstance(), but throws an exception if the chat has not been initialized.

6. Release the chat resources

fun release(force: Boolean = true)

Arguments:

Name Type Description
force Boolean Hard release flag: true releases the resources immediately, false releases them only if there are no event listeners


7. Initialize UsedeskPreparation

fun initPreparation(
    context: Context,
    chatConfiguration: UsedeskChatConfiguration = requireConfiguration()
): UsedeskPreparation

Example:

val usedeskChatPreparation = UsedeskChatSdk.initPreparation(requireContext(), chatConfiguration)

8. Release UsedeskPreparation

fun releasePreparation()

9. Set the notifications service factory

fun setNotificationsServiceFactory(
    notificationsServiceFactory: UsedeskNotificationsServiceFactory?
)

Arguments:

Name Type Description
notificationsServiceFactory UsedeskNotificationsServiceFactory? Notifications service factory. Pass null to reset a previously set one


10. Set a custom messages repository

fun setUsedeskMessagesRepository(usedeskMessagesRepository: UsedeskMessagesRepository?)

Arguments:

Name Type Description
usedeskMessagesRepository UsedeskMessagesRepository? Your own implementation of the drafts and unsent messages storage. Pass null to restore the default one


11. Start the notifications service

fun startService(context: Context)

12. Stop the notifications service

fun stopService(context: Context)


UsedeskChat methods

1. Add an event listener

fun addActionListener(listener: UsedeskActionListener)

2. Remove an event listener

fun removeActionListener(listener: UsedeskActionListener)

3. Check whether there are any listeners

fun isNoListeners(): Boolean

Example:

if (UsedeskChatSdk.requireInstance().isNoListeners()) { ... }

4. Send a text message

fun send(
    textMessage: String,
    localId: String? = null
)

Example:

usedeskChat.send("Hello")

5. Send a single file

fun send(
    fileInfo: UsedeskFileInfo,
    localId: String? = null
)

6. Send several files

fun send(fileInfoList: Collection<UsedeskFileInfo>)

Example:

val fileInfoList = listOf(UsedeskFileInfo.create(fileUri))
usedeskChat.send(fileInfoList)

7. Subscribe to the file upload progress

fun addFileUploadProgressListener(
    localMessageId: String,
    listener: IFileUploadProgressListener
)

The listener receives onProgress(sent: Long, total: Long) calls as the file is being uploaded.

8. Unsubscribe from the file upload progress

fun removeFileUploadProgressListener(
    localMessageId: String,
    listener: IFileUploadProgressListener
)

9. Rate a message

fun sendRating(
    agentMessage: UsedeskMessageAgentText,
    ratingId: String
)

Arguments:

Name Type Description
agentMessage UsedeskMessageAgentText The agent message being rated
ratingId String Identifier of the selected option — the id of one of agentMessage.rating.options


If the message has no rating (rating == null) or ratingId does not match any option in options, the call does nothing.

After a successful send the message is updated: the passed ratingId is stored in rating.selectedId. On error the send is retried automatically.

Example:

fun onRatingClick(agentMessage: UsedeskMessageAgentText, optionId: String) {
    usedeskChat.sendRating(agentMessage, optionId)
}

10. Resend an unsent message

fun sendAgain(messageId: String)

11. Remove an unsent message

fun removeMessage(messageId: String)

12. Set the message draft

fun setMessageDraft(messageDraft: UsedeskMessageDraft)

13. Get the message draft

fun getMessageDraft(): UsedeskMessageDraft

14. Send the message draft

fun sendMessageDraft()

15. Load the previous page of messages

fun loadPreviousMessagesPage()

16. Load a message form

fun loadForm(messageId: String)

17. Save a message form

fun saveForm(form: UsedeskForm)

18. Send a message form

fun send(form: UsedeskForm)

19. Send an offline form

fun send(
    offlineForm: UsedeskOfflineForm,
    onResult: (SendOfflineFormResult) -> Unit
)

Example:

val offlineForm = UsedeskOfflineForm(
    "John Smith",
    "john.smith@email.com",
    "Hello"
)
usedeskChat.send(offlineForm) { result -> ... }


UsedeskPreparation methods

1. Create a chat

fun createChat(
    apiToken: String,
    onResult: (CreateChatResult) -> Unit
)

Example:

usedeskPreparation.createChat(apiToken) { result -> ... }