Skip to content

Commit

Permalink
Adding support for interactive whatsapp messages (#332)
Browse files Browse the repository at this point in the history
* Adding support for interactive whatsapp messages
  • Loading branch information
saurabhnewatiya-plivo authored May 7, 2024
1 parent 6397de7 commit 99464aa
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Change Log

## [v4.65.0](https://github.com/plivo/plivo-node/tree/v4.65.0) (2024-05-07)
**Feature - Adding support for interactive whatsapp messages**
- Added new param `interactive` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message) to support interactive `whatsapp` messages

## [v4.64.0](https://github.com/plivo/plivo-node/tree/v4.64.0) (2024-04-04)
**Vulnerabilities Fix**
- Axios Cross-Site Request Forgery Vulnerability Fix.
Expand Down
29 changes: 26 additions & 3 deletions lib/resources/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} [optionalParams.dlt_template_id] This is the DLT template id passed in the message request.
* @param {string} [optionalParams.dlt_template_category] This is the DLT template category passed in the message request.
* @param {Template} [optionalParams.template] For sending templated whatsapp messages.
* @param {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -219,7 +220,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} [optionalParams.dlt_entity_id] This is the DLT entity id passed in the message request.
* @param {string} [optionalParams.dlt_template_id] This is the DLT template id passed in the message request.
* @param {string} [optionalParams.dlt_template_category] This is the DLT template category passed in the message request.
* @param {Template} [optionalParams.template] For sending templated whatsapp messages.
* @param {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -238,6 +239,7 @@ export class MessageInterface extends PlivoResourceInterface {
var trackable = src.trackable;
var messageExpiry = src.messageExpiry;
var template = src.template;
var interactive = src.interactive;
var dlt_entity_id = src.dlt_entity_id;
var dlt_template_id = src.dlt_template_id;
var dlt_template_category = src.dlt_template_category;
Expand Down Expand Up @@ -295,10 +297,12 @@ export class MessageInterface extends PlivoResourceInterface {
if (messageExpiry){
params.message_expiry = messageExpiry;
}
if(template)
{
if(template) {
params.template = template;
}
if(interactive) {
params.interactive = interactive;
}
if (dlt_entity_id) {
params.dlt_entity_id = dlt_entity_id
}
Expand All @@ -324,6 +328,12 @@ export class MessageInterface extends PlivoResourceInterface {
reject(new Error(errorText));
});
}
if ((params.type !== 'whatsapp') && params.interactive) {
let errorText = 'Interactive paramater is only applicable when message_type is whatsapp'
return new Promise(function(resolve, reject) {
reject(new Error(errorText));
});
}

if (params.template){
let errors = validate([{
Expand All @@ -338,6 +348,19 @@ export class MessageInterface extends PlivoResourceInterface {
}
}

if (params.interactive){
let errors = validate([{
field: 'interactive',
value: params.interactive,
validators: ['isInteractive']
},
]);

if (errors) {
return errors;
}
}

if (src) {
params.src = src;
}
Expand Down
18 changes: 15 additions & 3 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
validateTemplate
} from '../utils/template.js';
import { validateTemplate } from '../utils/template.js';
import { validateInteractive } from '../utils/interactive.js';

export let extend = (instance, data) => {
data = data || {};
Expand Down Expand Up @@ -41,6 +40,11 @@ export let validate = (() => {
const { error, value } = validateTemplate(field);
return {error, value}
};

Validators.isInteractive = field => {
const { error, value } = validateInteractive(field);
return {error, value}
};


return (data = []) => {
Expand Down Expand Up @@ -72,6 +76,14 @@ export let validate = (() => {
})
break;
}
case 'isInteractive':
const { err, val } = Validators.isInteractive(item.value);
if(err){
err.details.forEach((validationError, _) => {
errorText.push(`${validationError.message}`);
})
break;
}
default:
}
});
Expand Down
63 changes: 63 additions & 0 deletions lib/utils/interactive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const Joi = require('joi');

// Schema for Row
const rowSchema = Joi.object({
id: Joi.string().optional(),
title: Joi.string().optional(),
description: Joi.string().optional(),
});

// Schema for Section
const sectionSchema = Joi.object({
title: Joi.string().optional(),
rows: Joi.array().items(rowSchema).optional(),
});

// Schema for Buttons
const buttonsSchema = Joi.object({
id: Joi.string().optional(),
title: Joi.string().optional(),
cta_url: Joi.string().optional(),
});

// Schema for Action
const actionSchema = Joi.object({
buttons: Joi.array().items(buttonsSchema).optional(),
sections: Joi.array().items(sectionSchema).optional(),
});

// Schema for Header
const headerSchema = Joi.object({
type: Joi.string().optional(),
text: Joi.string().optional(),
media: Joi.string().optional(),
});

// Schema for Body
const bodySchema = Joi.object({
text: Joi.string().optional(),
});

// Schema for Footer
const footerSchema = Joi.object({
text: Joi.string().optional(),
});

// Schema for Interactive
const interactiveSchema = Joi.object({
type: Joi.string().optional(),
header: headerSchema.optional(),
body: bodySchema.optional(),
footer: footerSchema.optional(),
action: actionSchema.optional(),
});

// Function to validate the data against the interactiveSchema
function validateInteractive(data) {
const { error, value } = interactiveSchema.validate(data, { allowUnknown: true });
return { error, value };
}

module.exports = {
validateInteractive,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plivo",
"version": "4.64.0",
"version": "4.65.0",
"description": "A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML",
"homepage": "https://github.com/plivo/plivo-node",
"files": [
Expand Down
2 changes: 2 additions & 0 deletions types/resources/messages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @param {Template} [optionalParams.template] For sending templated whatsapp messages.
* @param {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -114,6 +115,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {boolean} [optionalParams.log] If set to false, the content of this message will not be logged on the Plivo infrastructure and the dst value will be masked (e.g., 141XXXXX528). Default is set to true.
* @param {Array} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
* @param {Template} [optionalParams.template] For sending templated whatsapp messages.
* @param {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @promise {object} return {@link MessageResponse} object if success
* @fail {Error} return Error
*/
Expand Down

0 comments on commit 99464aa

Please sign in to comment.