-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_messages.rb
More file actions
343 lines (293 loc) · 9.48 KB
/
interactive_messages.rb
File metadata and controls
343 lines (293 loc) · 9.48 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
# frozen_string_literal: true
# WhatsApp Interactive Messages Examples
# Demonstrates CTA URL and Catalog messages
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 "=== Interactive Messages Examples ===\n\n"
# 1. INTERACTIVE CTA URL - Text Header
puts "1. Sending CTA URL message with text header..."
begin
response = client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'text',
text: 'Special Offer'
},
body_text: 'Get 25% off your first purchase! Click below to shop now.',
display_text: 'Shop Now',
url: 'https://shop.example.com?utm_source=whatsapp',
footer_text: 'Limited time offer'
)
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 2. INTERACTIVE CTA URL - Image Header
puts "2. Sending CTA URL message with image header..."
begin
response = client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'image',
image: {
link: 'https://example.com/banner.jpg'
}
},
body_text: 'New collection just dropped! Browse our latest arrivals.',
display_text: 'View Collection',
url: 'https://shop.example.com/new-arrivals'
)
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 3. INTERACTIVE CTA URL - Video Header
puts "3. Sending CTA URL message with video header..."
begin
response = client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'video',
video: {
link: 'https://example.com/product-demo.mp4'
}
},
body_text: 'Watch our product in action! See how it can transform your daily routine.',
display_text: 'Learn More',
url: 'https://example.com/product-details',
footer_text: 'Free shipping available'
)
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 4. INTERACTIVE CTA URL - Document Header
puts "4. Sending CTA URL message with document header..."
begin
response = client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'document',
document: {
link: 'https://example.com/catalog.pdf',
filename: 'catalog.pdf'
}
},
body_text: 'Download our full catalog and explore all our products.',
display_text: 'View Online',
url: 'https://catalog.example.com'
)
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 5. INTERACTIVE CTA URL - No Header
puts "5. Sending CTA URL message without header..."
begin
response = client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Join our exclusive members club and get access to special deals!',
display_text: 'Join Now',
url: 'https://members.example.com/signup'
)
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 6. CATALOG MESSAGE
puts "6. Sending catalog message..."
begin
response = client.messages.send_interactive_catalog_message(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Browse our entire product catalog on WhatsApp!',
thumbnail_product_retailer_id: 'SKU-001', # Product SKU to show as thumbnail
footer_text: 'Tap to explore products'
)
puts "✓ Catalog message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 7. CATALOG MESSAGE - Minimal
puts "7. Sending minimal catalog message..."
begin
response = client.messages.send_interactive_catalog_message(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Check out our products!',
thumbnail_product_retailer_id: 'FEATURED-PRODUCT-123'
)
puts "✓ Catalog message sent! ID: #{response.messages.first.id}\n\n"
rescue StandardError => e
puts "✗ Error: #{e.message}\n\n"
end
# 8. VALIDATION EXAMPLES
puts "8. Testing validation...\n"
# Test body text length limit
puts " Testing body text length validation..."
begin
long_text = 'a' * 1025 # Exceeds 1024 character limit
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
body_text: long_text,
display_text: 'Click',
url: 'https://example.com'
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test display text length limit
puts " Testing display text length validation..."
begin
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Test message',
display_text: 'This is way too long for a button', # Exceeds 20 characters
url: 'https://example.com'
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test URL validation
puts " Testing URL validation..."
begin
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Test message',
display_text: 'Click',
url: 'ftp://invalid-protocol.com' # Invalid protocol
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test footer text length
puts " Testing footer text length validation..."
begin
long_footer = 'a' * 61 # Exceeds 60 character limit
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
body_text: 'Test message',
display_text: 'Click',
url: 'https://example.com',
footer_text: long_footer
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test invalid header type
puts " Testing invalid header type validation..."
begin
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'audio', # Invalid type for CTA URL
audio: { id: 'audio_id' }
},
body_text: 'Test message',
display_text: 'Click',
url: 'https://example.com'
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
# Test missing media in header
puts " Testing missing media in header validation..."
begin
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: recipient,
header: {
type: 'image'
# Missing 'image' field
},
body_text: 'Test message',
display_text: 'Click',
url: 'https://example.com'
)
puts " ✗ Validation failed - should have raised error\n"
rescue ArgumentError => e
puts " ✓ Validation passed: #{e.message}\n"
end
puts "\n=== All examples completed! ===\n"
# USE CASES
puts "\n=== Real-World Use Cases ===\n"
# E-commerce: Product promotion
def send_product_promotion(client, phone_number_id, customer_phone)
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: customer_phone,
header: {
type: 'image',
image: { link: 'https://shop.example.com/products/summer-dress.jpg' }
},
body_text: '🌟 Summer Sale! Get 40% off this beautiful dress. Limited stock available!',
display_text: 'Buy Now',
url: 'https://shop.example.com/products/summer-dress?utm_campaign=whatsapp',
footer_text: 'Free shipping on orders over $50'
)
end
# Event Registration
def send_event_registration(client, phone_number_id, attendee_phone)
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: attendee_phone,
header: {
type: 'video',
video: { link: 'https://events.example.com/preview.mp4' }
},
body_text: '🎉 Join us for the biggest tech conference of the year! Early bird tickets now available.',
display_text: 'Register',
url: 'https://events.example.com/techcon2024/register',
footer_text: 'Limited seats available'
)
end
# Customer Support: Knowledge Base
def send_support_article(client, phone_number_id, customer_phone)
client.messages.send_interactive_cta_url(
phone_number_id: phone_number_id,
to: customer_phone,
header: {
type: 'document',
document: {
link: 'https://support.example.com/guides/getting-started.pdf',
filename: 'getting-started.pdf'
}
},
body_text: 'Need help getting started? Check out our comprehensive guide!',
display_text: 'View Guide',
url: 'https://support.example.com/getting-started'
)
end
# Restaurant: Menu and Ordering
def send_restaurant_menu(client, phone_number_id, customer_phone)
client.messages.send_interactive_catalog_message(
phone_number_id: phone_number_id,
to: customer_phone,
body_text: '🍕 Hungry? Browse our full menu and order for delivery!',
thumbnail_product_retailer_id: 'PIZZA-MARGHERITA',
footer_text: 'Delivery in 30 minutes or less'
)
end
puts "✓ Use case examples defined and ready to use!\n"