Skip to content

Messaging APIs

Akshay Agarwal edited this page Mar 1, 2023 · 3 revisions

Message API supports real-time post/delete/list/reception 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.onSyncingSpacesStatusChanged = { isSpacesSyncInProgress in
    // isSpacesSyncInProgress will be true if Spaces Sync is InProgress
    // isSpacesSyncInProgress will be false if Spaces Sync is completed
 }

Screenshot 2023-03-01 at 4 15 07 PM

Post Text Message

Post text message to person id

post a simple text message to a person with personId.

webex.messages.post(personId: "{personId}", text: "text..."){ response in
     switch response.result {
         case .success(let message):
             // ...
             break
         case let .failure(let error):
             // ...
             break
     }
}

Post text message to person email

post a simple text message to a person with personEmail.

guard let emailAddress = EmailAddress.fromString("{person email address}") else{ return }

webex.messages.post(personEmail: emailAddress, text: "text..."){ response in
     switch response.result {
         case .success(let message):
             // ...
             break
         case let .failure(let error):
             // ...
             break
     }
}

Post text message to a room

post a text message to a room with roomId.

webex.messages.post(roomId: "{roomId}", text: "text..."){ response in
     switch response.result {
         case .success(let message):
             // ...
             break
         case let .failure(let error):
             // ...
             break
     }
}

Post Mentions

mentions could be person mention type or group mention type, could make an array contains both.

Post single mentions

post a message include person mention type items.

//create person mention items with personId
let mentionItem1 = Mention.person("{personId1}")
let mentionItem2 = Mention.person("{personId2}")
let mentionArray = [mentionItem1, mentionItem2]

webex.messages.post(roomId: "{roomId}", text: "text...", mentions: mentionArray){ response in
     switch response.result {
         case .success(let message):
             // ...
             break
         case let .failure(let error):
             // ...
             break
     }
}

Post a group mention

post a message include group mention type item.

//create a group mention item.
let mentionItem = Mention.all
let mentionArray = [mentionItem]

webex.messages.post(roomId: "{roomId}", text: "text...", mentions: mentionArray){ response in
     switch response.result {
         case .success(let message):
             // ...
             break
         case let .failure(let error):
             // ...
             break
     }
}

Post Files

Post simple files

created two local file items with local paths, and post a message contains those files

let fileItem1 = LocalFile.init(path: "{local file path}")
let fileItem2 = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}")
let fileArray = [fileItem1, fileItem2]

webex.messages.post(roomId: "{room id}", text: "{text...}", files: fileArray){ response in
    switch response.result {
        case .success(let message):
            // ...
            break
        case let .failure(let error):
            // ...
            break
    }
}

Post files with uploading progress

created a file item with local path. Post message with that file, and watch uploading progress.

guard let fileItem  = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}", 
    progressHandler: { (progress) in // progress as double }) else {return}

webex.messages.post(roomId: "{room id}", text: "{text...}", files: [fileItem]){ response in
    switch response.result {
        case .success(let message):
            // ...
            break
        case let .failure(let error):
            // ...
            break
    }
}

Post files with thumb nail

created a file item with thumbnail, and post message contains that file.

let fileThumb = LocalFile.Thumbnail.init(path: "{file path}", mime: "{mime type}", width: 320, height: 400)
guard let fileItem  = LocalFile.init(path: "{local file path}", name: "{file name}", mime: "{file mime type}", 
    thumbnail: fileThumb, progressHandler: { (progress) in // progress as double }) else {return}

webex.messages.post(roomId: "{room id}", text: "{text...}", files: [fileItem]){ response in
    switch response.result {
        case .success(let message):
            // ...
            break
        case let .failure(let err):
            // ...
            break
    }
}

Delete Message

delete a message with messageId

webex.messages.delete(messageId: "{message id}", completionHandler: { (response) in
    switch response.result{
        case .success(_):
            // delete success..
            break
        case .failure(let err):
            // delete fail...
            break
        }
})

List Messages

List messages before one date point

list messages before one particular date point with roomId, the 'max' is the maximum count expect to return.

let before = Before.date("{one date point}")

webex.messages.list(roomId: "{room id}", before: before, max: 50) { response in
    switch response.result {
    case .success(let messages):
        // ...
        break
    case .failure(let err):
        // ...
        break
    }
}

List messages before one particular message

list messages before one particular message with roomId, the 'max' is the maximum count expect to return.

let beforeMessage = Before.message("{message id}")

webex.messages.list(roomId: "{room id}", before: beforeMessage, max: 50) { response in
    switch response.result {
    case .success(let messages):
       // ...
       break
    case .failure(let err):
        // ...
        break
     }
}

List messages that mentioned me

list messages that mentioned me in a group room with roomId, the 'max' is the maximum count expect to return.

let mentioned = Mention.person("me")
webex.messages.list(roomId: "{room id}", max: 50, mentionedPeople: mentioned) { response in
    switch response.result {
    case .success(let messages):
        // ...
        break
    case .failure(let err):
        // ...
        break
    }
}

Message Reception Events

message reception with two events, messageReceived and messageDeleted.

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        // received message...
        break
    case .messageDeleted(let messageId):
        // deleted messageId...
        break
    }
}

File Download

Download simple files

receive a message contains files, and download files.

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        if let files = message.files {
            for file in files {
                webex.messages.downloadFile(file) { result in
                    if let localUrl = result.data{
                        // file download success in localUrl ...
                    }
                }
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}

Download files into particular destination

receive a message contains files, and download files into particular destination directory.

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        let destination = URL.init(string: "{destination directory}")
        if let files = message.files {
            for file in files {
               webex.messages.downloadFile(file, to: destination) { result in
                    if let localUrl = result.data{
                       // file download success in localUrl ...
                    }
               }
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}

Download files with progress handler

receive a message contains files, and download files with download progress handlers.

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        if let files = message.files {
            for file in files {
               webex.messages.downloadFile(file, progressHandler: { progress in
                      // progress in double
               }) { result in
                    if let localUrl = result.data{
                      // file download success in localUrl ...
                  }
               } 
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}

Download thumb nails

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        if let files = message.files {
            for file in files {
                webex.messages.downloadThumbnail(for: file) { result in
                    if let localUrl = result.data{
                        // file download success in localUrl ...
                    }
                }
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}

Download thumb nails into particular destination

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        let destination = URL.init(string: "{destination directory}")
        if let files = message.files {
            for file in files {
               webex.messages.downloadThumbnail(for: file, to: destination) { result in
                    if let localUrl = result.data{
                       // file download success in localUrl ...
                    }
               }
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}

Download thumb nails with progress handler

webex.messages.onEvent = { messageEvent in
    switch messageEvent{
    case .messageReceived(let message):
        if let files = message.files {
            for file in files {
               webex.messages.downloadThumbnail(for: file, progressHandler: { progress in
                      // progress in double
               }) { result in
                    if let localUrl = result.data{
                      // file download success in localUrl ...
                  }
               } 
            }
        }
        break
    case .messageDeleted(let messageId):
        // ...
        break
    }
}
Clone this wiki locally