Skip to content

Commit

Permalink
feat: added whats app reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Oct 8, 2024
1 parent 9666516 commit 4b96cb9
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
101 changes: 101 additions & 0 deletions packages/messages/__tests__/__dataSets__/whatsApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import {
WhatsAppVideoRequest,
UpdateMessageStatus,
UpdateMessageRequest,
WhatsAppReactionRequest,
WhatsAppReactionParams,
WhatsAppReaction,
} from '../../lib';

import { Audio } from '../../lib/classes/WhatsApp/Audio';
Expand Down Expand Up @@ -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,
}
];
56 changes: 56 additions & 0 deletions packages/messages/lib/classes/WhatsApp/WhatsAppReaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { AbstractMessage } from '../AbstractMessage';
import {
WhatsAppReactionParams,
WhatsAppReactionType,
WhatsAppContext,
} from '../../types';

/**
* Represents a custom 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 custom message to a WhatsApp user.
*
* @param {WhatsAppCustomParams} params - The parameters for creating a WhatsApp custom message.
* @example
* ```ts
* import { WhatsAppCustom } from '@vonage/messages';
*
* const { messageUUID } = await messagesClient.send(new WhatsAppCustom({
* to: TO_NUMBER,
* from: FROM_NUMBER,
* custom: {
* type: 'template',
* template: {
* namespace: 'your-namespace',
* name: 'your-template-name',
* },
* }
* 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;
}
}
}
1 change: 1 addition & 0 deletions packages/messages/lib/classes/WhatsApp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './WhatsAppSticker';
export * from './WhatsAppTemplate';
export * from './WhatsAppText';
export * from './WhatsAppVideo';
export * from './WhatsAppReaction';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type WhatsAppReactionType = {
/**
* The action taken.
*/
action: 'react' | 'unreact';

/**
* The emoji used as a reaction.
*/
emoji?: string;
}
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions packages/messages/lib/types/Channels/WhatsApp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand All @@ -40,6 +43,7 @@ export type AnyWhatsAppParams =
| WhatsAppStickerParams
| WhatsAppTemplateParams
| WhatsAppTextParams
| WhatsAppReactionParams
| WhatsAppVideoParams;

/**
Expand Down
69 changes: 69 additions & 0 deletions packages/messages/lib/types/Requests/WhatsAppReactionRequest.ts
Original file line number Diff line number Diff line change
@@ -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
}

}
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 @@ -22,3 +22,4 @@ export * from './WhatsAppStickerIdRequest';
export * from './WhatsAppTemplateRequest';
export * from './WhatsAppTextRequest';
export * from './UpdateMessageRequest';
export * from './WhatsAppReactionRequest';

0 comments on commit 4b96cb9

Please sign in to comment.