Skip to content
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

feat: added update to messages #960

Merged
merged 1 commit into from
Sep 25, 2024
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
22 changes: 22 additions & 0 deletions packages/messages/__tests__/__dataSets__/rcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
RCSVideo,
RCSFile,
RCSCustom,
UpdateMessageRequest,
UpdateMessageStatus
} from '../../lib';

export default [
Expand Down Expand Up @@ -434,4 +436,24 @@ export default [
messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
} as MessageSuccess,
},
{
baseUrl: 'https://api.vonage.com',
label: 'update a message as read',
request: [
'/v1/messages/1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
'PATCH',
{
status: 'revoked',
} as UpdateMessageRequest,
],
response: [
202,
],
clientMethod: 'updateMessage',
parameters: [
'1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
UpdateMessageStatus.REVOKED
],
expected: true
}
];
22 changes: 22 additions & 0 deletions packages/messages/__tests__/__dataSets__/whatsApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
WhatsAppImageRequest,
WhatsAppVideoParams,
WhatsAppVideoRequest,
UpdateMessageStatus,
UpdateMessageRequest,
} from '../../lib';

import { Audio } from '../../lib/classes/WhatsApp/Audio';
Expand Down Expand Up @@ -1013,4 +1015,24 @@ export default [
messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
} as MessageSuccess,
},
{
baseUrl: 'https://api.vonage.com',
label: 'update a message as read',
request: [
'/v1/messages/1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
'PATCH',
{
status: 'read',
} as UpdateMessageRequest,
],
response: [
202,
],
clientMethod: 'updateMessage',
parameters: [
'1d4723b0-9134-4440-8cf0-e9f39ccb1c6a',
UpdateMessageStatus.READ
],
expected: true
},
];
4 changes: 2 additions & 2 deletions packages/messages/__tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const messageTests = testDataSets.map((dataSet): TestTuple<Messages> => {
applicationId: 'abcd-1234',
privateKey: testPrivateKey,
}),
clientMethod: 'send',
clientMethod: test.clientMethod as keyof Messages,
parameters: test.parameters,
generator: false,
error: 'error' in test ? String(test.error) : false,
Expand All @@ -53,7 +53,7 @@ const messageTests = testDataSets.map((dataSet): TestTuple<Messages> => {
apiKey: '12345',
apiSecret: 'ABCDE',
}),
clientMethod: 'send',
clientMethod: test.clientMethod as keyof Messages,
parameters: test.parameters,
generator: false,
error: 'error' in test ? String(test.error) : false,
Expand Down
20 changes: 20 additions & 0 deletions packages/messages/lib/enums/UpdateMessageStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

export enum UpdateMessageStatus {
/**
* The status to set for the message. Setting the status of an inbound WhatsApp
* message to read indicates to the sender of the message that the
* message has been read (blue ticks are shown on that message in the WhatsApp
* UI). The status of an outbound WhatsApp message cannot be updated via
* this endpoint.
*/
READ ='read',


/**
* The status to set for the message. Setting the status of an outbound
* RCS message to revoked revokes that message if possible.
*/
REVOKED ='revoked',
}


3 changes: 2 additions & 1 deletion packages/messages/lib/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Channels';
export * from './Messenger';
export * from './UpdateMessageStatus';
export * from './Viber';
export * from './WhatsApp';
export * from './Channels';
43 changes: 43 additions & 0 deletions packages/messages/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AnyChannel,
} from './types';
import debug from 'debug';
import { UpdateMessageStatus } from './enums/UpdateMessageStatus';

const log = debug('vonage:messages');

Expand Down Expand Up @@ -96,4 +97,46 @@ export class Messages extends Client {
messageUUID: resp.data.message_uuid,
};
}

/**
* Update the status of outbound and/or inbound messages for certain
* channels. For example, you can revoke outbound messages or mark inbound
* messages as read.
*
* Please not that this endpoint is region specifc. You will need to set the
* region when you create the client.
*
* @example
* Update the status of a WhatsApp message to "read"
* ```ts
* const vonage = new Vonage(
* {
* applicationId: myAppId,
* privateKey: myPrivateKey
* },
* {
* apiHost: 'https://api-eu.vonage.com'
* }
* )
*
* await vonage.messages.updateMessage(messageId, UpdateMessageStatus.READ);
* ```
*
* @param {string} messageId - The ID of the message to update.
* @param {UpdateMessageStatus | string} status - The status to update the message to.
*
* @return {Promise<true>} A promise that resolves to true if the message was
* updated successfully.
*/
public async updateMessage(
messageId: string,
status: UpdateMessageStatus | string,
): Promise<true> {
// This endpoint returns a 202 so we don't need to parse the response
await this.sendPatchRequest<MessageSuccessResponse>(
`${this.config.apiHost}/v1/messages/${messageId}`,
{status: status},
);
return true;
}
}
16 changes: 16 additions & 0 deletions packages/messages/lib/types/Requests/UpdateMessageRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { UpdateMessageStatus } from '../../enums';

/**
* The request object for the {@link Messages.update} method.
*/
export type UpdateMessageRequest = {
/**
* The status to set for the message.
*
* This value depends on the type of message that was sent. The SDK has no way
* to know which value will be correct for the message. Confirm with the API
* specification or the API documentation which value is correct for the
* message {@link https://developer.vonage.com/en/api/messages#UpdateMessage}.
*/
status: UpdateMessageStatus | string;
}
1 change: 1 addition & 0 deletions packages/messages/lib/types/Requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './WhatsAppStickerUrlRequest';
export * from './WhatsAppStickerIdRequest';
export * from './WhatsAppTemplateRequest';
export * from './WhatsAppTextRequest';
export * from './UpdateMessageRequest';
Loading