-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_interactive.rb
More file actions
400 lines (355 loc) · 12 KB
/
enhanced_interactive.rb
File metadata and controls
400 lines (355 loc) · 12 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# frozen_string_literal: true
# WhatsApp Enhanced Interactive Messages Examples
# Demonstrates updated button headers and list validations
require 'kapso-client-ruby'
require 'dotenv'
Dotenv.load
# Initialize client
client = KapsoClientRuby::Client.new(
access_token: ENV['WHATSAPP_ACCESS_TOKEN']
)
phone_number_id = ENV['PHONE_NUMBER_ID']
recipient = '+1234567890'
puts "=== Enhanced Interactive Messages ===\n\n"
# 1. INTERACTIVE BUTTONS - Text Header
puts "1. Buttons with text header..."
begin
response = client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'text',
text: 'Appointment Confirmation'
},
body_text: 'We have an appointment slot available for you tomorrow at 2:00 PM.',
buttons: [
{ type: 'reply', reply: { id: 'confirm', title: 'Confirm' } },
{ type: 'reply', reply: { id: 'reschedule', title: 'Reschedule' } },
{ type: 'reply', reply: { id: 'cancel', title: 'Cancel' } }
],
footer: 'Please respond within 24 hours'
)
puts "✓ Sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 2. INTERACTIVE BUTTONS - Image Header (NEW!)
puts "2. Buttons with image header (NEW FEATURE)..."
begin
response = client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'image',
image: {
link: 'https://example.com/product-image.jpg'
}
},
body_text: 'Interested in this product? Choose an option below:',
buttons: [
{ type: 'reply', reply: { id: 'buy_now', title: 'Buy Now' } },
{ type: 'reply', reply: { id: 'add_cart', title: 'Add to Cart' } },
{ type: 'reply', reply: { id: 'more_info', title: 'More Info' } }
],
footer: '30-day money-back guarantee'
)
puts "✓ Sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 3. INTERACTIVE BUTTONS - Video Header (NEW!)
puts "3. Buttons with video header (NEW FEATURE)..."
begin
response = client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'video',
video: {
link: 'https://example.com/tutorial.mp4'
}
},
body_text: 'Watch this quick tutorial. Was it helpful?',
buttons: [
{ type: 'reply', reply: { id: 'yes', title: 'Yes, helpful' } },
{ type: 'reply', reply: { id: 'no', title: 'Not helpful' } }
]
)
puts "✓ Sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 4. INTERACTIVE BUTTONS - Document Header (NEW!)
puts "4. Buttons with document header (NEW FEATURE)..."
begin
response = client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'document',
document: {
link: 'https://example.com/invoice-12345.pdf',
filename: 'invoice.pdf'
}
},
body_text: 'Your invoice is ready. Please review and confirm:',
buttons: [
{ type: 'reply', reply: { id: 'approve', title: 'Approve' } },
{ type: 'reply', reply: { id: 'question', title: 'Question' } }
],
footer: 'Invoice #12345'
)
puts "✓ Sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 5. INTERACTIVE LIST - Extended Body Text
puts "5. List with extended body text (4096 chars)..."
long_body = "Welcome to our service menu! " + ("Here's what we offer. " * 100)
long_body = long_body[0...4000] # Keep under 4096
begin
response = client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'text',
text: 'Our Services'
},
body_text: long_body,
button_text: 'View Services',
sections: [
{
title: 'Popular Services',
rows: [
{ id: 'service1', title: 'Web Design', description: 'Custom website design' },
{ id: 'service2', title: 'Mobile Apps', description: 'iOS and Android apps' },
{ id: 'service3', title: 'SEO', description: 'Search engine optimization' }
]
}
],
footer: 'Select a service for details'
)
puts "✓ Sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 6. INTERACTIVE LIST - Maximum Rows (10)
puts "6. List with maximum rows (10 total)..."
begin
response = client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Choose from our top 10 products:',
button_text: 'View Products',
sections: [
{
title: 'Electronics',
rows: [
{ id: 'p1', title: 'Laptop', description: 'High-performance laptop' },
{ id: 'p2', title: 'Phone', description: 'Latest smartphone' },
{ id: 'p3', title: 'Tablet', description: 'Portable tablet' },
{ id: 'p4', title: 'Watch', description: 'Smart watch' },
{ id: 'p5', title: 'Earbuds', description: 'Wireless earbuds' }
]
},
{
title: 'Accessories',
rows: [
{ id: 'a1', title: 'Case', description: 'Protective case' },
{ id: 'a2', title: 'Charger', description: 'Fast charger' },
{ id: 'a3', title: 'Cable', description: 'USB-C cable' },
{ id: 'a4', title: 'Stand', description: 'Phone stand' },
{ id: 'a5', title: 'Adapter', description: 'Power adapter' }
]
}
]
)
puts "✓ Sent with 10 rows! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 7. VALIDATION TESTS
puts "7. Testing enhanced validations...\n"
# Test button count validation (max 3)
puts " Testing button count (max 3)..."
begin
client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Test',
buttons: [
{ type: 'reply', reply: { id: '1', title: 'Button 1' } },
{ type: 'reply', reply: { id: '2', title: 'Button 2' } },
{ type: 'reply', reply: { id: '3', title: 'Button 3' } },
{ type: 'reply', reply: { id: '4', title: 'Button 4' } } # Too many!
]
)
puts " ✗ Validation failed\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test list body text limit (4096)
puts " Testing list body text (max 4096)..."
begin
long_text = 'a' * 4097 # Exceeds limit
client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: recipient,
body_text: long_text,
button_text: 'View',
sections: [{ title: 'Test', rows: [{ id: '1', title: 'Item' }] }]
)
puts " ✗ Validation failed\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test list row count (max 10)
puts " Testing list row count (max 10)..."
begin
too_many_rows = (1..11).map { |i| { id: "item#{i}", title: "Item #{i}" } }
client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Select',
button_text: 'View',
sections: [{ title: 'Items', rows: too_many_rows }]
)
puts " ✗ Validation failed\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test list header type (text only)
puts " Testing list header type (text only)..."
begin
client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'image', # Lists only support text headers!
image: { link: 'https://example.com/image.jpg' }
},
body_text: 'Test',
button_text: 'View',
sections: [{ title: 'Test', rows: [{ id: '1', title: 'Item' }] }]
)
puts " ✗ Validation failed\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test button header with invalid media
puts " Testing button header validation..."
begin
client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'image'
# Missing image field!
},
body_text: 'Test',
buttons: [{ type: 'reply', reply: { id: '1', title: 'OK' } }]
)
puts " ✗ Validation failed\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
puts "\n=== All enhanced features demonstrated! ===\n"
# USE CASES
puts "\n=== Real-World Use Cases ===\n"
# E-commerce: Product selection with image
def product_quick_buy(client, phone_number_id, customer_phone)
client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: customer_phone,
header: {
type: 'image',
image: { link: 'https://shop.example.com/featured-product.jpg' }
},
body_text: '🔥 Flash Sale! This item is 50% off for the next 2 hours.',
buttons: [
{ type: 'reply', reply: { id: 'buy', title: 'Buy Now' } },
{ type: 'reply', reply: { id: 'cart', title: 'Add to Cart' } },
{ type: 'reply', reply: { id: 'notify', title: 'Notify Later' } }
],
footer: 'Limited stock available'
)
end
# Customer Support: Video tutorial with feedback
def send_tutorial_with_feedback(client, phone_number_id, customer_phone)
client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: customer_phone,
header: {
type: 'video',
video: { link: 'https://support.example.com/tutorials/setup.mp4' }
},
body_text: '📺 Here\'s a quick video showing how to set up your device. Did this help?',
buttons: [
{ type: 'reply', reply: { id: 'solved', title: 'Problem Solved' } },
{ type: 'reply', reply: { id: 'more_help', title: 'Need More Help' } }
],
footer: 'Support Team'
)
end
# Document approval workflow
def document_approval(client, phone_number_id, manager_phone)
client.messages.send_interactive_buttons(
phone_number_id: phone_number_id,
to: manager_phone,
header: {
type: 'document',
document: {
link: 'https://docs.example.com/contracts/2024-Q1.pdf',
filename: 'Q1-2024-Contract.pdf'
}
},
body_text: '📄 Contract requires your approval. Please review the attached document.',
buttons: [
{ type: 'reply', reply: { id: 'approve', title: 'Approve' } },
{ type: 'reply', reply: { id: 'reject', title: 'Reject' } },
{ type: 'reply', reply: { id: 'clarify', title: 'Ask Question' } }
],
footer: 'Deadline: Friday 5 PM'
)
end
# Comprehensive service menu (using max rows)
def comprehensive_service_menu(client, phone_number_id, customer_phone)
client.messages.send_interactive_list(
phone_number_id: phone_number_id,
to: customer_phone,
header: {
type: 'text',
text: 'Complete Service Catalog'
},
body_text: 'Browse our full catalog of services. We offer professional solutions across multiple categories to meet all your business needs.',
button_text: 'Browse Services',
sections: [
{
title: 'Design Services',
rows: [
{ id: 'web_design', title: 'Web Design', description: 'Custom website design' },
{ id: 'brand_design', title: 'Brand Design', description: 'Logo and branding' },
{ id: 'ui_design', title: 'UI/UX Design', description: 'User interface design' }
]
},
{
title: 'Development Services',
rows: [
{ id: 'web_dev', title: 'Web Development', description: 'Full-stack development' },
{ id: 'mobile_dev', title: 'Mobile Apps', description: 'iOS & Android apps' },
{ id: 'api_dev', title: 'API Development', description: 'Backend APIs' }
]
},
{
title: 'Marketing Services',
rows: [
{ id: 'seo', title: 'SEO', description: 'Search optimization' },
{ id: 'social', title: 'Social Media', description: 'Social media marketing' },
{ id: 'email', title: 'Email Marketing', description: 'Email campaigns' },
{ id: 'content', title: 'Content Marketing', description: 'Content creation' }
]
}
],
footer: 'Get a free consultation'
)
end