Skip to content

Commit

Permalink
Adding support for location whatsapp messages
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhnewatiya-plivo committed May 12, 2024
1 parent 99464aa commit d80b2e9
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

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

## [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
Expand Down
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,69 @@ console.log('Phlo run result', result);
});
```


## WhatsApp Messaging
Plivo's WhatsApp API allows you to send different types of messages over WhatsApp, including templated messages, free form messages and interactive messages. Below are some examples on how to use the Plivo Go SDK to send these types of messages.

### Templated Messages
Templated messages are a crucial to your WhatsApp messaging experience, as businesses can only initiate WhatsApp conversation with their customers using templated messages.

WhatsApp templates support 4 components: `header` , `body`, `footer` and `button`. At the point of sending messages, the template object you see in the code acts as a way to pass the dynamic values within these components. `header` can accomodate `text` or `media` (images, video, documents) content. `body` can accomodate text content. `button` can support dynamic values in a `url` button or to specify a developer-defined payload which will be returned when the WhatsApp user clicks on the `quick_reply` button. `footer` cannot have any dynamic variables.

Example:
```javascript
```

### Free Form Messages
Non-templated or Free Form WhatsApp messages can be sent as a reply to a user-initiated conversation (Service conversation) or if there is an existing ongoing conversation created previously by sending a templated WhatsApp message.

#### Free Form Text Message
Example:
```javascript
```

#### Free Form Media Message
Example:
```javascript
```

### Interactive Messages
This guide shows how to send non-templated interactive messages to recipients using Plivo’s APIs.

#### Quick Reply Buttons
Quick reply buttons allow customers to quickly respond to your message with predefined options.

Example:
```javascript
```

#### Interactive Lists
Interactive lists allow you to present customers with a list of options.

Example:
```javascript
```

#### Interactive CTA URLs
CTA URL messages allow you to send links and call-to-action buttons.

Example:
```javascript
```

### Location Messages
This guide shows how to send templated and non-templated location messages to recipients using Plivo’s APIs.

#### Templated Location Messages
Example:
```javascript
```

#### Non-Templated Location Messages
Example:
```javascript
```

### More examples
More examples are available [here](https://github.com/plivo/plivo-examples-node). Also refer to the [guides for configuring the Express server to run various scenarios](https://www.plivo.com/docs/sms/quickstart/node-expressjs/) & use it to test out your integration in under 5 minutes.

Expand Down
29 changes: 27 additions & 2 deletions lib/resources/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @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.
* @param {Location} [optionalParams.location] For sending location whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -221,6 +222,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 {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @param {Location} [optionalParams.location] For sending location whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -240,6 +242,7 @@ export class MessageInterface extends PlivoResourceInterface {
var messageExpiry = src.messageExpiry;
var template = src.template;
var interactive = src.interactive;
var location = src.location;
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 @@ -303,6 +306,9 @@ export class MessageInterface extends PlivoResourceInterface {
if(interactive) {
params.interactive = interactive;
}
if(location) {
params.location = location;
}
if (dlt_entity_id) {
params.dlt_entity_id = dlt_entity_id
}
Expand All @@ -322,18 +328,24 @@ export class MessageInterface extends PlivoResourceInterface {
});
}

if ((params.type !== 'whatsapp') && params.template){
if (params.type && (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.type !== 'whatsapp') && params.interactive) {
if (params.type && (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.type && (params.type !== 'whatsapp') && params.location) {
let errorText = 'Location 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 Down Expand Up @@ -361,6 +373,19 @@ export class MessageInterface extends PlivoResourceInterface {
}
}

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

if (errors) {
return errors;
}
}

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

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

Validators.isLocation = field => {
const { error, value } = validateLocation(field);
return {error, value}
};


return (data = []) => {
Expand Down
20 changes: 20 additions & 0 deletions lib/utils/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Joi = require('joi');

// Schema for Location
const locationSchema = Joi.object({
latitude: Joi.string().required(),
longitude: Joi.string().required(),
name: Joi.string().required(),
address: Joi.string().required()
});

// Function to validate location data
function validateLocation(data) {
const { error, value } = locationSchema.validate(data, { allowUnknown: true });
return { error, value };
}

module.exports = {
locationSchema,
validateLocation
};
2 changes: 2 additions & 0 deletions lib/utils/template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('joi');
const { locationSchema } = require('./location');

const currencySchema = Joi.object({
fallback_value: Joi.string().required(),
Expand All @@ -17,6 +18,7 @@ const parameterSchema = Joi.object({
payload: Joi.string().optional(),
currency: currencySchema.optional(),
date_time: dateTimeSchema.optional(),
location: locationSchema.optional(),
});

const componentSchema = Joi.object({
Expand Down
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.65.0",
"version": "4.66.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 @@ -92,6 +92,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 {Template} [optionalParams.template] For sending templated whatsapp messages.
* @param {Interactive} [optionalParams.interactive] For sending interactive whatsapp messages.
* @param {Location} [optionalParams.location] For sending location whatsapp messages.
* @promise {object} return {@link PlivoGenericMessage} object if success
* @fail {Error} return Error
*/
Expand All @@ -116,6 +117,7 @@ export class MessageInterface extends PlivoResourceInterface {
* @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.
* @param {Location} [optionalParams.location] For sending location whatsapp messages.
* @promise {object} return {@link MessageResponse} object if success
* @fail {Error} return Error
*/
Expand Down

0 comments on commit d80b2e9

Please sign in to comment.