diff --git a/packages/messages/README.md b/packages/messages/README.md index a2fec552..f5be4ddb 100644 --- a/packages/messages/README.md +++ b/packages/messages/README.md @@ -120,14 +120,15 @@ for general use will be listed as having 'General Availability'. Channels which are currently part of a Beta program will be listed as 'Beta'. This table details the current release status of each channel implemented in this SDK: -| Channel | API Release Status | -|--------------------|:--------------------:| -| SMS | General Availability | -| MMS | General Availability | -| RCS | Beta | -| Facebook Messenger | General Availability | -| WhatsApp | General Availability | -| Viber | General Availability | +| Channel | API Release Status | +|---------------------|:--------------------:| +| SMS | General Availability | +| MMS | General Availability | +| RCS | Beta | +| Facebook Messenger | General Availability | +| WhatsApp | General Availability | +| WhatsApp (reaction) | Beta | +| Viber | General Availability | [signup]: https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=node-server-sdk diff --git a/packages/messages/__tests__/__dataSets__/whatsApp.ts b/packages/messages/__tests__/__dataSets__/whatsApp.ts index f65bc928..a8a2eb50 100644 --- a/packages/messages/__tests__/__dataSets__/whatsApp.ts +++ b/packages/messages/__tests__/__dataSets__/whatsApp.ts @@ -29,6 +29,9 @@ import { WhatsAppVideoRequest, UpdateMessageStatus, UpdateMessageRequest, + WhatsAppReactionRequest, + WhatsAppReactionParams, + WhatsAppReaction, } from '../../lib'; import { Audio } from '../../lib/classes/WhatsApp/Audio'; @@ -1035,4 +1038,102 @@ export default [ ], expected: true }, + { + label: 'send reaction', + request: [ + '/v1/messages', + 'POST', + { + from: '12126875309', + to: '14152739164', + channel: 'whatsapp', + message_type: 'reaction', + client_ref: 'my-ref', + context: { + message_uuid: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + webhook_version: 'v1', + webhook_url: 'https://example.com', + reaction: { + action: 'react', + emoji: '😄', + } + } as WhatsAppReactionRequest, + ], + response: [ + 200, + { + message_uuid: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + ], + method: 'POST', + clientMethod: 'send', + parameters: [ + new WhatsAppReaction({ + from: '12126875309', + to: '14152739164', + clientRef: 'my-ref', + context: { + messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + webhookVersion: 'v1', + webhookUrl: 'https://example.com', + reaction: { + action: 'react', + emoji: '😄', + } + } as WhatsAppReactionParams), + ], + expected: { + messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + } as MessageSuccess, + }, + { + label: 'remove reaction', + request: [ + '/v1/messages', + 'POST', + { + from: '12126875309', + to: '14152739164', + channel: 'whatsapp', + message_type: 'reaction', + client_ref: 'my-ref', + context: { + message_uuid: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + webhook_version: 'v1', + webhook_url: 'https://example.com', + reaction: { + action: 'unreact', + } + } as WhatsAppReactionRequest, + ], + response: [ + 200, + { + message_uuid: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + ], + method: 'POST', + clientMethod: 'send', + parameters: [ + new WhatsAppReaction({ + from: '12126875309', + to: '14152739164', + clientRef: 'my-ref', + context: { + messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + }, + webhookVersion: 'v1', + webhookUrl: 'https://example.com', + reaction: { + action: 'unreact', + } + } as WhatsAppReactionParams), + ], + expected: { + messageUUID: '1d4723b0-9134-4440-8cf0-e9f39ccb1c6a', + } as MessageSuccess, + } ]; diff --git a/packages/messages/lib/classes/WhatsApp/WhatsAppReaction.ts b/packages/messages/lib/classes/WhatsApp/WhatsAppReaction.ts new file mode 100644 index 00000000..11c30f91 --- /dev/null +++ b/packages/messages/lib/classes/WhatsApp/WhatsAppReaction.ts @@ -0,0 +1,71 @@ +import { AbstractMessage } from '../AbstractMessage'; +import { + WhatsAppReactionParams, + WhatsAppReactionType, + WhatsAppContext, +} from '../../types'; + +/** + * Represents a reaction message for WhatsApp. + * + * @group WhatsApp + */ +export class WhatsAppReaction + extends AbstractMessage + implements WhatsAppReactionParams +{ + public channel: 'whatsapp'; + public messageType: 'reaction'; + public reaction: WhatsAppReactionType; + + public context?: WhatsAppContext; + + /** + * Sends a reaction message to a WhatsApp user. + * + * @param {WhatsAppReactionParams} params - The parameters for creating a WhatsApp reaction message. + * @example + * Send a reaction + * ```ts + * import { WhatsAppReaction } from '@vonage/messages'; + * + * const { messageUUID } = await messagesClient.send(new WhatsAppReaction({ + * to: TO_NUMBER, + * from: FROM_NUMBER, + * reaction: { + * action: 'react', + * emoji: '😍', + * } + * clientRef: 'my-personal-reference', + * })); + * + * console.log(`Message sent successfully with UUID ${messageUUID}`); + * ``` + * + * @example + * Remove reaction + * ```ts + * import { WhatsAppReaction } from '@vonage/messages'; + * + * const { messageUUID } = await messagesClient.send(new WhatsAppReaction({ + * to: TO_NUMBER, + * from: FROM_NUMBER, + * reaction: { + * action: 'unreact', + * } + * clientRef: 'my-personal-reference', + * })); + * + * console.log(`Message sent successfully with UUID ${messageUUID}`); + * ``` + */ + public constructor(params: WhatsAppReactionParams) { + super(params); + this.channel = 'whatsapp'; + this.messageType = 'reaction'; + this.reaction = params.reaction; + if (params.context) { + this.context = params.context; + } + } +} diff --git a/packages/messages/lib/classes/WhatsApp/index.ts b/packages/messages/lib/classes/WhatsApp/index.ts index b733f48c..8c3b89ab 100644 --- a/packages/messages/lib/classes/WhatsApp/index.ts +++ b/packages/messages/lib/classes/WhatsApp/index.ts @@ -6,3 +6,4 @@ export * from './WhatsAppSticker'; export * from './WhatsAppTemplate'; export * from './WhatsAppText'; export * from './WhatsAppVideo'; +export * from './WhatsAppReaction'; diff --git a/packages/messages/lib/types/Channels/WhatsApp/WhatAppReactionType.ts b/packages/messages/lib/types/Channels/WhatsApp/WhatAppReactionType.ts new file mode 100644 index 00000000..16b1afe8 --- /dev/null +++ b/packages/messages/lib/types/Channels/WhatsApp/WhatAppReactionType.ts @@ -0,0 +1,11 @@ +export type WhatsAppReactionType = { + /** + * The action taken. + */ + action: 'react' | 'unreact'; + + /** + * The emoji used as a reaction. + */ + emoji?: string; +} diff --git a/packages/messages/lib/types/Channels/WhatsApp/WhatsAppReactionParams.ts b/packages/messages/lib/types/Channels/WhatsApp/WhatsAppReactionParams.ts new file mode 100644 index 00000000..2a115503 --- /dev/null +++ b/packages/messages/lib/types/Channels/WhatsApp/WhatsAppReactionParams.ts @@ -0,0 +1,11 @@ +import { WhatsAppParams } from './WhatsAppParams'; +import { WhatsAppReactionType } from './WhatAppReactionType'; +/** + * Represents WhatsApp reaction message parameters. + * + * @group WhatsApp + * @category Parameters + */ +export type WhatsAppReactionParams = { + reaction: WhatsAppReactionType; +} & WhatsAppParams; diff --git a/packages/messages/lib/types/Channels/WhatsApp/index.ts b/packages/messages/lib/types/Channels/WhatsApp/index.ts index 4988be90..d81d25a2 100644 --- a/packages/messages/lib/types/Channels/WhatsApp/index.ts +++ b/packages/messages/lib/types/Channels/WhatsApp/index.ts @@ -7,6 +7,7 @@ import { WhatsAppStickerParams } from './WhatsAppStickerParams'; import { WhatsAppTemplateParams } from './WhatsAppTemplateParams'; import { WhatsAppTextParams } from './WhatsAppTextParams'; import { WhatsAppVideoParams } from './WhatsAppVideoParams'; +import { WhatsAppReactionParams } from './WhatsAppReactionParams'; import { Channels } from '../../../enums'; export * from './WhatsAppAudioParams'; @@ -23,6 +24,8 @@ export * from './WhatsAppTemplateType'; export * from './WhatsAppTextParams'; export * from './WhatsAppVideoParams'; export * from './WhatsAppParams'; +export * from './WhatsAppReactionParams'; +export * from './WhatAppReactionType'; /** * Represents a union type that can be any of the WhatsApp-specific message @@ -40,6 +43,7 @@ export type AnyWhatsAppParams = | WhatsAppStickerParams | WhatsAppTemplateParams | WhatsAppTextParams + | WhatsAppReactionParams | WhatsAppVideoParams; /** diff --git a/packages/messages/lib/types/Requests/WhatsAppReactionRequest.ts b/packages/messages/lib/types/Requests/WhatsAppReactionRequest.ts new file mode 100644 index 00000000..260c3ba5 --- /dev/null +++ b/packages/messages/lib/types/Requests/WhatsAppReactionRequest.ts @@ -0,0 +1,69 @@ +export type WhatsAppReactionRequest = { + /** + * A client-defined reference string for the message. + */ + client_ref: string; + + /** + * The type of the message, which is 'reaction' for a text message. + */ + message_type: 'reaction'; + + /** + * The recipient's identifier. + */ + to: string; + + /** + * The sender's identifier. + */ + from: string; + + /** + * The channel through which the message will be sent, which is 'whatsapp' for WhatsApp. + */ + channel: 'whatsapp'; + + /** + * Specifies which version of the Messages API will be used to send Status + * Webhook messages for this particular message. For example, if v0.1 is + * set, then the JSON body of Status Webhook messages for this message will + * be sent in Messages v0.1 format. Over-rides account-level and + * application-level API version settings on a per-message basis. + */ + webhook_version: 'v0.1' | 'v1'; + + /** + * Specifies the URL to which Status Webhook messages will be sent for this + * particular message. Over-rides account-level and application-level Status + * Webhook url settings on a per-message basis. + */ + webhook_url: string; + + /** + * A context used for quoting/replying/reacting to a specific message in a + * conversation. When used for quoting or replying, the WhatsApp UI will + * display the new message along with a contextual bubble that displays the + * quoted/replied to message's content. When used for reacting the WhatsApp + * UI will display the reaction emoji below the reacted to message. + */ + context: { + /** + * The UUID of the message being quoted/replied/reacted to. + */ + message_uuid: string; + }; + + reaction: { + /** + * The action taken. + */ + action: 'react' | 'unreact'; + + /** + * The emoji used as a reaction. + */ + emoji?: string + } + +} diff --git a/packages/messages/lib/types/Requests/index.ts b/packages/messages/lib/types/Requests/index.ts index ccb5705a..36a8a970 100644 --- a/packages/messages/lib/types/Requests/index.ts +++ b/packages/messages/lib/types/Requests/index.ts @@ -22,3 +22,4 @@ export * from './WhatsAppStickerIdRequest'; export * from './WhatsAppTemplateRequest'; export * from './WhatsAppTextRequest'; export * from './UpdateMessageRequest'; +export * from './WhatsAppReactionRequest';