kapso-client-ruby 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +81 -81
- data/CHANGELOG.md +262 -91
- data/Gemfile +20 -20
- data/RAILS_INTEGRATION.md +477 -477
- data/README.md +1053 -752
- data/Rakefile +40 -40
- data/TEMPLATE_TOOLS_GUIDE.md +120 -120
- data/WHATSAPP_24_HOUR_GUIDE.md +133 -133
- data/examples/advanced_features.rb +352 -349
- data/examples/advanced_messaging.rb +241 -0
- data/examples/basic_messaging.rb +139 -136
- data/examples/enhanced_interactive.rb +400 -0
- data/examples/flows_usage.rb +307 -0
- data/examples/interactive_messages.rb +343 -0
- data/examples/media_management.rb +256 -253
- data/examples/rails/jobs.rb +387 -387
- data/examples/rails/models.rb +239 -239
- data/examples/rails/notifications_controller.rb +226 -226
- data/examples/template_management.rb +393 -390
- data/kapso-ruby-logo.jpg +0 -0
- data/lib/kapso_client_ruby/client.rb +321 -316
- data/lib/kapso_client_ruby/errors.rb +348 -329
- data/lib/kapso_client_ruby/rails/generators/install_generator.rb +75 -75
- data/lib/kapso_client_ruby/rails/generators/templates/env.erb +20 -20
- data/lib/kapso_client_ruby/rails/generators/templates/initializer.rb.erb +32 -32
- data/lib/kapso_client_ruby/rails/generators/templates/message_service.rb.erb +137 -137
- data/lib/kapso_client_ruby/rails/generators/templates/webhook_controller.rb.erb +61 -61
- data/lib/kapso_client_ruby/rails/railtie.rb +54 -54
- data/lib/kapso_client_ruby/rails/service.rb +188 -188
- data/lib/kapso_client_ruby/rails/tasks.rake +166 -166
- data/lib/kapso_client_ruby/resources/calls.rb +172 -172
- data/lib/kapso_client_ruby/resources/contacts.rb +190 -190
- data/lib/kapso_client_ruby/resources/conversations.rb +103 -103
- data/lib/kapso_client_ruby/resources/flows.rb +382 -0
- data/lib/kapso_client_ruby/resources/media.rb +205 -205
- data/lib/kapso_client_ruby/resources/messages.rb +760 -380
- data/lib/kapso_client_ruby/resources/phone_numbers.rb +85 -85
- data/lib/kapso_client_ruby/resources/templates.rb +283 -283
- data/lib/kapso_client_ruby/types.rb +348 -262
- data/lib/kapso_client_ruby/version.rb +5 -5
- data/lib/kapso_client_ruby.rb +75 -74
- data/scripts/.env.example +17 -17
- data/scripts/kapso_template_finder.rb +91 -91
- data/scripts/sdk_setup.rb +404 -404
- data/scripts/test.rb +60 -60
- metadata +12 -3
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# WhatsApp Interactive Messages Examples
|
|
4
|
+
# Demonstrates CTA URL and Catalog messages
|
|
5
|
+
|
|
6
|
+
require 'kapso-client-ruby'
|
|
7
|
+
require 'dotenv'
|
|
8
|
+
|
|
9
|
+
Dotenv.load
|
|
10
|
+
|
|
11
|
+
# Initialize client
|
|
12
|
+
client = KapsoClientRuby::Client.new(
|
|
13
|
+
access_token: ENV['WHATSAPP_ACCESS_TOKEN']
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
phone_number_id = ENV['PHONE_NUMBER_ID']
|
|
17
|
+
recipient = '+1234567890'
|
|
18
|
+
|
|
19
|
+
puts "=== Interactive Messages Examples ===\n\n"
|
|
20
|
+
|
|
21
|
+
# 1. INTERACTIVE CTA URL - Text Header
|
|
22
|
+
|
|
23
|
+
puts "1. Sending CTA URL message with text header..."
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
response = client.messages.send_interactive_cta_url(
|
|
27
|
+
phone_number_id: phone_number_id,
|
|
28
|
+
to: recipient,
|
|
29
|
+
header: {
|
|
30
|
+
type: 'text',
|
|
31
|
+
text: 'Special Offer'
|
|
32
|
+
},
|
|
33
|
+
body_text: 'Get 25% off your first purchase! Click below to shop now.',
|
|
34
|
+
display_text: 'Shop Now',
|
|
35
|
+
url: 'https://shop.example.com?utm_source=whatsapp',
|
|
36
|
+
footer_text: 'Limited time offer'
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
|
|
40
|
+
rescue StandardError => e
|
|
41
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# 2. INTERACTIVE CTA URL - Image Header
|
|
45
|
+
|
|
46
|
+
puts "2. Sending CTA URL message with image header..."
|
|
47
|
+
|
|
48
|
+
begin
|
|
49
|
+
response = client.messages.send_interactive_cta_url(
|
|
50
|
+
phone_number_id: phone_number_id,
|
|
51
|
+
to: recipient,
|
|
52
|
+
header: {
|
|
53
|
+
type: 'image',
|
|
54
|
+
image: {
|
|
55
|
+
link: 'https://example.com/banner.jpg'
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
body_text: 'New collection just dropped! Browse our latest arrivals.',
|
|
59
|
+
display_text: 'View Collection',
|
|
60
|
+
url: 'https://shop.example.com/new-arrivals'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
|
|
64
|
+
rescue StandardError => e
|
|
65
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# 3. INTERACTIVE CTA URL - Video Header
|
|
69
|
+
|
|
70
|
+
puts "3. Sending CTA URL message with video header..."
|
|
71
|
+
|
|
72
|
+
begin
|
|
73
|
+
response = client.messages.send_interactive_cta_url(
|
|
74
|
+
phone_number_id: phone_number_id,
|
|
75
|
+
to: recipient,
|
|
76
|
+
header: {
|
|
77
|
+
type: 'video',
|
|
78
|
+
video: {
|
|
79
|
+
link: 'https://example.com/product-demo.mp4'
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
body_text: 'Watch our product in action! See how it can transform your daily routine.',
|
|
83
|
+
display_text: 'Learn More',
|
|
84
|
+
url: 'https://example.com/product-details',
|
|
85
|
+
footer_text: 'Free shipping available'
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
|
|
89
|
+
rescue StandardError => e
|
|
90
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# 4. INTERACTIVE CTA URL - Document Header
|
|
94
|
+
|
|
95
|
+
puts "4. Sending CTA URL message with document header..."
|
|
96
|
+
|
|
97
|
+
begin
|
|
98
|
+
response = client.messages.send_interactive_cta_url(
|
|
99
|
+
phone_number_id: phone_number_id,
|
|
100
|
+
to: recipient,
|
|
101
|
+
header: {
|
|
102
|
+
type: 'document',
|
|
103
|
+
document: {
|
|
104
|
+
link: 'https://example.com/catalog.pdf',
|
|
105
|
+
filename: 'catalog.pdf'
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
body_text: 'Download our full catalog and explore all our products.',
|
|
109
|
+
display_text: 'View Online',
|
|
110
|
+
url: 'https://catalog.example.com'
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
|
|
114
|
+
rescue StandardError => e
|
|
115
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# 5. INTERACTIVE CTA URL - No Header
|
|
119
|
+
|
|
120
|
+
puts "5. Sending CTA URL message without header..."
|
|
121
|
+
|
|
122
|
+
begin
|
|
123
|
+
response = client.messages.send_interactive_cta_url(
|
|
124
|
+
phone_number_id: phone_number_id,
|
|
125
|
+
to: recipient,
|
|
126
|
+
body_text: 'Join our exclusive members club and get access to special deals!',
|
|
127
|
+
display_text: 'Join Now',
|
|
128
|
+
url: 'https://members.example.com/signup'
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
puts "✓ Message sent! ID: #{response.messages.first.id}\n\n"
|
|
132
|
+
rescue StandardError => e
|
|
133
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# 6. CATALOG MESSAGE
|
|
137
|
+
|
|
138
|
+
puts "6. Sending catalog message..."
|
|
139
|
+
|
|
140
|
+
begin
|
|
141
|
+
response = client.messages.send_interactive_catalog_message(
|
|
142
|
+
phone_number_id: phone_number_id,
|
|
143
|
+
to: recipient,
|
|
144
|
+
body_text: 'Browse our entire product catalog on WhatsApp!',
|
|
145
|
+
thumbnail_product_retailer_id: 'SKU-001', # Product SKU to show as thumbnail
|
|
146
|
+
footer_text: 'Tap to explore products'
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
puts "✓ Catalog message sent! ID: #{response.messages.first.id}\n\n"
|
|
150
|
+
rescue StandardError => e
|
|
151
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# 7. CATALOG MESSAGE - Minimal
|
|
155
|
+
|
|
156
|
+
puts "7. Sending minimal catalog message..."
|
|
157
|
+
|
|
158
|
+
begin
|
|
159
|
+
response = client.messages.send_interactive_catalog_message(
|
|
160
|
+
phone_number_id: phone_number_id,
|
|
161
|
+
to: recipient,
|
|
162
|
+
body_text: 'Check out our products!',
|
|
163
|
+
thumbnail_product_retailer_id: 'FEATURED-PRODUCT-123'
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
puts "✓ Catalog message sent! ID: #{response.messages.first.id}\n\n"
|
|
167
|
+
rescue StandardError => e
|
|
168
|
+
puts "✗ Error: #{e.message}\n\n"
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# 8. VALIDATION EXAMPLES
|
|
172
|
+
|
|
173
|
+
puts "8. Testing validation...\n"
|
|
174
|
+
|
|
175
|
+
# Test body text length limit
|
|
176
|
+
puts " Testing body text length validation..."
|
|
177
|
+
begin
|
|
178
|
+
long_text = 'a' * 1025 # Exceeds 1024 character limit
|
|
179
|
+
client.messages.send_interactive_cta_url(
|
|
180
|
+
phone_number_id: phone_number_id,
|
|
181
|
+
to: recipient,
|
|
182
|
+
body_text: long_text,
|
|
183
|
+
display_text: 'Click',
|
|
184
|
+
url: 'https://example.com'
|
|
185
|
+
)
|
|
186
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
187
|
+
rescue ArgumentError => e
|
|
188
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Test display text length limit
|
|
192
|
+
puts " Testing display text length validation..."
|
|
193
|
+
begin
|
|
194
|
+
client.messages.send_interactive_cta_url(
|
|
195
|
+
phone_number_id: phone_number_id,
|
|
196
|
+
to: recipient,
|
|
197
|
+
body_text: 'Test message',
|
|
198
|
+
display_text: 'This is way too long for a button', # Exceeds 20 characters
|
|
199
|
+
url: 'https://example.com'
|
|
200
|
+
)
|
|
201
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
202
|
+
rescue ArgumentError => e
|
|
203
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Test URL validation
|
|
207
|
+
puts " Testing URL validation..."
|
|
208
|
+
begin
|
|
209
|
+
client.messages.send_interactive_cta_url(
|
|
210
|
+
phone_number_id: phone_number_id,
|
|
211
|
+
to: recipient,
|
|
212
|
+
body_text: 'Test message',
|
|
213
|
+
display_text: 'Click',
|
|
214
|
+
url: 'ftp://invalid-protocol.com' # Invalid protocol
|
|
215
|
+
)
|
|
216
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
217
|
+
rescue ArgumentError => e
|
|
218
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# Test footer text length
|
|
222
|
+
puts " Testing footer text length validation..."
|
|
223
|
+
begin
|
|
224
|
+
long_footer = 'a' * 61 # Exceeds 60 character limit
|
|
225
|
+
client.messages.send_interactive_cta_url(
|
|
226
|
+
phone_number_id: phone_number_id,
|
|
227
|
+
to: recipient,
|
|
228
|
+
body_text: 'Test message',
|
|
229
|
+
display_text: 'Click',
|
|
230
|
+
url: 'https://example.com',
|
|
231
|
+
footer_text: long_footer
|
|
232
|
+
)
|
|
233
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
234
|
+
rescue ArgumentError => e
|
|
235
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Test invalid header type
|
|
239
|
+
puts " Testing invalid header type validation..."
|
|
240
|
+
begin
|
|
241
|
+
client.messages.send_interactive_cta_url(
|
|
242
|
+
phone_number_id: phone_number_id,
|
|
243
|
+
to: recipient,
|
|
244
|
+
header: {
|
|
245
|
+
type: 'audio', # Invalid type for CTA URL
|
|
246
|
+
audio: { id: 'audio_id' }
|
|
247
|
+
},
|
|
248
|
+
body_text: 'Test message',
|
|
249
|
+
display_text: 'Click',
|
|
250
|
+
url: 'https://example.com'
|
|
251
|
+
)
|
|
252
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
253
|
+
rescue ArgumentError => e
|
|
254
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Test missing media in header
|
|
258
|
+
puts " Testing missing media in header validation..."
|
|
259
|
+
begin
|
|
260
|
+
client.messages.send_interactive_cta_url(
|
|
261
|
+
phone_number_id: phone_number_id,
|
|
262
|
+
to: recipient,
|
|
263
|
+
header: {
|
|
264
|
+
type: 'image'
|
|
265
|
+
# Missing 'image' field
|
|
266
|
+
},
|
|
267
|
+
body_text: 'Test message',
|
|
268
|
+
display_text: 'Click',
|
|
269
|
+
url: 'https://example.com'
|
|
270
|
+
)
|
|
271
|
+
puts " ✗ Validation failed - should have raised error\n"
|
|
272
|
+
rescue ArgumentError => e
|
|
273
|
+
puts " ✓ Validation passed: #{e.message}\n"
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
puts "\n=== All examples completed! ===\n"
|
|
277
|
+
|
|
278
|
+
# USE CASES
|
|
279
|
+
|
|
280
|
+
puts "\n=== Real-World Use Cases ===\n"
|
|
281
|
+
|
|
282
|
+
# E-commerce: Product promotion
|
|
283
|
+
def send_product_promotion(client, phone_number_id, customer_phone)
|
|
284
|
+
client.messages.send_interactive_cta_url(
|
|
285
|
+
phone_number_id: phone_number_id,
|
|
286
|
+
to: customer_phone,
|
|
287
|
+
header: {
|
|
288
|
+
type: 'image',
|
|
289
|
+
image: { link: 'https://shop.example.com/products/summer-dress.jpg' }
|
|
290
|
+
},
|
|
291
|
+
body_text: '🌟 Summer Sale! Get 40% off this beautiful dress. Limited stock available!',
|
|
292
|
+
display_text: 'Buy Now',
|
|
293
|
+
url: 'https://shop.example.com/products/summer-dress?utm_campaign=whatsapp',
|
|
294
|
+
footer_text: 'Free shipping on orders over $50'
|
|
295
|
+
)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Event Registration
|
|
299
|
+
def send_event_registration(client, phone_number_id, attendee_phone)
|
|
300
|
+
client.messages.send_interactive_cta_url(
|
|
301
|
+
phone_number_id: phone_number_id,
|
|
302
|
+
to: attendee_phone,
|
|
303
|
+
header: {
|
|
304
|
+
type: 'video',
|
|
305
|
+
video: { link: 'https://events.example.com/preview.mp4' }
|
|
306
|
+
},
|
|
307
|
+
body_text: '🎉 Join us for the biggest tech conference of the year! Early bird tickets now available.',
|
|
308
|
+
display_text: 'Register',
|
|
309
|
+
url: 'https://events.example.com/techcon2024/register',
|
|
310
|
+
footer_text: 'Limited seats available'
|
|
311
|
+
)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Customer Support: Knowledge Base
|
|
315
|
+
def send_support_article(client, phone_number_id, customer_phone)
|
|
316
|
+
client.messages.send_interactive_cta_url(
|
|
317
|
+
phone_number_id: phone_number_id,
|
|
318
|
+
to: customer_phone,
|
|
319
|
+
header: {
|
|
320
|
+
type: 'document',
|
|
321
|
+
document: {
|
|
322
|
+
link: 'https://support.example.com/guides/getting-started.pdf',
|
|
323
|
+
filename: 'getting-started.pdf'
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
body_text: 'Need help getting started? Check out our comprehensive guide!',
|
|
327
|
+
display_text: 'View Guide',
|
|
328
|
+
url: 'https://support.example.com/getting-started'
|
|
329
|
+
)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Restaurant: Menu and Ordering
|
|
333
|
+
def send_restaurant_menu(client, phone_number_id, customer_phone)
|
|
334
|
+
client.messages.send_interactive_catalog_message(
|
|
335
|
+
phone_number_id: phone_number_id,
|
|
336
|
+
to: customer_phone,
|
|
337
|
+
body_text: '🍕 Hungry? Browse our full menu and order for delivery!',
|
|
338
|
+
thumbnail_product_retailer_id: 'PIZZA-MARGHERITA',
|
|
339
|
+
footer_text: 'Delivery in 30 minutes or less'
|
|
340
|
+
)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
puts "✓ Use case examples defined and ready to use!\n"
|