You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -101,17 +105,17 @@ Replace the comment `<CREATE A CHAT CLIENT>` with the following code:
101
105
102
106
```
103
107
let endpoint = "<ACS_RESOURCE_ENDPOINT>"
104
-
let credential =
105
-
try CommunicationTokenCredential(
106
-
token: "<ACCESS_TOKEN>"
107
-
)
108
-
let options = AzureCommunicationChatClientOptions()
109
-
110
-
let chatClient = try ChatClient(
111
-
endpoint: endpoint,
112
-
credential: credential,
113
-
withOptions: options
114
-
)
108
+
let credential =
109
+
try CommunicationTokenCredential(
110
+
token: "<ACCESS_TOKEN>"
111
+
)
112
+
let options = AzureCommunicationChatClientOptions()
113
+
114
+
let chatClient = try ChatClient(
115
+
endpoint: endpoint,
116
+
credential: credential,
117
+
withOptions: options
118
+
)
115
119
```
116
120
117
121
Replace `<ACS_RESOURCE_ENDPOINT>` with the endpoint of your Azure Communication Services resource. Replace `<ACCESS_TOKEN>` with a valid Communication Services access token.
@@ -136,10 +140,10 @@ Now you use your `ChatClient` to create a new thread with an initial user.
136
140
Replace the comment `<CREATE A CHAT THREAD>` with the following code:
137
141
138
142
```
139
-
let request = CreateThreadRequest(
143
+
let request = CreateChatThreadRequest(
140
144
topic: "Quickstart",
141
145
participants: [
142
-
Participant(
146
+
ChatParticipant(
143
147
id: CommunicationUserIdentifier("<USER_ID>"),
144
148
displayName: "Jack"
145
149
)
@@ -150,7 +154,7 @@ var threadId: String?
150
154
chatClient.create(thread: request) { result, _ in
151
155
switch result {
152
156
case let .success(result):
153
-
threadId = result.thread?.id
157
+
threadId = result.chatThread?.id
154
158
155
159
case .failure:
156
160
fatalError("Failed to create thread.")
@@ -164,11 +168,31 @@ Replace `<USER_ID>` with a valid Communication Services user ID.
164
168
165
169
You're using a semaphore here to wait for the completion handler before continuing. In later steps, you'll use the `threadId` from the response returned to the completion handler.
166
170
171
+
## List all chat threads
172
+
173
+
After creating a chat thread we can list all chat threads by calling the `listChatThreads` method on `ChatClient`. Replace the comment `<LIST ALL CHAT THREADS>` with the following code:
174
+
175
+
```
176
+
chatClient.listThreads { result, _ in
177
+
switch result {
178
+
case let .success(chatThreadItems):
179
+
var iterator = chatThreadItems.syncIterator
180
+
while let chatThreadItem = iterator.next() {
181
+
print("Thread id: \(chatThreadItem.id)")
182
+
}
183
+
case .failure:
184
+
print("Failed to list threads")
185
+
}
186
+
semaphore.signal()
187
+
}
188
+
semaphore.wait()
189
+
```
190
+
167
191
## Get a chat thread client
168
192
169
193
Now that you've created a chat thread, you can obtain a `ChatThreadClient` to perform operations within the thread.
170
194
171
-
Replace the comment `<CREATE A CHAT THREAD CLIENT>` with the following code:
195
+
Replace the comment `<GET A CHAT THREAD CLIENT>` with the following code:
172
196
173
197
```
174
198
let chatThreadClient = try chatClient.createClient(forThread: threadId!)
@@ -184,10 +208,13 @@ let message = SendChatMessageRequest(
184
208
senderDisplayName: "Jack"
185
209
)
186
210
211
+
var messageId: String?
212
+
187
213
chatThreadClient.send(message: message) { result, _ in
188
214
switch result {
189
215
case let .success(result):
190
216
print("Message sent, message id: \(result.id)")
217
+
messageId = result.id
191
218
case .failure:
192
219
print("Failed to send message")
193
220
}
@@ -198,22 +225,67 @@ semaphore.wait()
198
225
199
226
First, you construct the `SendChatMessageRequest`, which contains the content and sender's display name. This request can also contain the share history time, if you want to include it. The response returned to the completion handler contains the ID of the message that was sent.
200
227
228
+
229
+
## Send a read receipt
230
+
231
+
You can send a read receipt for a particular message by calling `ChatThreadClients``sendReadReceipt` method. Replace the comment `<SEND A READ RECEIPT>` with the following code:
232
+
233
+
```
234
+
if let id = messageId {
235
+
chatThreadClient.sendReadReceipt(forMessage: id) { result, _ in
236
+
switch result {
237
+
case .success:
238
+
print("Read receipt sent")
239
+
case .failure:
240
+
print("Failed to send read receipt")
241
+
}
242
+
semaphore.signal()
243
+
}
244
+
semaphore.wait()
245
+
} else {
246
+
print("Cannot send read receipt without a message id")
247
+
}
248
+
```
249
+
250
+
## Receive chat messages from a chat thread
251
+
252
+
You can receive messages from a chat thread by calling the `listMessages()` method from `ChatThreadClient`. List messages includes system messages as well as user sent messages. For more information on the types of messages you can receive see [Message Types](https://docs.microsoft.com/azure/communication-services/concepts/chat/concepts#message-types)
253
+
254
+
Replace the comment `<RECEIVE MESSAGES>` with the following code:
255
+
256
+
```
257
+
chatThreadClient.listMessages { result, _ in
258
+
switch result {
259
+
case let .success(messages):
260
+
var iterator = messages.syncIterator
261
+
while let message = iterator.next() {
262
+
print("Received message of type \(message.type)")
263
+
}
264
+
265
+
case .failure:
266
+
print("Failed to receive messages")
267
+
}
268
+
semaphore.signal()
269
+
}
270
+
semaphore.wait()
271
+
```
272
+
201
273
## Add a user as a participant to the chat thread
202
274
203
275
Replace the comment `<ADD A USER>` with the following code:
204
276
205
277
```
206
-
let user = Participant(
278
+
let user = ChatParticipant(
207
279
id: CommunicationUserIdentifier("<USER_ID>"),
208
280
displayName: "Jane"
209
281
)
210
282
211
283
chatThreadClient.add(participants: [user]) { result, _ in
Replace `<USER ID>` with the Communication Services user ID of the participant being removed.
268
-
269
320
## Run the code
270
321
271
-
In Xcode, select **Run**to build and run the project. In the console, you can view the output from the code and the logger output from the chat client.
322
+
In Xcode hit the Run button to build and run the project. In the console you can view the output from the code and the logger output from the ChatClient.
0 commit comments