Skip to content

Update Send Message #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added
- Added exception details for telemetry for SendMessage
- Expose `OriginalMessageId` to `ChatSDK.onNewMessage()` to handle message ordering
- Update `ChatSDK.sendMessage()` to return `OmnichannelMessage`

### Changed
- Uptake [@microsoft/[email protected]](https://www.npmjs.com/package/@microsoft/ocsdk/v/0.5.13)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ import {DeliveryMode, MessageContentType, MessageType, PersonType} from '@micros

...

const displayName = "Contoso"
const message = "Sample message from customer";
const messageToSend = {
content: message
Expand Down
16 changes: 13 additions & 3 deletions __tests__/core/messaging/ACSClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ describe('ACSClient', () => {
await client.initialize(config);

const chatThreadClient: any = {};
const sendMessageResponse = {id: '0'};
chatThreadClient.listParticipants = jest.fn(() => ({
next: jest.fn(() => ({
value: 'value',
Expand All @@ -353,7 +354,7 @@ describe('ACSClient', () => {
done: jest.fn()
})),
}));
chatThreadClient.sendMessage = jest.fn();
chatThreadClient.sendMessage = jest.fn(() => sendMessageResponse);

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

await conversation.sendMessage({
content: 'message',
const content = 'message';
const chatMessage = await conversation.sendMessage({
content
});

expect(chatThreadClient.sendMessage).toHaveBeenCalledTimes(1);
expect(chatMessage).toBeDefined();
expect(chatMessage.id).toBe(sendMessageResponse.id);
expect(chatMessage.content).toBe(content);
expect(chatMessage.tags.includes('FromCustomer')).toBe(true);
expect(chatMessage.tags.includes('ChannelId-lcw')).toBe(true);
expect(chatMessage.properties.tags.includes('FromCustomer')).toBe(true);
expect(chatMessage.properties.tags.includes('ChannelId-lcw')).toBe(true);
expect(chatMessage.timestamp).toBeDefined();
});

it('ACSClient.sendMessage() failure should throw an error', async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/OmnichannelChatSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ class OmnichannelChatSDK {
return message;
}

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

try {
await (this.conversation as ACSConversation)?.sendMessage(sendMessageRequest);
const chatMessage = await (this.conversation as ACSConversation)?.sendMessage(sendMessageRequest);

this.scenarioMarker.completeScenario(TelemetryEvent.SendMessages, {
RequestId: this.requestId,
ChatId: this.chatToken.chatId as string
});

return chatMessage;
} catch (error) {
const exceptionDetails: ChatSDKExceptionDetails = {
response: ChatSDKErrorName.ChatSDKSendMessageFailed,
Expand Down
24 changes: 22 additions & 2 deletions src/core/messaging/ACSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export class ACSConversation {
}
}

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

if (!message.metadata) {
Expand All @@ -333,8 +333,26 @@ export class ACSConversation {
}

try {
await this.chatThreadClient?.sendMessage(sendMessageRequest, sendMessageOptions);
const response = await this.chatThreadClient?.sendMessage(sendMessageRequest, sendMessageOptions);
this.logger?.completeScenario(ACSClientEvent.SendMessage);

if (response?.id) {
console.log(response?.id);
const chatMessage = {
id: response?.id,
content: message.content,
sender: { communicationUserId: this.sessionInfo?.id as string },
displayName: sendMessageOptions.senderDisplayName,
metadata: {
tags: defaultMessageTags.join(',')
},
createdOn: new Date(parseInt(response?.id)) || response?.id
};

return createOmnichannelMessage(chatMessage as any, { // eslint-disable-line @typescript-eslint/no-explicit-any
liveChatVersion: LiveChatVersion.V2
});
}
} catch (error) {
const exceptionDetails = {
response: 'SendMessageFailed',
Expand All @@ -347,6 +365,8 @@ export class ACSConversation {

throw error;
}

return {} as OmnichannelMessage;
}

public async sendTyping(): Promise<void> {
Expand Down