Skip to content

Messaging APIs

raghsund edited this page Mar 1, 2023 · 6 revisions

Message API supports real-time post/delete/list/edit features

Features & Usage

NOTE:

Before making any API calls on the spaces or using spaceId for loading messages in a space or making a space call use the following APIs to be sure all spaces are correctly synced in the local data warehouse.

// Denotes if syncing latest conversations to local data warehouse is complete.
// returns true if Spaces Sync is completed
// returns false if Spaces Sync is InProgress
webex.spaces.isSpacesSyncCompleted 
    
// The callback handler for when syncing status for spaces changes.
webex.spaces.setOnSpaceSyncingStatusChangedListener( CompletionHandler { result->
    if(result.isSuccessful){
        //result.data gives the sync status
        if(result.data){
        // result.data will be true if Spaces Sync is InProgress
        }
        else{
        // result.data will be false if Spaces Sync is completed
        }
    }
}
Screenshot 2023-03-01 at 3 25 52 PM

Get Message

API to get a message object by corresponding message ID. messageId represents the ID of a message

webex.messages.get(messageId, CompletionHandler { result ->
    if (result.isSuccessful) {
        // result.data gives the message object
    } else {
        // result.error?.errorMessage gives error message
    }
})

Post Text Message

Post text message to person id

Post a simple text message to a person with personId.

webex.messages.postToPerson(personId, message, null, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Success. result.data contains the Message object
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

Post text message to person email

Post a simple text message to a person with personEmail.

val emailAddress = EmailAddress.fromString(email) // email is the Email address in string format
webex.messages.postToPerson(emailAddress, message, files, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Success. result.data contains the Message object
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

Post text message to a space

Post a text message to a space with a spaceId.

val text: Message.Text = Message.Text.plain("some_message")
webex.messages.postToSpace(spaceId, text, null, null, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Success. result.data contains the Message object
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

Post message to a space or a person using a Draft object

Can be used to send files, mention people and reply to a parent message.

target is the destination to which message is being sent. It can be PersonId, Space ID, Email address

draft is of type Message.Draft

webex.messages.post(target, draft, CompletionHandler { result ->
    if (result.isSuccessful) {
        // result.data contains the message object
    } else {
        // result.error?.errorMessage gives the error message
    }
})

Post Mentions

Mentions can be of two types -

  • Person : Mention a particular person
  • Group : Mention a group

Post single mentions

Post a message with mention type as Person ID

//create person mention items with personId
val mentionsList = ArrayList<Mention>()
mentionsList.add(Mention.Person("personId1", startIndex, endIndex))
mentionsList.add(Mention.Person("personId2", startIndex, endIndex))
webex.messages.postToSpace(spaceId, text, mentionsList, files, completionHandler)

NOTE: startIndex and endIndex are the index positions in the text message which will indicate the mention object. For eg. to mention XYZ in the text Hello XYZ, the index position would be 6 and 8, where 6 is the startIndex and 8 is the endIndex

Post a group mention

Post a message with mention type as group.

// Create a group mention item
val mentionsList = ArrayList<Mention>()
mentionsList.add(Mention.All())
webex.messages.postToSpace(spaceId, text, mentionsList, files, completionHandler)

Post Files

Post simple files

val text = Message.Text.plain("hello world")
// Create a File object
val tempFile = File.createTempFile("tempFile", ".txt")
// Create a local file object
val localFile = LocalFile(tempFile, "text/html", null, null)
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)

Post files with an uploading progress handler

val text = Message.Text.plain("hello world")
// Create a File object
val tempFile = File.createTempFile("tempFile", ".txt")
// Create a local file object
val localFile = LocalFile(tempFile, "text/html", null, object : MessageClient.ProgressHandler {
    override fun onProgress(bytes: Double) {
        // Use the bytes value to denote quantity uploaded
    }
})
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)

Post files with a thumbnail

// Create a File object
val file = File.createTempFile("tempFile", ".txt")
// Create a thumbnail
var thumbnail = LocalFile.Thumbnail(file, null, 320, 400)
// Create a local file object
val localFile = LocalFile(file, "text/html", thumbnail, object : MessageClient.ProgressHandler {
    override fun onProgress(bytes: Double) {
        // Use the bytes value to denote quantity uploaded
    }
})
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)

Edit Message

Edit a message with a Message object

webex.messages.edit(messageObject, text, mentions, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Edit success
        val newMessageObject = result.data
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

Delete Message

Delete a message with a messageId

webex.messages.delete(messageId, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Message deleted successfully
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

List Messages

List messages before one date point

List messages in a space before a particular Date, the 'max' is the maximum number of messages to be returned.

val before = Before.Date(date)
webex.messages.list(spaceId, before, max, mentions, { result ->
    if (result.isSuccessful) {
        // result.data contains the messages
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

List messages before one particular message

List messages before a particular messageId in a space, the 'max' is the maximum number of messages to be returned.

val before = Before.Message("messageId")
webex.messages.list(spaceId, before, max, mentions, { result ->
    if (result.isSuccessful) {
        // result.data contains the messages
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

List messages that mentions specific person

List messages that mentioned some person with given personId in a space, the 'max' is the maximum number of messages to be returned.

val mentionsList : ArrayList<Mention> = ArrayList()
mentionsList.add(Mention.Person(personIdOfSomePersonInSpace))
mentionsList.add(Mention.Person(personIdOfAnotherPersonInSpace))
webex.messages.list(spaceId, null, max, mentionsList, { result ->
    if (result.isSuccessful) {
        // result.data contains the messages
    } else {
        // Error occurred. result.error?.errorMessage provides the error message
    }
})

Message Events

Set message observer to receive message events

webex.messages.setMessageObserver(object : MessageObserver {
    override fun onEvent(event: MessageObserver.MessageEvent) {
        when (event) {
            is MessageObserver.MessageReceived -> {
                // event.getMessage() gives the Message object
            }
            is MessageObserver.MessageDeleted -> {
                ...
            }
            is MessageObserver.MessageFileThumbnailsUpdated -> {
                ...
            }
            is MessageObserver.MessageEdited -> {
                ...
            }
            is MessageObserver.MessagesUpdated -> {
                ...
            }
        }
    }
})

File Download

Download files

Receive a message containing files, and download files.

remoteFile is the target file which is to be downloaded. It is of type Remote File

file is the local file directory in which the downloaded file is to be saved.

webex.messages.downloadFile(remoteFile, file,
    object : MessageClient.ProgressHandler {
        override fun onProgress(bytes: Double) {
        }
    },
    CompletionHandler { fileUrlResult ->
        if (fileUrlResult.isSuccessful) {
            // Download successful. fileUrlResult.data gives the file URI
        } else {
            // Error occurred. fileUrlResult.error?.errorMessage gives the error message
        }
    }
)

Download thumbnails

API to download thumbnail associated with a remote file object.

remoteFile is the target file, thumbnail of which is to be downloaded. It is of type Remote File

file is the local file directory in which the thumbnail is to be saved.

webex.messages.downloadThumbnail(remoteFile, file, CompletionHandler { result ->
    if (result.isSuccessful) {
        if (result.data != null) {
            // result.data gives the thumbnail URI
        } else {
            // Error occurred
        }
    } else {
        emitter.onError(Throwable(result.error?.errorMessage))
    }
})

Mark message as read

Mark messages sent before the specified messageId in the space as read, including the specified message with its messageId. If this value is null, then mark all messages in the space as read.

messageId is the ID of the message

webex.messages.markAsRead(spaceId, messageId, CompletionHandler { result ->
    if (result.isSuccessful) {
        // Message is marked as read
    } else {
        // result.error?.errorMessage gives the error message
    }
})
Clone this wiki locally