kapso-client-ruby 1.0.0 → 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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +81 -81
  3. data/CHANGELOG.md +262 -91
  4. data/Gemfile +20 -20
  5. data/RAILS_INTEGRATION.md +478 -0
  6. data/README.md +1053 -734
  7. data/Rakefile +40 -40
  8. data/TEMPLATE_TOOLS_GUIDE.md +120 -120
  9. data/WHATSAPP_24_HOUR_GUIDE.md +133 -133
  10. data/examples/advanced_features.rb +352 -349
  11. data/examples/advanced_messaging.rb +241 -0
  12. data/examples/basic_messaging.rb +139 -136
  13. data/examples/enhanced_interactive.rb +400 -0
  14. data/examples/flows_usage.rb +307 -0
  15. data/examples/interactive_messages.rb +343 -0
  16. data/examples/media_management.rb +256 -253
  17. data/examples/rails/jobs.rb +388 -0
  18. data/examples/rails/models.rb +240 -0
  19. data/examples/rails/notifications_controller.rb +227 -0
  20. data/examples/template_management.rb +393 -390
  21. data/kapso-ruby-logo.jpg +0 -0
  22. data/lib/kapso_client_ruby/client.rb +321 -316
  23. data/lib/kapso_client_ruby/errors.rb +348 -329
  24. data/lib/kapso_client_ruby/rails/generators/install_generator.rb +76 -0
  25. data/lib/kapso_client_ruby/rails/generators/templates/env.erb +21 -0
  26. data/lib/kapso_client_ruby/rails/generators/templates/initializer.rb.erb +33 -0
  27. data/lib/kapso_client_ruby/rails/generators/templates/message_service.rb.erb +138 -0
  28. data/lib/kapso_client_ruby/rails/generators/templates/webhook_controller.rb.erb +62 -0
  29. data/lib/kapso_client_ruby/rails/railtie.rb +55 -0
  30. data/lib/kapso_client_ruby/rails/service.rb +189 -0
  31. data/lib/kapso_client_ruby/rails/tasks.rake +167 -0
  32. data/lib/kapso_client_ruby/resources/calls.rb +172 -172
  33. data/lib/kapso_client_ruby/resources/contacts.rb +190 -190
  34. data/lib/kapso_client_ruby/resources/conversations.rb +103 -103
  35. data/lib/kapso_client_ruby/resources/flows.rb +382 -0
  36. data/lib/kapso_client_ruby/resources/media.rb +205 -205
  37. data/lib/kapso_client_ruby/resources/messages.rb +760 -380
  38. data/lib/kapso_client_ruby/resources/phone_numbers.rb +85 -85
  39. data/lib/kapso_client_ruby/resources/templates.rb +283 -283
  40. data/lib/kapso_client_ruby/types.rb +348 -262
  41. data/lib/kapso_client_ruby/version.rb +5 -5
  42. data/lib/kapso_client_ruby.rb +75 -68
  43. data/scripts/.env.example +17 -17
  44. data/scripts/kapso_template_finder.rb +91 -91
  45. data/scripts/sdk_setup.rb +404 -404
  46. data/scripts/test.rb +60 -60
  47. metadata +24 -3
