Skip to content

Commit 202396f

Browse files
authored
Update Send Message (#437)
* Update send message from acs client to return message data * Update test * Update send message interface to return chat message * Update CHANGELOG.md * Update README.md
1 parent e08a8cd commit 202396f

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Added
88
- Added exception details for telemetry for SendMessage
99
- Expose `OriginalMessageId` to `ChatSDK.onNewMessage()` to handle message ordering
10+
- Update `ChatSDK.sendMessage()` to return `OmnichannelMessage`
1011

1112
### Changed
1213
- Uptake [@microsoft/ocsdk@0.5.13](https://www.npmjs.com/package/@microsoft/ocsdk/v/0.5.13)

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ import {DeliveryMode, MessageContentType, MessageType, PersonType} from '@micros
372372

373373
...
374374

375-
const displayName = "Contoso"
376375
const message = "Sample message from customer";
377376
const messageToSend = {
378377
content: message

__tests__/core/messaging/ACSClient.spec.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ describe('ACSClient', () => {
341341
await client.initialize(config);
342342

343343
const chatThreadClient: any = {};
344+
const sendMessageResponse = {id: '0'};
344345
chatThreadClient.listParticipants = jest.fn(() => ({
345346
next: jest.fn(() => ({
346347
value: 'value',
@@ -353,7 +354,7 @@ describe('ACSClient', () => {
353354
done: jest.fn()
354355
})),
355356
}));
356-
chatThreadClient.sendMessage = jest.fn();
357+
chatThreadClient.sendMessage = jest.fn(() => sendMessageResponse);
357358

358359
client.chatClient = {};
359360
client.chatClient.getChatThreadClient = jest.fn(() => chatThreadClient);
@@ -365,11 +366,20 @@ describe('ACSClient', () => {
365366
pollingInterval: 1000,
366367
});
367368

368-
await conversation.sendMessage({
369-
content: 'message',
369+
const content = 'message';
370+
const chatMessage = await conversation.sendMessage({
371+
content
370372
});
371373

372374
expect(chatThreadClient.sendMessage).toHaveBeenCalledTimes(1);
375+
expect(chatMessage).toBeDefined();
376+
expect(chatMessage.id).toBe(sendMessageResponse.id);
377+
expect(chatMessage.content).toBe(content);
378+
expect(chatMessage.tags.includes('FromCustomer')).toBe(true);
379+
expect(chatMessage.tags.includes('ChannelId-lcw')).toBe(true);
380+
expect(chatMessage.properties.tags.includes('FromCustomer')).toBe(true);
381+
expect(chatMessage.properties.tags.includes('ChannelId-lcw')).toBe(true);
382+
expect(chatMessage.timestamp).toBeDefined();
373383
});
374384

375385
it('ACSClient.sendMessage() failure should throw an error', async () => {

src/OmnichannelChatSDK.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ class OmnichannelChatSDK {
12601260
return message;
12611261
}
12621262

1263-
public async sendMessage(message: ChatSDKMessage): Promise<void> {
1263+
public async sendMessage(message: ChatSDKMessage): Promise<OmnichannelMessage | void> {
12641264
this.scenarioMarker.startScenario(TelemetryEvent.SendMessages, {
12651265
RequestId: this.requestId,
12661266
ChatId: this.chatToken.chatId as string
@@ -1288,12 +1288,14 @@ class OmnichannelChatSDK {
12881288
}
12891289

12901290
try {
1291-
await (this.conversation as ACSConversation)?.sendMessage(sendMessageRequest);
1291+
const chatMessage = await (this.conversation as ACSConversation)?.sendMessage(sendMessageRequest);
12921292

12931293
this.scenarioMarker.completeScenario(TelemetryEvent.SendMessages, {
12941294
RequestId: this.requestId,
12951295
ChatId: this.chatToken.chatId as string
12961296
});
1297+
1298+
return chatMessage;
12971299
} catch (error) {
12981300
const exceptionDetails: ChatSDKExceptionDetails = {
12991301
response: ChatSDKErrorName.ChatSDKSendMessageFailed,

src/core/messaging/ACSClient.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ export class ACSConversation {
312312
}
313313
}
314314

315-
public async sendMessage(message: ChatSDKMessage): Promise<void> {
315+
public async sendMessage(message: ChatSDKMessage): Promise<OmnichannelMessage> {
316316
this.logger?.startScenario(ACSClientEvent.SendMessage);
317317

318318
if (!message.metadata) {
@@ -333,8 +333,26 @@ export class ACSConversation {
333333
}
334334

335335
try {
336-
await this.chatThreadClient?.sendMessage(sendMessageRequest, sendMessageOptions);
336+
const response = await this.chatThreadClient?.sendMessage(sendMessageRequest, sendMessageOptions);
337337
this.logger?.completeScenario(ACSClientEvent.SendMessage);
338+
339+
if (response?.id) {
340+
console.log(response?.id);
341+
const chatMessage = {
342+
id: response?.id,
343+
content: message.content,
344+
sender: { communicationUserId: this.sessionInfo?.id as string },
345+
displayName: sendMessageOptions.senderDisplayName,
346+
metadata: {
347+
tags: defaultMessageTags.join(',')
348+
},
349+
createdOn: new Date(parseInt(response?.id)) || response?.id
350+
};
351+
352+
return createOmnichannelMessage(chatMessage as any, { // eslint-disable-line @typescript-eslint/no-explicit-any
353+
liveChatVersion: LiveChatVersion.V2
354+
});
355+
}
338356
} catch (error) {
339357
const exceptionDetails = {
340358
response: 'SendMessageFailed',
@@ -347,6 +365,8 @@ export class ACSConversation {
347365

348366
throw error;
349367
}
368+
369+
return {} as OmnichannelMessage;
350370
}
351371

352372
public async sendTyping(): Promise<void> {

0 commit comments

Comments
 (0)