Skip to content

Commit

Permalink
Merge pull request #311 from plivo/SMS-6061
Browse files Browse the repository at this point in the history
SMS-6061 adding whatsapp support
  • Loading branch information
narayana-plivo authored Aug 8, 2023
2 parents 03771d2 + 9360694 commit d6d9ff3
Show file tree
Hide file tree
Showing 9 changed files with 1,711 additions and 1,009 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Change Log
## [v4.54.0](https://github.com/plivo/plivo-node/tree/v4.54.0) (2023-08-07)
**Feature - WhatsApp message support**
- Added new param `template` and new message_type `whatsapp` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message)
- Added new `message_states` (`read`) `message_type`(`whatsapp`),`conversation_id`, `conversation_origin`, `conversation_expiry_timestamp` in [list all messages API](https://www.plivo.com/docs/sms/api/message#list-all-messages) and [get message details API](https://www.plivo.com/docs/sms/api/message#retrieve-a-message) response

## [4.53.0](https://github.com/plivo/plivo-node/tree/v4.53.0) (2023-08-03)
**Feature - DLT parameters**
Expand All @@ -9,6 +13,7 @@
**Feature - Number Masking**
- Added Create, Delete, Update, Get and List Masking Session API


## [v4.51.0](https://github.com/plivo/plivo-node/tree/v4.51.0) (2023-07-07)
**Fix Intermediate GET request failure**
- GET API request body removed
Expand Down
50 changes: 47 additions & 3 deletions lib/resources/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
validate
} from '../utils/common.js';


const action = 'Message/';
const idField = 'messageUuid';
let actionKey = Symbol('api action');
Expand Down Expand Up @@ -51,9 +52,14 @@ export class MessageGetResponse {
this.requesterIP = params.requesterIp;
this.isDomestic = params.isDomestic;
this.replacedSender = params.replacedSender;

this.conversationId = params.conversationId;
this.conversationOrigin = params.conversationOrigin;
this.conversationExpirationTimestamp = params.conversationExpirationTimestamp;
this.dltEntityID = params.dltEntityId;
this.dltTemplateID = params.dltTemplateId;
this.dltTemplateCategory = params.dltTemplateCategory;

}
}