@@ -1,391 +1,394 @@
1
- # frozen_string_literal: true
2
-
3
- require 'whatsapp_cloud_api'
4
-
5
- puts "=== Template Management Examples ==="
6
-
7
- # Initialize client
8
- client = WhatsAppCloudApi::Client.new(
9
- access_token: ENV['WHATSAPP_ACCESS_TOKEN']
10
- )
11
-
12
- business_account_id = ENV['BUSINESS_ACCOUNT_ID']
13
-
14
- # Example 1: List Existing Templates
15
- puts "\n--- List Existing Templates ---"
16
-
17
- begin
18
- templates = client.templates.list(business_account_id: business_account_id)
19
-
20
- puts "Found #{templates.data.length} templates:"
21
- templates.data.each do |template|
22
- puts "- #{template.name} (#{template.language}) - Status: #{template.status}"
23
- end
24
-
25
- # Handle pagination if there are more results
26
- if templates.paging.after
27
- puts "\nMore templates available. Next cursor: #{templates.paging.after}"
28
- end
29
-
30
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
31
- puts "Error listing templates: #{e.message}"
32
- end
33
-
34
- # Example 2: Create Marketing Template
35
- puts "\n--- Create Marketing Template ---"
36
-
37
- begin
38
- # Build a marketing template with the helper method
39
- template_data = client.templates.build_marketing_template(
40
- name: 'ruby_sdk_promo',
41
- language: 'en_US',
42
- header: {
43
- type: 'HEADER',
44
- format: 'TEXT',
45
- text: 'Special Offer for {{1}}!'
46
- },
47
- body: 'Hi {{1}}, we have a special {{2}} discount just for you! Use code {{3}} to get {{4}} off your next purchase.',
48
- footer: 'This offer expires in 24 hours',
49
- buttons: [
50
- {
51
- type: 'URL',
52
- text: 'Shop Now',
53
- url: 'https://example.com/shop?code={{1}}'
54
- },
55
- {
56
- type: 'QUICK_REPLY',
57
- text: 'More Info'
58
- }
59
- ],
60
- body_example: {
61
- body_text: [['John', 'exclusive', 'SAVE20', '20%']]
62
- }
63
- )
64
-
65
- # Create the template
66
- response = client.templates.create(
67
- business_account_id: business_account_id,
68
- **template_data
69
- )
70
-
71
- puts "Marketing template created!"
72
- puts "Template ID: #{response.id}"
73
- puts "Status: #{response.status}"
74
-
75
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
76
- puts "Error creating marketing template: #{e.message}"
77
-
78
- if e.template_error?
79
- puts "Template-specific error - check template format and content"
80
- end
81
- end
82
-
83
- # Example 3: Create Authentication Template
84
- puts "\n--- Create Authentication Template ---"
85
-
86
- begin
87
- # Build authentication template using helper
88
- auth_template = client.templates.build_authentication_template(
89
- name: 'ruby_sdk_auth',
90
- language: 'en_US',
91
- ttl_seconds: 300, # 5 minutes
92
- code_expiration_minutes: 5,
93
- otp_type: 'COPY_CODE'
94
- )
95
-
96
- response = client.templates.create(
97
- business_account_id: business_account_id,
98
- **auth_template
99
- )
100
-
101
- puts "Authentication template created!"
102
- puts "Template ID: #{response.id}"
103
- puts "Status: #{response.status}"
104
-
105
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
106
- puts "Error creating auth template: #{e.message}"
107
- end
108
-
109
- # Example 4: Create Utility Template
110
- puts "\n--- Create Utility Template ---"
111
-
112
- begin
113
- utility_template = client.templates.build_utility_template(
114
- name: 'ruby_sdk_notification',
115
- language: 'en_US',
116
- header: {
117
- type: 'HEADER',
118
- format: 'TEXT',
119
- text: 'Order Update'
120
- },
121
- body: 'Your order #{{1}} has been {{2}}. Estimated delivery: {{3}}.',
122
- footer: 'Thank you for choosing our service',
123
- buttons: [
124
- {
125
- type: 'URL',
126
- text: 'Track Order',
127
- url: 'https://example.com/track/{{1}}'
128
- }
129
- ],
130
- body_example: {
131
- body_text: [['12345', 'shipped', 'Tomorrow 2-4 PM']]
132
- }
133
- )
134
-
135
- response = client.templates.create(
136
- business_account_id: business_account_id,
137
- **utility_template
138
- )
139
-
140
- puts "Utility template created!"
141
- puts "Template ID: #{response.id}"
142
-
143
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
144
- puts "Error creating utility template: #{e.message}"
145
- end
146
-
147
- # Example 5: Create Complex Template with All Components
148
- puts "\n--- Create Complex Template ---"
149
-
150
- begin
151
- components = [
152
- # Header with image
153
- {
154
- type: 'HEADER',
155
- format: 'IMAGE',
156
- example: {
157
- header_handle: ['https://example.com/header-image.jpg']
158
- }
159
- },
160
- # Body with variables
161
- {
162
- type: 'BODY',
163
- text: 'Hello {{1}}! Your {{2}} order totaling {{3}} is ready for pickup. ' \
164
- 'Please bring your ID and order confirmation {{4}}.',
165
- example: {
166
- body_text: [['John Doe', 'premium', '$125.99', '#ORD12345']]
167
- }
168
- },
169
- # Footer
170
- {
171
- type: 'FOOTER',
172
- text: 'Reply STOP to unsubscribe'
173
- },
174
- # Multiple buttons
175
- {
176
- type: 'BUTTONS',
177
- buttons: [
178
- {
179
- type: 'URL',
180
- text: 'View Order',
181
- url: 'https://example.com/orders/{{1}}'
182
- },
183
- {
184
- type: 'PHONE_NUMBER',
185
- text: 'Call Store',
186
- phone_number: '+1234567890'
187
- },
188
- {
189
- type: 'QUICK_REPLY',
190
- text: 'Reschedule'
191
- }
192
- ]
193
- }
194
- ]
195
-
196
- response = client.templates.create(
197
- business_account_id: business_account_id,
198
- name: 'ruby_sdk_complex',
199
- language: 'en_US',
200
- category: 'UTILITY',
201
- components: components
202
- )
203
-
204
- puts "Complex template created!"
205
- puts "Template ID: #{response.id}"
206
-
207
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
208
- puts "Error creating complex template: #{e.message}"
209
- end
210
-
211
- # Example 6: Send Template Messages
212
- puts "\n--- Send Template Messages ---"
213
-
214
- begin
215
- # Send simple template without variables
216
- response1 = client.messages.send_template(
217
- phone_number_id: ENV['PHONE_NUMBER_ID'],
218
- to: '+1234567890',
219
- name: 'hello_world', # Meta's sample template
220
- language: 'en_US'
221
- )
222
-
223
- puts "Simple template sent: #{response1.messages.first.id}"
224
-
225
- # Send template with parameters
226
- response2 = client.messages.send_template(
227
- phone_number_id: ENV['PHONE_NUMBER_ID'],
228
- to: '+1234567890',
229
- name: 'ruby_sdk_promo', # Our created template
230
- language: 'en_US',
231
- components: [
232
- {
233
- type: 'header',
234
- parameters: [
235
- { type: 'text', text: 'John Doe' }
236
- ]
237
- },
238
- {
239
- type: 'body',
240
- parameters: [
241
- { type: 'text', text: 'John' },
242
- { type: 'text', text: 'exclusive' },
243
- { type: 'text', text: 'RUBY20' },
244
- { type: 'text', text: '20%' }
245
- ]
246
- },
247
- {
248
- type: 'button',
249
- sub_type: 'url',
250
- index: '0',
251
- parameters: [
252
- { type: 'text', text: 'RUBY20' }
253
- ]
254
- }
255
- ]
256
- )
257
-
258
- puts "Parameterized template sent: #{response2.messages.first.id}"
259
-
260
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
261
- puts "Error sending template: #{e.message}"
262
-
263
- case e.category
264
- when :template
265
- puts "Template error - check template name, language, and parameters"
266
- when :parameter
267
- puts "Parameter error - check component parameters format"
268
- end
269
- end
270
-
271
- # Example 7: Template Management Operations
272
- puts "\n--- Template Management Operations ---"
273
-
274
- begin
275
- # Get specific template details
276
- template_id = 'your_template_id' # Replace with actual template ID
277
-
278
- template = client.templates.get(
279
- business_account_id: business_account_id,
280
- template_id: template_id
281
- )
282
-
283
- puts "Template Details:"
284
- puts "Name: #{template.name}"
285
- puts "Status: #{template.status}"
286
- puts "Category: #{template.category}"
287
- puts "Quality Score: #{template.quality_score_category}"
288
-
289
- # Update template (if allowed)
290
- if template.status == 'REJECTED'
291
- puts "Attempting to update rejected template..."
292
-
293
- client.templates.update(
294
- business_account_id: business_account_id,
295
- template_id: template_id,
296
- category: 'UTILITY' # Change category if needed
297
- )
298
-
299
- puts "Template updated successfully"
300
- end
301
-
302
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
303
- puts "Template management error: #{e.message}"
304
- end
305
-
306
- # Example 8: Delete Templates
307
- puts "\n--- Delete Templates ---"
308
-
309
- begin
310
- # Delete by template ID
311
- client.templates.delete(
312
- business_account_id: business_account_id,
313
- template_id: 'template_id_to_delete'
314
- )
315
-
316
- puts "Template deleted by ID"
317
-
318
- # Delete by name and language
319
- client.templates.delete(
320
- business_account_id: business_account_id,
321
- name: 'ruby_sdk_test',
322
- language: 'en_US'
323
- )
324
-
325
- puts "Template deleted by name"
326
-
327
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
328
- puts "Delete error: #{e.message}"
329
-
330
- if e.http_status == 404
331
- puts "Template not found - may already be deleted"
332
- end
333
- end
334
-
335
- # Example 9: Template Validation and Best Practices
336
- puts "\n--- Template Validation Examples ---"
337
-
338
- # Good template example
339
- def create_good_template(client, business_account_id)
340
- client.templates.create(
341
- business_account_id: business_account_id,
342
- name: 'good_template_example',
343
- language: 'en_US',
344
- category: 'UTILITY',
345
- components: [
346
- {
347
- type: 'BODY',
348
- text: 'Your appointment with {{1}} is confirmed for {{2}} at {{3}}.',
349
- example: {
350
- body_text: [['Dr. Smith', 'tomorrow', '2:00 PM']]
351
- }
352
- },
353
- {
354
- type: 'FOOTER',
355
- text: 'Reply CANCEL to cancel this appointment'
356
- }
357
- ]
358
- )
359
- end
360
-
361
- # Bad template example (this will likely be rejected)
362
- def create_bad_template_example(client, business_account_id)
363
- begin
364
- client.templates.create(
365
- business_account_id: business_account_id,
366
- name: 'bad_template_example',
367
- language: 'en_US',
368
- category: 'MARKETING',
369
- components: [
370
- {
371
- type: 'BODY',
372
- text: 'URGENT!!! Buy now or MISS OUT!!! Limited time offer!!!'
373
- # No example provided, excessive caps, promotional language
374
- }
375
- ]
376
- )
377
- rescue WhatsAppCloudApi::Errors::GraphApiError => e
378
- puts "Bad template rejected (expected): #{e.message}"
379
- end
380
- end
381
-
382
- begin
383
- good_response = create_good_template(client, business_account_id)
384
- puts "Good template created: #{good_response.id}"
385
- rescue => e
386
- puts "Error with good template: #{e.message}"
387
- end
388
-
389
- create_bad_template_example(client, business_account_id)
390
-
1
+ # frozen_string_literal: true
2
+
3
+ require 'kapso-client-ruby'
4
+ require 'dotenv'
5
+
6
+ Dotenv.load
7
+
8
+ puts "=== Template Management Examples ==="
9
+
10
+ # Initialize client
11
+ client = KapsoClientRuby::Client.new(
12
+ access_token: ENV['WHATSAPP_ACCESS_TOKEN']
13
+ )
14
+
15
+ business_account_id = ENV['BUSINESS_ACCOUNT_ID']
16
+
17
+ # Example 1: List Existing Templates
18
+ puts "\n--- List Existing Templates ---"
19
+
20
+ begin
21
+ templates = client.templates.list(business_account_id: business_account_id)
22
+
23
+ puts "Found #{templates.data.length} templates:"
24
+ templates.data.each do |template|
25
+ puts "- #{template.name} (#{template.language}) - Status: #{template.status}"
26
+ end
27
+
28
+ # Handle pagination if there are more results
29
+ if templates.paging.after
30
+ puts "\nMore templates available. Next cursor: #{templates.paging.after}"
31
+ end
32
+
33
+ rescue KapsoClientRuby::Errors::GraphApiError => e
34
+ puts "Error listing templates: #{e.message}"
35
+ end
36
+
37
+ # Example 2: Create Marketing Template
38
+ puts "\n--- Create Marketing Template ---"
39
+
40
+ begin
41
+ # Build a marketing template with the helper method
42
+ template_data = client.templates.build_marketing_template(
43
+ name: 'ruby_sdk_promo',
44
+ language: 'en_US',
45
+ header: {
46
+ type: 'HEADER',
47
+ format: 'TEXT',
48
+ text: 'Special Offer for {{1}}!'
49
+ },
50
+ body: 'Hi {{1}}, we have a special {{2}} discount just for you! Use code {{3}} to get {{4}} off your next purchase.',
51
+ footer: 'This offer expires in 24 hours',
52
+ buttons: [
53
+ {
54
+ type: 'URL',
55
+ text: 'Shop Now',
56
+ url: 'https://example.com/shop?code={{1}}'
57
+ },
58
+ {
59
+ type: 'QUICK_REPLY',
60
+ text: 'More Info'
61
+ }
62
+ ],
63
+ body_example: {
64
+ body_text: [['John', 'exclusive', 'SAVE20', '20%']]
65
+ }
66
+ )
67
+
68
+ # Create the template
69
+ response = client.templates.create(
70
+ business_account_id: business_account_id,
71
+ **template_data
72
+ )
73
+
74
+ puts "Marketing template created!"
75
+ puts "Template ID: #{response.id}"
76
+ puts "Status: #{response.status}"
77
+
78
+ rescue KapsoClientRuby::Errors::GraphApiError => e
79
+ puts "Error creating marketing template: #{e.message}"
80
+
81
+ if e.template_error?
82
+ puts "Template-specific error - check template format and content"
83
+ end
84
+ end
85
+
86
+ # Example 3: Create Authentication Template
87
+ puts "\n--- Create Authentication Template ---"
88
+
89
+ begin
90
+ # Build authentication template using helper
91
+ auth_template = client.templates.build_authentication_template(
92
+ name: 'ruby_sdk_auth',
93
+ language: 'en_US',
94
+ ttl_seconds: 300, # 5 minutes
95
+ code_expiration_minutes: 5,
96
+ otp_type: 'COPY_CODE'
97
+ )
98
+
99
+ response = client.templates.create(
100
+ business_account_id: business_account_id,
101
+ **auth_template
102
+ )
103
+
104
+ puts "Authentication template created!"
105
+ puts "Template ID: #{response.id}"
106
+ puts "Status: #{response.status}"
107
+
108
+ rescue KapsoClientRuby::Errors::GraphApiError => e
109
+ puts "Error creating auth template: #{e.message}"
110
+ end
111
+
112
+ # Example 4: Create Utility Template
113
+ puts "\n--- Create Utility Template ---"
114
+
115
+ begin
116
+ utility_template = client.templates.build_utility_template(
117
+ name: 'ruby_sdk_notification',
118
+ language: 'en_US',
119
+ header: {
120
+ type: 'HEADER',
121
+ format: 'TEXT',
122
+ text: 'Order Update'
123
+ },
124
+ body: 'Your order #{{1}} has been {{2}}. Estimated delivery: {{3}}.',
125
+ footer: 'Thank you for choosing our service',
126
+ buttons: [
127
+ {
128
+ type: 'URL',
129
+ text: 'Track Order',
130
+ url: 'https://example.com/track/{{1}}'
131
+ }
132
+ ],
133
+ body_example: {
134
+ body_text: [['12345', 'shipped', 'Tomorrow 2-4 PM']]
135
+ }
136
+ )
137
+
138
+ response = client.templates.create(
139
+ business_account_id: business_account_id,
140
+ **utility_template
141
+ )
142
+
143
+ puts "Utility template created!"
144
+ puts "Template ID: #{response.id}"
145
+
146
+ rescue KapsoClientRuby::Errors::GraphApiError => e
147
+ puts "Error creating utility template: #{e.message}"
148
+ end
149
+
150
+ # Example 5: Create Complex Template with All Components
151
+ puts "\n--- Create Complex Template ---"
152
+
153
+ begin
154
+ components = [
155
+ # Header with image
156
+ {
157
+ type: 'HEADER',
158
+ format: 'IMAGE',
159
+ example: {
160
+ header_handle: ['https://example.com/header-image.jpg']
161
+ }
162
+ },
163
+ # Body with variables
164
+ {
165
+ type: 'BODY',
166
+ text: 'Hello {{1}}! Your {{2}} order totaling {{3}} is ready for pickup. ' \
167
+ 'Please bring your ID and order confirmation {{4}}.',
168
+ example: {
169
+ body_text: [['John Doe', 'premium', '$125.99', '#ORD12345']]
170
+ }
171
+ },
172
+ # Footer
173
+ {
174
+ type: 'FOOTER',
175
+ text: 'Reply STOP to unsubscribe'
176
+ },
177
+ # Multiple buttons
178
+ {
179
+ type: 'BUTTONS',
180
+ buttons: [
181
+ {
182
+ type: 'URL',
183
+ text: 'View Order',
184
+ url: 'https://example.com/orders/{{1}}'
185
+ },
186
+ {
187
+ type: 'PHONE_NUMBER',
188
+ text: 'Call Store',
189
+ phone_number: '+1234567890'
190
+ },
191
+ {
192
+ type: 'QUICK_REPLY',
193
+ text: 'Reschedule'
194
+ }
195
+ ]
196
+ }
197
+ ]
198
+
199
+ response = client.templates.create(
200
+ business_account_id: business_account_id,
201
+ name: 'ruby_sdk_complex',
202
+ language: 'en_US',
203
+ category: 'UTILITY',
204
+ components: components
205
+ )
206
+
207
+ puts "Complex template created!"
208
+ puts "Template ID: #{response.id}"
209
+
210
+ rescue KapsoClientRuby::Errors::GraphApiError => e
211
+ puts "Error creating complex template: #{e.message}"
212
+ end
213
+
214
+ # Example 6: Send Template Messages
215
+ puts "\n--- Send Template Messages ---"
216
+
217
+ begin
218
+ # Send simple template without variables
219
+ response1 = client.messages.send_template(
220
+ phone_number_id: ENV['PHONE_NUMBER_ID'],
221
+ to: '+1234567890',
222
+ name: 'hello_world', # Meta's sample template
223
+ language: 'en_US'
224
+ )
225
+
226
+ puts "Simple template sent: #{response1.messages.first.id}"
227
+
228
+ # Send template with parameters
229
+ response2 = client.messages.send_template(
230
+ phone_number_id: ENV['PHONE_NUMBER_ID'],
231
+ to: '+1234567890',
232
+ name: 'ruby_sdk_promo', # Our created template
233
+ language: 'en_US',
234
+ components: [
235
+ {
236
+ type: 'header',
237
+ parameters: [
238
+ { type: 'text', text: 'John Doe' }
239
+ ]
240
+ },
241
+ {
242
+ type: 'body',
243
+ parameters: [
244
+ { type: 'text', text: 'John' },
245
+ { type: 'text', text: 'exclusive' },
246
+ { type: 'text', text: 'RUBY20' },
247
+ { type: 'text', text: '20%' }
248
+ ]
249
+ },
250
+ {
251
+ type: 'button',
252
+ sub_type: 'url',
253
+ index: '0',
254
+ parameters: [
255
+ { type: 'text', text: 'RUBY20' }
256
+ ]
257
+ }
258
+ ]
259
+ )
260
+
261
+ puts "Parameterized template sent: #{response2.messages.first.id}"
262
+
263
+ rescue KapsoClientRuby::Errors::GraphApiError => e
264
+ puts "Error sending template: #{e.message}"
265
+
266
+ case e.category
267
+ when :template
268
+ puts "Template error - check template name, language, and parameters"
269
+ when :parameter
270
+ puts "Parameter error - check component parameters format"
271
+ end
272
+ end
273
+
274
+ # Example 7: Template Management Operations
275
+ puts "\n--- Template Management Operations ---"
276
+
277
+ begin
278
+ # Get specific template details
279
+ template_id = 'your_template_id' # Replace with actual template ID
280
+
281
+ template = client.templates.get(
282
+ business_account_id: business_account_id,
283
+ template_id: template_id
284
+ )
285
+
286
+ puts "Template Details:"
287
+ puts "Name: #{template.name}"
288
+ puts "Status: #{template.status}"
289
+ puts "Category: #{template.category}"
290
+ puts "Quality Score: #{template.quality_score_category}"
291
+
292
+ # Update template (if allowed)
293
+ if template.status == 'REJECTED'
294
+ puts "Attempting to update rejected template..."
295
+
296
+ client.templates.update(
297
+ business_account_id: business_account_id,
298
+ template_id: template_id,
299
+ category: 'UTILITY' # Change category if needed
300
+ )
301
+
302
+ puts "Template updated successfully"
303
+ end
304
+
305
+ rescue KapsoClientRuby::Errors::GraphApiError => e
306
+ puts "Template management error: #{e.message}"
307
+ end
308
+
309
+ # Example 8: Delete Templates
310
+ puts "\n--- Delete Templates ---"
311
+
312
+ begin
313
+ # Delete by template ID
314
+ client.templates.delete(
315
+ business_account_id: business_account_id,
316
+ template_id: 'template_id_to_delete'
317
+ )
318
+
319
+ puts "Template deleted by ID"
320
+
321
+ # Delete by name and language
322
+ client.templates.delete(
323
+ business_account_id: business_account_id,
324
+ name: 'ruby_sdk_test',
325
+ language: 'en_US'
326
+ )
327
+
328
+ puts "Template deleted by name"
329
+
330
+ rescue KapsoClientRuby::Errors::GraphApiError => e
331
+ puts "Delete error: #{e.message}"
332
+
333
+ if e.http_status == 404
334
+ puts "Template not found - may already be deleted"
335
+ end
336
+ end
337
+
338
+ # Example 9: Template Validation and Best Practices
339
+ puts "\n--- Template Validation Examples ---"
340
+
341
+ # Good template example
342
+ def create_good_template(client, business_account_id)
343
+ client.templates.create(
344
+ business_account_id: business_account_id,
345
+ name: 'good_template_example',
346
+ language: 'en_US',
347
+ category: 'UTILITY',
348
+ components: [
349
+ {
350
+ type: 'BODY',
351
+ text: 'Your appointment with {{1}} is confirmed for {{2}} at {{3}}.',
352
+ example: {
353
+ body_text: [['Dr. Smith', 'tomorrow', '2:00 PM']]
354
+ }
355
+ },
356
+ {
357
+ type: 'FOOTER',
358
+ text: 'Reply CANCEL to cancel this appointment'
359
+ }
360
+ ]
361
+ )
362
+ end
363
+
364
+ # Bad template example (this will likely be rejected)
365
+ def create_bad_template_example(client, business_account_id)
366
+ begin
367
+ client.templates.create(
368
+ business_account_id: business_account_id,
369
+ name: 'bad_template_example',
370
+ language: 'en_US',
371
+ category: 'MARKETING',
372
+ components: [
373
+ {
374
+ type: 'BODY',
375
+ text: 'URGENT!!! Buy now or MISS OUT!!! Limited time offer!!!'
376
+ # No example provided, excessive caps, promotional language
377
+ }
378
+ ]
379
+ )
380
+ rescue KapsoClientRuby::Errors::GraphApiError => e
381
+ puts "Bad template rejected (expected): #{e.message}"
382
+ end
383
+ end
384
+
385
+ begin
386
+ good_response = create_good_template(client, business_account_id)
387
+ puts "Good template created: #{good_response.id}"
388
+ rescue => e
389
+ puts "Error with good template: #{e.message}"
390
+ end
391
+
392
+ create_bad_template_example(client, business_account_id)
393
+
391
394
  puts "\n=== Template Management Examples Completed ==="