whatsapp-docs-mcp 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +54 -2
- package/dist/index.js.map +1 -1
- package/dist/tools.d.ts +10 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +59 -0
- package/dist/tools.js.map +1 -1
- package/dist/utils/search.d.ts +18 -2
- package/dist/utils/search.d.ts.map +1 -1
- package/dist/utils/search.js +328 -93
- package/dist/utils/search.js.map +1 -1
- package/dist/utils/search.test.js +128 -0
- package/dist/utils/search.test.js.map +1 -1
- package/docs/catalogos/sell_products_and_services.md +1 -1
- package/docs/flows/changelog.md +613 -0
- package/docs/flows/gettingstarted/health_insurance.md +537 -0
- package/docs/flows/gettingstarted/personalised_offer.md +495 -0
- package/docs/flows/gettingstarted/pre_approved_loan.md +506 -0
- package/docs/flows/gettingstarted/purchase_intent.md +246 -0
- package/docs/flows/gettingstarted.md +52 -0
- package/docs/flows/guias.md +60 -0
- package/docs/flows/playground.md +135 -0
- package/docs/flows/referencia.md +102 -0
- package/docs/flows/support.md +32 -0
- package/docs/flows.md +98 -0
- package/docs/{mensagens/limites_de_mensagens → messaging_limits}/upcoming_changes.md +1 -1
- package/docs/suporte.md +408 -0
- package/package.json +9 -2
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
<!-- Source: https://developers.facebook.com/docs/whatsapp/flows/gettingstarted/personalised-offer -->
|
|
2
|
+
<!-- Scraped: 2025-12-21T15:24:23.708Z -->
|
|
3
|
+
|
|
4
|
+
[Plataforma do WhatsApp Business](https://developers.facebook.com/docs/whatsapp)
|
|
5
|
+
|
|
6
|
+
[](#)
|
|
7
|
+
|
|
8
|
+
# Use Case Guide: Capture interest for a personalised Offer
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
## Intro and Overview
|
|
13
|
+
|
|
14
|
+
WhatsApp is improving the way that you communicate with your customers to tailor products and services - enabling customers to select, customise, and signal an intent to purchase- all without leaving WhatsApp or speaking to an agent.
|
|
15
|
+
|
|
16
|
+
In this guide, we will walk through the entire process to build a Flow for a ‘Personalised Offer’ use case. The templates here can be adapted to suit your use case.
|
|
17
|
+
|
|
18
|
+
Flows we will build will demonstrate how you can:
|
|
19
|
+
|
|
20
|
+
- Present groups of products for your user to select
|
|
21
|
+
- Offer customisation options (budget, size, purpose etc.), to narrow down to a specific product
|
|
22
|
+
- Based on their inputs, make recommendations of a list of products, including detailed overview of each product
|
|
23
|
+
- Confirm product selection and allow users to proceed with a purchase
|
|
24
|
+
|
|
25
|
+
This template can be adapted to any product or service that you want to offer to customers. Customers can share their needs and preferences, allowing you to offer the ideal solution. This use case can accelerate marketing campaigns performance during large sales events, allowing you to provide a targeted offer for specific products.
|
|
26
|
+
|
|
27
|
+
## Getting Started
|
|
28
|
+
|
|
29
|
+
To follow this guide, ensure you have:
|
|
30
|
+
|
|
31
|
+
- Completed [prerequisites](/docs/whatsapp/flows/gettingstarted#prerequisites) for building Flows.
|
|
32
|
+
- A [Glitch](https://l.facebook.com/l.php?u=https%3A%2F%2Fglitch.com%2Fsignup&h=AT3ZG-13kF1PaCWyIiBinuIrpwdyWJRlSIint1tlOlZjX5XZSlb3hQc12WaJc__4CgQBoGUgpdow4_snoVoeFk4d1aJ_nxyASd4fMrb3-v4To801ZM0lHl4zK0r3VbdF_Jrhi_B-L-v01IZNhsdOK1pi964) account
|
|
33
|
+
|
|
34
|
+
[](#)
|
|
35
|
+
|
|
36
|
+
## Flows JSON Template
|
|
37
|
+
|
|
38
|
+
Flow JSON
|
|
39
|
+
|
|
40
|
+
{
|
|
41
|
+
|
|
42
|
+
"version": "7.3",
|
|
43
|
+
|
|
44
|
+
"data\_api\_version": "3.0",
|
|
45
|
+
|
|
46
|
+
"routing\_model": {
|
|
47
|
+
|
|
48
|
+
"PRODUCT\_SELECTOR": \[
|
|
49
|
+
|
|
50
|
+
"OPTIONS"
|
|
51
|
+
|
|
52
|
+
\],
|
|
53
|
+
|
|
54
|
+
"OPTIONS": \[
|
|
55
|
+
|
|
56
|
+
"OFFER"
|
|
57
|
+
|
|
58
|
+
\],
|
|
59
|
+
|
|
60
|
+
"OFFER": \[
|
|
61
|
+
|
|
62
|
+
"PRODUCT\_DETAIL"
|
|
63
|
+
|
|
64
|
+
\]
|
|
65
|
+
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
"screens": \[
|
|
69
|
+
|
|
70
|
+
{
|
|
71
|
+
|
|
72
|
+
"id": "PRODUCT\_SELECTOR",
|
|
73
|
+
|
|
74
|
+
"title": "Black Friday Deals",
|
|
75
|
+
|
|
76
|
+
"data": {
|
|
77
|
+
|
|
78
|
+
"products": {
|
|
79
|
+
|
|
80
|
+
"type": "array",
|
|
81
|
+
|
|
82
|
+
"items": {
|
|
83
|
+
|
|
84
|
+
"type": "object",
|
|
85
|
+
|
|
86
|
+
"properties": {
|
|
87
|
+
|
|
88
|
+
"id": {
|
|
89
|
+
|
|
90
|
+
"type": "string"
|
|
91
|
+
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
"title": {
|
|
95
|
+
|
|
96
|
+
"type": "string"
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
"\_\_example\_\_": \[
|
|
105
|
+
|
|
106
|
+
{
|
|
107
|
+
|
|
108
|
+
"id": "0\_mobile\_phones",
|
|
109
|
+
|
|
110
|
+
"title": "Mobile phones"
|
|
111
|
+
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
{
|
|
115
|
+
|
|
116
|
+
"id": "1\_eBook\_readers",
|
|
117
|
+
|
|
118
|
+
Enter to Rename, ⇧Enter to Preview
|
|
119
|
+
|
|
120
|
+
Preview
|
|
121
|
+
|
|
122
|
+
Run
|
|
123
|
+
|
|
124
|
+
Configurações
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
Select screen
|
|
129
|
+
|
|
130
|
+
PRODUCT\_SELECTOR
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+

|
|
135
|
+
|
|
136
|
+
* * *
|
|
137
|
+
|
|
138
|
+
Preview Flow
|
|
139
|
+
|
|
140
|
+
What would you like to buy now
|
|
141
|
+
|
|
142
|
+
- Mobile phones
|
|
143
|
+
|
|
144
|
+
- eBook readers
|
|
145
|
+
|
|
146
|
+
- Cameras
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
Continue
|
|
150
|
+
|
|
151
|
+
Gerenciada pela empresa. Saiba mais Saiba mais
|
|
152
|
+
|
|
153
|
+
### Create new flow from a template
|
|
154
|
+
|
|
155
|
+
1. In the [Flows section of WhatsApp Manager](https://business.facebook.com/wa/manage/flows/) click on the **Create Flow** button in the top right corner.
|
|
156
|
+
2. In the Create page, fill in the details for the pre-approved loan Flow:
|
|
157
|
+
- **Name** - Type _Personalised offer_, or choose another name you like.
|
|
158
|
+
- **Categories** - Select _Lead generation_.
|
|
159
|
+
- **Template** - Choose _Capture interest for a personalised offer_. You can further customize the template to suit your use case.
|
|
160
|
+
3. Click **Create** to create Flow.
|
|
161
|
+
|
|
162
|
+
You can preview the Flow on the right of the Builder UI.
|
|
163
|
+
|
|
164
|
+
The Flow remains in the draft state as you edit it. You can share it with your team for testing purposes only. To share it with a large audience, you’ll need to publish it. However, you can’t edit the Flow once you [publish](#publishing). Since you will still need to add the endpoint URL for this Flow, leave it as a draft for now and proceed to the next step, where you’ll configure the demo backend endpoint.
|
|
165
|
+
|
|
166
|
+
**See also**
|
|
167
|
+
|
|
168
|
+
- [Flow JSON Overview](/docs/whatsapp/flows/reference/flowjson)
|
|
169
|
+
|
|
170
|
+
[](#)
|
|
171
|
+
|
|
172
|
+
## Configure Demo Backend on Glitch
|
|
173
|
+
|
|
174
|
+
WhatsApp Flows lets you connect to an external endpoint. This endpoint can provide dynamic data for your Flow and control routing. It also receives user-submitted responses from the Flow.
|
|
175
|
+
|
|
176
|
+
For testing purposes, this template uses Glitch to host the endpoint. Using Glitch is entirely optional, and not required to use Flows. You can [clone the endpoint code from GitHub](https://l.facebook.com/l.php?u=https%3A%2F%2Fgithub.com%2FWhatsApp%2FWhatsApp-Flows-Tools%2Ftree%2Fmain%2Fexamples%2Fendpoint%2Fnodejs%2Fpersonalised-offer&h=AT3_iuCx4y6CJOsONJe84kq0TiojnfgQX9CYg54_ogcOs3jKyxiFDHNp_LF4fI6QlxKzisIAMRXBTmCt0hp568UhMIwaDDln5Y1YxGkKEufNQTB3vzWw2V7rktJHQ9tCKM43vhtXT7pZNfTh1LJf2hCP33c) and run it in any environment you prefer.
|
|
177
|
+
|
|
178
|
+
### 1\. Remix (fork) Glitch endpoint
|
|
179
|
+
|
|
180
|
+
Access the [endpoint code in Glitch](https://l.facebook.com/l.php?u=https%3A%2F%2Fglitch.com%2Fedit%2F%23%21%2Fwhatsapp-flows-personalised-offer&h=AT0X9UCNHIm1SPOYd3vEEx4C8JxycCA2EdmuFNAe83Ms4fg2VaNA3ZBol7EBUYlTL1YWuWmqKnXx8XCgDtBQjrWtG-xg_00o1a7cwCmj7yFJCaaeIMWO_82g_shAxe_xcBC7fy73d6fKYGu0pbOhol-sVcU) and remix it to get your unique domain. To remix it, click **Remix** at the top of the page. A unique domain will appear as a placeholder in the input element on the right side of the Glitch page.
|
|
181
|
+
|
|
182
|
+
### 2\. Setup encryption key
|
|
183
|
+
|
|
184
|
+
Private key helps decrypt the messages received. The passphrase will be used to verify the private key. Along with the private key, you also need its corresponding public key, which you’ll upload later. Never use the private keys for your production accounts here. Create a temporary private key for testing on Glitch, and then replace it with your production key in your own infrastructure.
|
|
185
|
+
|
|
186
|
+
1. Generate the public-private key pair by running the command below in the Glitch terminal. Replace `YOUR_PASSPHRASE` with your designated passphrase. Access the Glitch terminal by clicking the **TERMINAL** tab at the bottom of the page a run following command: `node src/keyGenerator.js YOUR_PASSPHRASE`
|
|
187
|
+
|
|
188
|
+
2. Copy the passphrase and private key and paste them to the .env file. Click on the file labeled **.env** on the left sidebar, then click on **✏️ Plain text** on top. _Do not edit it directly from the UI, as it will break your key formatting._
|
|
189
|
+
|
|
190
|
+
3. After you set the environment variables, copy the public key that you generated and [upload the public key](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/whatsapp-business-encryption#set-business-public-key) via the Graph API.
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
### 3\. Set endpoint URI
|
|
194
|
+
|
|
195
|
+
Once you set up encryption keys, you can proceed with setting Endpoint URI for your flow.
|
|
196
|
+
|
|
197
|
+
1. At the top right of the Glitch page, click on **Share** and copy the **Live Site** URL from the displayed modal.
|
|
198
|
+
|
|
199
|
+
2. Head to the [Flow Builder](https://business.facebook.com/wa/manage/flows/) and in the Flow Builder click on the **three dot** menu in top right corner of the screen. Select **Setup** under the **Endpoint** section.
|
|
200
|
+
|
|
201
|
+
3. A popup will appear, allowing you to configure the endpoint URI, business phone number, and app on Meta for Developers. Save the Live Site URL copied from the Glitch into the first step of modal.
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
After making the necessary configurations, perform a health check from the last step of the modal. You should be able to get a successful response (if you get an error please check the details and the provided resolution advice).
|
|
205
|
+
|
|
206
|
+
### 4\. Set App Secret (optional)
|
|
207
|
+
|
|
208
|
+
App secret is used in signature verification. It helps you check whether a request is coming via WhatsApp and, therefore, is safe to process. You’ll add it to the **.env** file.
|
|
209
|
+
|
|
210
|
+
To access your app secret, select your App from the [dashboard in the Meta for Developers](https://developers.facebook.com/apps/). In the left navigation pane under **App settings**, choose **Basic**. Click **Show** under **App secret** and copy the secret. Then, return to Glitch, open the .env file, and create a variable named APP\_SECRET with the value of the secret you copied.
|
|
211
|
+
|
|
212
|
+
Now you have completed all the required steps to be able to test flow with the provided endpoint.
|
|
213
|
+
|
|
214
|
+
**See also**
|
|
215
|
+
|
|
216
|
+
- [Detailed code walk through](#overview-of-demo-backend) for demo backend
|
|
217
|
+
- [Implementing endpoint for Flows](https://developers.facebook.com/docs/whatsapp/flows/guides/implementingyourflowendpoint) for full details on how to build production endpoint
|
|
218
|
+
|
|
219
|
+
[](#)
|
|
220
|
+
|
|
221
|
+
## Testing and Debugging
|
|
222
|
+
|
|
223
|
+
### Debug flow using the interactive preview
|
|
224
|
+
|
|
225
|
+
After you complete the configurations, toggle the interactive preview in the WhatsApp Builder UI to test the Flow.
|
|
226
|
+
|
|
227
|
+
1. Trigger the interactive preview by clicking on settings menu in the **Preview** section of the Flow Builder and enabling **Interactive mode** toggle.
|
|
228
|
+
|
|
229
|
+
2. In the modal that appears, select the phone number, enter any string as **Flow token** and choose the **Request data** option under **Request data on the first screen**. This sends a request to the endpoint to retrieve data for the first screen.
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
Now, click on **Actions** tab at the bottom of the code editor in Builder. You’ll see an `init` action in the list. Click on it to see the details and you will see the decrypted request sent to the endpoint. There will also be decrypted response received from endpoint with the initial data payload.
|
|
233
|
+
|
|
234
|
+
Return back to **Preview** and proceed to change tenure by selecting an option from the Tenure dropdown. Back in **Actions** tab notice how the tenure is set to selected new tenure and the action has changed to `data_exchange`.
|
|
235
|
+
|
|
236
|
+
Keep testing out the Flow and observe the data changes in the **Actions** tab. Similar logs will be generated when users interact with the Flow from their mobile devices.
|
|
237
|
+
|
|
238
|
+
You can also see decrypted request and responses logged in the Glitch LOGS tab at the bottom of the Glitch screen.
|
|
239
|
+
|
|
240
|
+
### Send draft flow to your device
|
|
241
|
+
|
|
242
|
+
Before you publish your flow you can also send it and test it on an actual device. To send draft flow to your device, follow [instructions here](/docs/whatsapp/flows/guides/testingdebugging#send-draft-flow-to-your-device).
|
|
243
|
+
|
|
244
|
+
**See also**
|
|
245
|
+
|
|
246
|
+
- [Flow Testing and debugging guide](/docs/whatsapp/flows/guides/testingdebugging)
|
|
247
|
+
|
|
248
|
+
[](#)
|
|
249
|
+
|
|
250
|
+
## Publishing
|
|
251
|
+
|
|
252
|
+
When you first created your Flow, it entered the Draft state. And as you edited and saved the modified Flow JSON content, it remained in the Draft state. You are able to send the Flow while it's in the Draft state, but only for testing purposes. If you want to send the Flow to a larger audience, you'll need to Publish the Flow.
|
|
253
|
+
|
|
254
|
+
You can publish your Flow once you have ensured that:
|
|
255
|
+
|
|
256
|
+
- All validation errors and [publishing checks](/docs/whatsapp/flows/guides/healthmonitoring#publishing-checks) have been resolved.
|
|
257
|
+
- The Flow meets the [design principles](https://developers.facebook.com/docs/whatsapp/flows/guides/bestpractices) of WhatsApp Flows
|
|
258
|
+
- The Flow complies with [WhatsApp Terms of Service](https://l.facebook.com/l.php?u=https%3A%2F%2Fwww.whatsapp.com%2Flegal%2Fterms-of-service%2F%3Flang%3Den&h=AT3qlOnUDAf8ywI0uoUYqO1yK-O6Zyl-ZEFNkQIlDiU3tTv0UGhiiD4Hudr6G_Xi4b8Ka2p-ePnIR8363FfmvnPtC4zxve-Y1U44N20jkW5_Aikcji1PaERmnPOJXCe6ShbhudT1gsnOFpB4Dq4P4qs4NAQ), the [WhatsApp Business Messaging Policy](https://l.facebook.com/l.php?u=https%3A%2F%2Ffaq.whatsapp.com%2F933578044281252&h=AT0o5u-A3llsjsPJcMGrZ6Enn_xN5-yrr4TYINJKeMFor_-sTChZHVG7kXBVmus9V5fcKvo83Fxj5sGG-vQA1HwjTTBeuE28Pee8NRQ7wdEhsSRVRWIjhr6T0Iviorgh881T86WETA-g1FS_c7Y7sNBDKQs) and, if applicable, the [WhatsApp Commerce Policy](https://l.facebook.com/l.php?u=https%3A%2F%2Fwww.whatsapp.com%2Flegal%2Fcommerce-policy%2F%3Flang%3Den&h=AT2yBEOzSWj5CVD862hi350QSODFZtvYrCnPOMhxhLrRc8m9q70rJ36OAmxfVj31YcsXomoSMHHgqRdwLTbbQfBCRTIczT_bNXdEk7pF_5JX8JH5F3Z9SGZzkC_8GgVBpt9oI9Te5DOlyhK8_xGwcnBAaIQ)
|
|
259
|
+
|
|
260
|
+
Remember, once a Flow has been published it can no longer be modified. See [Flow Status Lifecycle](/docs/whatsapp/flows/gettingstarted/flows-lifecycle) for more information on the different Flow states.
|
|
261
|
+
|
|
262
|
+
To publish your Flow, open the **three dot** menu to the right of the **Save** button and click **Publish**. Once published, the Flow can be sent to anyone!
|
|
263
|
+
|
|
264
|
+
[](#)
|
|
265
|
+
|
|
266
|
+
## Sending
|
|
267
|
+
|
|
268
|
+
You can send your WhatsApp Flow as:
|
|
269
|
+
|
|
270
|
+
- **[Template messages](https://developers.facebook.com/docs/whatsapp/flows/gettingstarted/sendingaflow#templatemessages)** - these do not require a 24-hour customer service window to be open between you and the message recipient before the message can be sent.
|
|
271
|
+
- **[Interactive Flow messages](https://developers.facebook.com/docs/whatsapp/flows/gettingstarted/sendingaflow#userinitiated)** - these can only be sent to a user when a customer service window is open between you and the user.
|
|
272
|
+
|
|
273
|
+
[Learn more about sending your Flow](https://developers.facebook.com/docs/whatsapp/flows/gettingstarted/sendingaflow)
|
|
274
|
+
|
|
275
|
+
[](#)
|
|
276
|
+
|
|
277
|
+
## Receiving flow response
|
|
278
|
+
|
|
279
|
+
Upon flow completion a response message will be sent to the WhatsApp chat. You will receive it in the same way as you receive all other messages from the user - via message webhook.
|
|
280
|
+
|
|
281
|
+
[Learn more about how to setup messaging webhook](/docs/whatsapp/flows/gettingstarted/receiveflowresponse)
|
|
282
|
+
|
|
283
|
+
[](#)
|
|
284
|
+
|
|
285
|
+
## Monitoring
|
|
286
|
+
|
|
287
|
+
Flow monitoring is only applicable to Flows with endpoint.
|
|
288
|
+
|
|
289
|
+
After your Flow is published and being sent to the customers, it is important to monitor your Flow's health and address any problems as they are discovered by WhatsApp.
|
|
290
|
+
|
|
291
|
+
There are multiple ways how you can monitor your flows:
|
|
292
|
+
|
|
293
|
+
- Metrics Dashboard in WhatsApp Account Manager
|
|
294
|
+
- Navigate to [Flow section of Whatsapp Account Manager](https://business.facebook.com/wa/manage/flows/) and click on any published flow with the endpoint. You will be presented with a Details page with a Performance Metrics Dashboard.
|
|
295
|
+
- Metrics API
|
|
296
|
+
- All the data presented in the Details page is also available to be queried through [Flows Metrics API](https://developers.facebook.com/docs/whatsapp/flows/reference/metrics_api).
|
|
297
|
+
- Quality Webhooks
|
|
298
|
+
- You should also [subscribe to Flows Quality Webhooks](https://developers.facebook.com/docs/whatsapp/flows/reference/flowswebhooks#subscribe-to-webhooks) to be updated in real time about the statuses and performance of your business' Flows.
|
|
299
|
+
|
|
300
|
+
See [Flow Health and Monitoring](https://developers.facebook.com/docs/whatsapp/flows/guides/healthmonitoring) overview for more information.
|
|
301
|
+
|
|
302
|
+
[](#)
|
|
303
|
+
|
|
304
|
+
## Next Steps
|
|
305
|
+
|
|
306
|
+
Now that you have successfully completed this guide, learn more about what you can do with this Flows in our [Guides](/docs/whatsapp/flows/guides) and [Reference](https://developers.facebook.com/docs/whatsapp/flows/reference) sections.
|
|
307
|
+
|
|
308
|
+
[](#)
|
|
309
|
+
|
|
310
|
+
## Overview of demo backend
|
|
311
|
+
|
|
312
|
+
There are four JavaScript files in the [Glitch example src directory](https://l.facebook.com/l.php?u=https%3A%2F%2Fglitch.com%2Fedit%2F%23%21%2Fwhatsapp-flows-personalised-offer&h=AT2JNHg9CyeA5gA9S8bC8rXvAy7XLleIb4m5SplK4wYwMBrDirAvIci0lWHTHmDDYU9vFEjpTI05LGtyVYff8KvpatcIf4brfztG3EsFH_SjQpm1fliOyQGbvPRZiSXD6dnK--9dljrYkUh1YlgZBeslbFw): `encryption.js`, `flow.js`, `keyGenerator.js`, and `server.js`. The entry file is `server.js`, so let’s look at it first.
|
|
313
|
+
|
|
314
|
+
### server.js
|
|
315
|
+
|
|
316
|
+
The `server.js` file starts by configuring the Express application to use the express.json middleware to parse incoming JSON requests. Then, it loads the environment variables needed for the endpoint.
|
|
317
|
+
|
|
318
|
+
```
|
|
319
|
+
const { APP_SECRET, PRIVATE_KEY, PASSPHRASE, PORT = "3000" } = process.env;
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
The `server.js` file also contains a POST endpoint that performs different steps:
|
|
323
|
+
|
|
324
|
+
Checks that the private key is present:
|
|
325
|
+
|
|
326
|
+
```
|
|
327
|
+
if (!PRIVATE_KEY) {
|
|
328
|
+
throw new Error('Private key is empty. Please check your env variable "PRIVATE_KEY".');
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Validates the request signature using the isRequestSignatureValid function found at the bottom of the file:
|
|
333
|
+
|
|
334
|
+
```
|
|
335
|
+
if(!isRequestSignatureValid(req)) {
|
|
336
|
+
// Return status code 432 if request signature does not match.
|
|
337
|
+
// To learn more about return error codes visit: https://developers.facebook.com/docs/whatsapp/flows/reference/error-codes#endpoint_error_codes
|
|
338
|
+
return res.status(432).send();
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
Decrypts incoming messages using the decryptRequest function found in the encryption.js file:
|
|
343
|
+
|
|
344
|
+
```
|
|
345
|
+
let decryptedRequest = null;
|
|
346
|
+
try {
|
|
347
|
+
decryptedRequest = decryptRequest(req.body, PRIVATE_KEY, PASSPHRASE);
|
|
348
|
+
} catch (err) {
|
|
349
|
+
console.error(err);
|
|
350
|
+
if (err instanceof FlowEndpointException) {
|
|
351
|
+
return res.status(err.statusCode).send();
|
|
352
|
+
}
|
|
353
|
+
return res.status(500).send();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const { aesKeyBuffer, initialVectorBuffer, decryptedBody } = decryptedRequest;
|
|
357
|
+
console.log("💬 Decrypted Request:", decryptedBody);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Decides what Flow screen to display to the user. You’ll look at the getNextScreen function in detail later.
|
|
361
|
+
|
|
362
|
+
```
|
|
363
|
+
const screenResponse = await getNextScreen(decryptedBody);
|
|
364
|
+
console.log("👉 Response to Encrypt:", screenResponse);
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Encrypts the response to be sent to the user:
|
|
368
|
+
|
|
369
|
+
```
|
|
370
|
+
res.send(encryptResponse(screenResponse, aesKeyBuffer, initialVectorBuffer));
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### encryption.js
|
|
374
|
+
|
|
375
|
+
This file contains the logic for encrypting and decrypting messages exchanged for security purposes. See [Code examples](https://developers.facebook.com/docs/whatsapp/flows/guides/implementingyourflowendpoint#code-examples) section of Endpoint implementation guide for encryption examples in other languages.
|
|
376
|
+
|
|
377
|
+
### keyGenerator.js
|
|
378
|
+
|
|
379
|
+
This file helps generate the private and public keys, as you saw earlier.
|
|
380
|
+
|
|
381
|
+
### flow.js
|
|
382
|
+
|
|
383
|
+
The logic for handling the Flow is housed in this file. It starts with an object assigned the name `SCREEN_RESPONSES`. The object contains screen IDs with their corresponding details, such as the preset data used in the data exchanges. This object is generated from Flow Builder under **"..." > Endpoint > Snippets > Responses**. In the same object, you also have another ID, `SUCCESS`, that is sent back to the client device when the Flow is successfully completed. This closes the Flow.
|
|
384
|
+
|
|
385
|
+
The `getNextScreen` function contains the logic that guides the endpoint on what Flow data to display to the user. It starts by extracting the necessary data from the decrypted message.
|
|
386
|
+
|
|
387
|
+
```
|
|
388
|
+
const { screen, data, version, action, flow_token } = decryptedBody;
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
WhatsApp Flows endpoints usually receive three types of requests:
|
|
392
|
+
|
|
393
|
+
- A [data\_exchange](https://developers.facebook.com/docs/whatsapp/flows/guides/implementingyourflowendpoint#data_exchange_request) request signified by a `data_exchange` action
|
|
394
|
+
- An [error notification](https://developers.facebook.com/docs/whatsapp/flows/guides/implementingyourflowendpoint/#error_notification_request) request signified by a `data.error` element
|
|
395
|
+
- A [health check](https://developers.facebook.com/docs/whatsapp/flows/guides/implementingyourflowendpoint/#health_check_request) request signified by a `ping` action
|
|
396
|
+
|
|
397
|
+
#### Health check and Error handler
|
|
398
|
+
|
|
399
|
+
The function handles the health check and error notifications using if statements and responds accordingly, as shown in the snippet below:
|
|
400
|
+
|
|
401
|
+
```
|
|
402
|
+
// handle health check request
|
|
403
|
+
if (action === "ping") {
|
|
404
|
+
return {
|
|
405
|
+
version,
|
|
406
|
+
data: {
|
|
407
|
+
status: "active",
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// handle error notification
|
|
413
|
+
if (data?.error) {
|
|
414
|
+
console.warn("Received client error:", data);
|
|
415
|
+
return {
|
|
416
|
+
version,
|
|
417
|
+
data: {
|
|
418
|
+
acknowledged: true,
|
|
419
|
+
},
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
#### INIT handler
|
|
425
|
+
|
|
426
|
+
When a user clicks the Flow’s call to action (CTA) button, a request with `INIT` action is sent to the endpoint. This action returns the initial offer data and pre-sets the radio buttons on the PRODUCT\_SELECTOR screen.
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
// handle initial request when opening the flow and display PRODUCT_SELECTOR screen
|
|
430
|
+
if (action === "INIT") {
|
|
431
|
+
return {
|
|
432
|
+
...SCREEN_RESPONSES.PRODUCT_SELECTOR
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
#### data-exchange handlers
|
|
438
|
+
|
|
439
|
+
For `data_exchange` actions, a switch case structure is used to determine what data to send back based on the screen ID and other request data.
|
|
440
|
+
|
|
441
|
+
For the first screen with ID `PRODUCT_SELECTOR` we parse selected `product_type` and based on that set variables for `OPTIONS` screen. The `phone_use_case` boolean controls whether the phone related questions are shown on the the `OPTIONS` screen.
|
|
442
|
+
|
|
443
|
+
```
|
|
444
|
+
case "PRODUCT_SELECTOR":
|
|
445
|
+
const product_type = data.product_selection.split('_').pop().slice(0, -1);
|
|
446
|
+
return {
|
|
447
|
+
...SCREEN_RESPONSES.OPTIONS,
|
|
448
|
+
data: {
|
|
449
|
+
// copy initial screen data then override specific fields
|
|
450
|
+
...SCREEN_RESPONSES.OPTIONS.data,
|
|
451
|
+
phone_use_case: data.product_selection === SCREEN_RESPONSES.PRODUCT_SELECTOR.data.products[0].id,
|
|
452
|
+
cta_label: "View " + product_type + "s",
|
|
453
|
+
screen_heading: "Let's find the perfect " + product_type + " offer for you",
|
|
454
|
+
selected_product: product_type,
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
When processing `OPTIONS` screen response, you should return personalised offer based on the users inputs.
|
|
460
|
+
|
|
461
|
+
```
|
|
462
|
+
case "OPTIONS":
|
|
463
|
+
// TODO here process user selected preferences and return customised offer
|
|
464
|
+
return {
|
|
465
|
+
...SCREEN_RESPONSES.OFFER,
|
|
466
|
+
data: {
|
|
467
|
+
// copy initial screen data then override specific fields
|
|
468
|
+
...SCREEN_RESPONSES.OFFER.data,
|
|
469
|
+
offer_label: "Here are 4 shortlisted " + data.selected_product + "s",
|
|
470
|
+
selected_product: data.selected_product,
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
In the response for `OFFER` screen, we receive user’s selected device and should return appropriate device details in response.
|
|
476
|
+
|
|
477
|
+
```
|
|
478
|
+
case "OFFER":
|
|
479
|
+
// TODO return details of selected device
|
|
480
|
+
return {
|
|
481
|
+
...SCREEN_RESPONSES.PRODUCT_DETAIL,
|
|
482
|
+
data: {
|
|
483
|
+
// copy initial screen data then override specific fields
|
|
484
|
+
...SCREEN_RESPONSES.PRODUCT_DETAIL.data,
|
|
485
|
+
product_name: SCREEN_RESPONSES.OFFER.data.shortlisted_devices
|
|
486
|
+
.filter((a) => a.id === data.device)
|
|
487
|
+
.map((a) => a.title)[0],
|
|
488
|
+
selected_device: data.device,
|
|
489
|
+
},
|
|
490
|
+
};
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
After the `SUMMARY` screen is submitted from the client, a success response is sent to the client device to mark the Flow as complete.
|
|
494
|
+
|
|
495
|
+
[](#)
|