Expand All @@ -79,6 +85,9 @@ export class MessageListResponse {
this.requesterIP = params.requesterIp;
this.isDomestic = params.isDomestic;
this.replacedSender = params.replacedSender;
this.conversationId = params.conversationId;
this.conversationOrigin = params.conversationOrigin;
this.conversationExpirationTimestamp = params.conversationExpirationTimestamp;
this.dltEntityID = params.dltEntityId;
this.dltTemplateID = params.dltTemplateId;
this.dltTemplateCategory = params.dltTemplateCategory;
Expand Down Expand Up @@ -169,7 +178,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.type] - The type of message. Should be `sms` `whatsapp` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @param {list} [optionalParams.media_urls] For sending mms, specify the media urls in list of string
Expand All @@ -178,6 +187,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.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -193,14 +203,16 @@ export class MessageInterface extends PlivoResourceInterface {
* @param {string} dst - destination number
* @param {string} text - text to send
* @param {object} optionalParams - Optional Params to send message
* @param {string} [optionalParams.type] - The type of message. Should be `sms` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.type] - The type of message. Should be `sms` `whatsapp` or `mms`. Defaults to `sms`.
* @param {string} [optionalParams.url] The URL to which with the status of the message is sent.
* @param {string} [optionalParams.method] The method used to call the url. Defaults to POST.
* @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 {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.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -218,6 +230,7 @@ export class MessageInterface extends PlivoResourceInterface {
var log = src.log;
var trackable = src.trackable;
var messageExpiry = src.messageExpiry;
var template = src.template;
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 All @@ -234,7 +247,6 @@ export class MessageInterface extends PlivoResourceInterface {
if (errors) {
return errors;
}

if (!src && !powerpackUUID) {
let errorText = 'Neither of src or powerpack uuid present, either one is required'
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -276,6 +288,10 @@ export class MessageInterface extends PlivoResourceInterface {
if (messageExpiry){
params.message_expiry = messageExpiry;
}
if(template)
{
params.template = template;
}
if (dlt_entity_id) {
params.dlt_entity_id = dlt_entity_id
}
Expand All @@ -285,6 +301,34 @@ export class MessageInterface extends PlivoResourceInterface {
if (dlt_template_category) {
params.dlt_template_category = dlt_template_category
}

}

if ((params.type === 'whatsapp') && !src){
let errorText = 'src parameter not present'
return new Promise(function(resolve, reject) {
reject(new Error(errorText));
});
}

if ((params.type !== 'whatsapp') && params.template){
let errorText = 'Template 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([{
field: 'template',
value: params.template,
validators: ['isTemplate']
},
]);

if (errors) {
return errors;
}
}

if (src) {
Expand Down
16 changes: 13 additions & 3 deletions lib/rest/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,10 @@ export function Request(config) {
is_domestic: false,
dlt_entity_id: "9876",
dlt_template_id: "5432",
dlt_template_category: "transactional"
dlt_template_category: "transactional",
conversation_id: "1234",
conversation_origin: "service",
conversation_expiration_timestamp: "2023-08-03 23:02:00+05:30"
}
});
}
Expand Down Expand Up @@ -2200,7 +2203,11 @@ export function Request(config) {
is_domestic: false,
dlt_entity_id: "2233",
dlt_template_id: "4455",
dlt_template_category: "service_implicit"
dlt_template_category: "service_implicit",
conversation_id: "9876",
conversation_origin: "marketing",
conversation_expiration_timestamp: "2023-08-03 23:02:00+05:30"

},
{
error_code: '200',
Expand All @@ -2219,7 +2226,10 @@ export function Request(config) {
is_domestic: false,
dlt_entity_id: null,
dlt_template_id: null,
dlt_template_category: null
dlt_template_category: null,
conversation_id: null,
conversation_origin: null,
conversation_expiration_timestamp: null
}
]
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
validateTemplate
} from '../utils/template.js';

export let extend = (instance, data) => {
data = data || {};
for (let key in data) {
Expand Down Expand Up @@ -33,6 +37,12 @@ export let validate = (() => {
return !field;
};

Validators.isTemplate = field => {
const { error, value } = validateTemplate(field);
return {error, value}
};


return (data = []) => {
let errorText = [];

Expand All @@ -54,6 +64,14 @@ export let validate = (() => {
errorText.push(item.field + ' should be string.');
}
break;
case 'isTemplate':
const { error, value } = Validators.isTemplate(item.value);
if(error){
error.details.forEach((validationError, _) => {
errorText.push(`${validationError.message}`);
})
break;
}
default:
}
});
Expand Down
42 changes: 42 additions & 0 deletions lib/utils/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const Joi = require('joi');

const currencySchema = Joi.object({
fallback_value: Joi.string().required(),
currency_code: Joi.string().required(),
amount_1000: Joi.number().required(),
});

const dateTimeSchema = Joi.object({
fallback_value: Joi.string().required(),
});

const parameterSchema = Joi.object({
type: Joi.string().required(),
text: Joi.string().optional(),
media: Joi.string().optional(),
currency: currencySchema.optional(),
date_time: dateTimeSchema.optional(),
});

const componentSchema = Joi.object({
type: Joi.string().required(),
sub_type: Joi.string().optional(),
index: Joi.string().optional(),
parameters: Joi.array().items(parameterSchema).optional(),
});

const templateSchema = Joi.object({
name: Joi.string().required(),
language: Joi.string().required(),
components: Joi.array().items(componentSchema).optional(),
});

// Validate the data against the templateSchema
export function validateTemplate(data){
const { error, value } = templateSchema.validate(data);
return {error, value}
}




Loading

0 comments on commit d6d9ff3

Please sign in to comment.