-
Notifications
You must be signed in to change notification settings - Fork 29
Messaging APIs
Message API supports real-time post/delete/list/reception features
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
}
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 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 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
}
}
mentions could be person mention type or group mention type, could make an array contains both.
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 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
}
}
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
}
}
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
}
}
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 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 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 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 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 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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}
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
}
}