-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhatsapp-lead-follow-up.json
More file actions
378 lines (378 loc) · 11.2 KB
/
Copy pathwhatsapp-lead-follow-up.json
File metadata and controls
378 lines (378 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{
"name": "WhatsApp Lead Follow-Up — Wati reference workflow",
"nodes": [
{
"parameters": {
"content": "## WhatsApp Lead Follow-Up (free reference workflow)\n\nExample intake → Wati template request → Google Sheet → reply check before one follow-up after 24 hours.\n\nRequires approved templates, permission to contact the recipient, Wati API credentials and a configured Google Sheet. Other providers need their own request and response adapters. No response-time or delivery guarantee. Test before enabling.\n\nExpected payload: {\"name\":\"...\",\"phone\":\"+15551234567\",\"source\":\"website\",\"message\":\"...\"}\n\nTemplates: lead_instant_reply and lead_follow_up_24h, both with a name variable. Reply history must have messages.items with boolean owner and valid created/timestamp values. Empty, unknown or potentially incomplete history stops the workflow.\n\nhttps://trystackpick.com/templates/",
"height": 540,
"width": 420
},
"id": "note-setup",
"name": "Setup notes",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-720,
-160
]
},
{
"parameters": {
"httpMethod": "POST",
"path": "lead-intake",
"options": {}
},
"id": "webhook-lead",
"name": "New lead (webhook)",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [
-240,
80
],
"webhookId": "lead-intake"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "a1",
"name": "name",
"value": "={{ $json.body.name || 'there' }}",
"type": "string"
},
{
"id": "a2",
"name": "phone",
"value": "={{ ($json.body.phone || '').replace(/[^+\\d]/g, '') }}",
"type": "string"
},
{
"id": "a3",
"name": "source",
"value": "={{ $json.body.source || 'unknown' }}",
"type": "string"
},
{
"id": "a4",
"name": "message",
"value": "={{ $json.body.message || '' }}",
"type": "string"
},
{
"id": "a5",
"name": "received_at",
"value": "={{ $now.toISO() }}",
"type": "string"
}
]
},
"options": {}
},
"id": "set-normalize",
"name": "Normalize lead",
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-20,
80
]
},
{
"parameters": {
"method": "POST",
"url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/sendTemplateMessage?whatsappNumber={{ $json.phone }}",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Bearer YOUR_WATI_API_KEY"
},
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ template_name: 'lead_instant_reply', broadcast_name: 'lead_instant_reply', parameters: [{ name: 'name', value: $json.name }] }) }}",
"options": {}
},
"id": "http-instant-reply",
"name": "Send instant WhatsApp reply",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
200,
80
]
},
{
"parameters": {
"operation": "append",
"documentId": {
"__rl": true,
"mode": "list",
"value": ""
},
"sheetName": {
"__rl": true,
"mode": "list",
"value": ""
},
"columns": {
"mappingMode": "defineBelow",
"value": {
"received_at": "={{ $('Normalize lead').item.json.received_at }}",
"name": "={{ $('Normalize lead').item.json.name }}",
"phone": "={{ $('Normalize lead').item.json.phone }}",
"source": "={{ $('Normalize lead').item.json.source }}",
"message": "={{ $('Normalize lead').item.json.message }}",
"status": "instant_reply_sent"
}
},
"options": {}
},
"id": "sheets-log",
"name": "Log lead to sheet",
"type": "n8n-nodes-base.googleSheets",
"typeVersion": 4.5,
"position": [
420,
80
]
},
{
"parameters": {
"amount": 24,
"unit": "hours"
},
"id": "wait-24h",
"name": "Wait 24 hours",
"type": "n8n-nodes-base.wait",
"typeVersion": 1.1,
"position": [
640,
80
],
"webhookId": "wait-24h-lead"
},
{
"parameters": {
"method": "POST",
"url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/sendTemplateMessage?whatsappNumber={{ $('Normalize lead').item.json.phone }}",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Bearer YOUR_WATI_API_KEY"
},
{
"name": "Content-Type",
"value": "application/json"
}
]
},
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ template_name: 'lead_follow_up_24h', broadcast_name: 'lead_follow_up_24h', parameters: [{ name: 'name', value: $('Normalize lead').item.json.name }] }) }}",
"options": {}
},
"id": "http-follow-up",
"name": "Send 24h follow-up",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
860,
80
]
},
{
"parameters": {
"content": "The 24-hour follow-up now requires verified reply history. An inbound reply stops the cadence; unknown, empty or potentially incomplete history raises an error. Review Wati pagination/fields and use test numbers before enabling.",
"height": 180,
"width": 320
},
"id": "note-replied",
"name": "Reply-check tip",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
820,
-140
]
},
{
"parameters": {
"method": "GET",
"url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/getMessages/{{ $('Normalize lead').item.json.phone }}?pageSize=100&pageNumber=1",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "Bearer YOUR_WATI_API_KEY"
}
]
},
"options": {}
},
"id": "verify-replies-before-followup",
"name": "Check replies (24h)",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1480,
300
]
},
{
"parameters": {
"jsCode": "// Reference Wati adapter: stop instead of treating missing/unrecognized history as no reply.\nconst lead = $('Normalize lead').first().json;\nconst response = $input.first().json;\nconst msgs = response.messages && response.messages.items;\nconst since = Date.parse(lead.received_at);\nif (!Number.isFinite(since) || !Array.isArray(msgs) || response.result === false) {\n throw new Error('Reply history could not be verified. Check the Wati response adapter before sending.');\n}\nif (msgs.length === 0 || msgs.length >= 100 || response.messages.hasMore === true ||\n (Number.isFinite(response.messages.totalCount) && response.messages.totalCount > msgs.length)) {\n throw new Error('Reply history is empty or may be incomplete. Review/paginate it before sending.');\n}\nfunction messageTime(m) {\n const raw = m.created ?? m.timestamp;\n if (typeof raw === 'number') return raw < 100000000000 ? raw * 1000 : raw;\n if (typeof raw === 'string' && /^\\d+(\\.\\d+)?$/.test(raw)) {\n const n = Number(raw); return n < 100000000000 ? n * 1000 : n;\n }\n return typeof raw === 'string' ? Date.parse(raw) : NaN;\n}\nlet replied = false;\nfor (const m of msgs) {\n const at = messageTime(m);\n if (!Number.isFinite(at) || typeof m.owner !== 'boolean') {\n throw new Error('Unknown message timestamp/direction. Validate the Wati response mapping before sending.');\n }\n if (m.owner === false && at > since) replied = true;\n}\nreturn [{ json: { ...lead, replied } }];\n"
},
"id": "classify-replies-before-followup",
"name": "Replied? (24h)",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1700,
300
]
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "no-reply",
"leftValue": "={{ $json.replied }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "false",
"singleValue": true
}
}
],
"combinator": "and"
},
"options": {}
},
"id": "allow-followup-with-verified-history",
"name": "No reply yet (24h)?",
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
1920,
300
]
}
],
"connections": {
"New lead (webhook)": {
"main": [
[
{
"node": "Normalize lead",
"type": "main",
"index": 0
}
]
]
},
"Normalize lead": {
"main": [
[
{
"node": "Send instant WhatsApp reply",
"type": "main",
"index": 0
}
]
]
},
"Send instant WhatsApp reply": {
"main": [
[
{
"node": "Log lead to sheet",
"type": "main",
"index": 0
}
]
]
},
"Log lead to sheet": {
"main": [
[
{
"node": "Wait 24 hours",
"type": "main",
"index": 0
}
]
]
},
"Wait 24 hours": {
"main": [
[
{
"node": "Check replies (24h)",
"type": "main",
"index": 0
}
]
]
},
"Check replies (24h)": {
"main": [
[
{
"node": "Replied? (24h)",
"type": "main",
"index": 0
}
]
]
},
"Replied? (24h)": {
"main": [
[
{
"node": "No reply yet (24h)?",
"type": "main",
"index": 0
}
]
]
},
"No reply yet (24h)?": {
"main": [
[
{
"node": "Send 24h follow-up",
"type": "main",
"index": 0
}
],
[]
]
}
},
"settings": {
"executionOrder": "v1"
},
"pinData": {},
"meta": {
"templateCredsSetupCompleted": false
}
}