-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_features.rb
More file actions
353 lines (285 loc) · 9.38 KB
/
advanced_features.rb
File metadata and controls
353 lines (285 loc) · 9.38 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
# frozen_string_literal: true
require 'kapso-client-ruby'
require 'dotenv'
Dotenv.load
puts "=== Advanced Features with Kapso Proxy ==="
# Initialize Kapso client
kapso_client = KapsoClientRuby::Client.new(
kapso_api_key: ENV['KAPSO_API_KEY'],
base_url: 'https://app.kapso.ai/api/meta',
debug: true
)
phone_number_id = ENV['PHONE_NUMBER_ID']
# Example 1: Message History and Analytics
puts "\n--- Message History ---"
begin
# Query message history
messages = kapso_client.messages.query(
phone_number_id: phone_number_id,
direction: 'inbound',
since: '2024-01-01T00:00:00Z',
limit: 10
)
puts "Found #{messages.data.length} messages:"
messages.data.each do |message|
puts "- #{message['id']}: #{message['type']} from #{message['from']}"
end
# Get messages by conversation
if messages.data.any? && messages.data.first['conversation_id']
conv_messages = kapso_client.messages.list_by_conversation(
phone_number_id: phone_number_id,
conversation_id: messages.data.first['conversation_id'],
limit: 5
)
puts "\nConversation messages: #{conv_messages.data.length}"
end
rescue KapsoClientRuby::Errors::KapsoProxyRequiredError => e
puts "Kapso Proxy required: #{e.message}"
rescue KapsoClientRuby::Errors::GraphApiError => e
puts "Message history error: #{e.message}"
end
# Example 2: Conversation Management
puts "\n--- Conversation Management ---"
begin
# List active conversations
conversations = kapso_client.conversations.list(
phone_number_id: phone_number_id,
status: 'active',
limit: 10
)
puts "Active conversations: #{conversations.data.length}"
conversations.data.each do |conv|
puts "Conversation #{conv.id}:"
puts " Phone: #{conv.phone_number}"
puts " Status: #{conv.status}"
puts " Last Active: #{conv.last_active_at}"
if conv.kapso
puts " Contact Name: #{conv.kapso['contact_name']}"
puts " Messages Count: #{conv.kapso['messages_count']}"
puts " Last Message: #{conv.kapso['last_message_text']}"
end
end
# Get specific conversation details
if conversations.data.any?
conversation_id = conversations.data.first.id
conv_details = kapso_client.conversations.get(
conversation_id: conversation_id
)
puts "\nDetailed conversation info:"
puts "ID: #{conv_details.id}"
puts "Status: #{conv_details.status}"
puts "Metadata: #{conv_details.metadata}"
# Update conversation status
kapso_client.conversations.update_status(
conversation_id: conversation_id,
status: 'archived'
)
puts "Conversation archived successfully"
# Unarchive it
kapso_client.conversations.unarchive(conversation_id: conversation_id)
puts "Conversation unarchived"
end
rescue KapsoClientRuby::Errors::GraphApiError => e
puts "Conversation management error: #{e.message}"
end
# Example 3: Contact Management
puts "\n--- Contact Management ---"
begin
# List contacts
contacts = kapso_client.contacts.list(
phone_number_id: phone_number_id,
limit: 10
)
puts "Found #{contacts.data.length} contacts:"
contacts.data.each do |contact|
puts "Contact #{contact.wa_id}:"
puts " Phone: #{contact.phone_number}"
puts " Profile Name: #{contact.profile_name}"
puts " Metadata: #{contact.metadata}"
end
# Get specific contact
if contacts.data.any?
wa_id = contacts.data.first.wa_id
contact_details = kapso_client.contacts.get(
phone_number_id: phone_number_id,
wa_id: wa_id
)
puts "\nContact details for #{wa_id}:"
puts "Profile Name: #{contact_details.profile_name}"
# Update contact metadata
kapso_client.contacts.update(
phone_number_id: phone_number_id,
wa_id: wa_id,
metadata: {
tags: ['ruby_sdk', 'test_contact'],
source: 'api_example',
notes: 'Updated via Ruby SDK'
}
)
puts "Contact metadata updated"
# Add tags
kapso_client.contacts.add_tags(
phone_number_id: phone_number_id,
wa_id: wa_id,
tags: ['premium_customer']
)
puts "Tags added to contact"
# Search contacts
search_results = kapso_client.contacts.search(
phone_number_id: phone_number_id,
query: 'john',
search_in: ['profile_name', 'phone_number']
)
puts "Search results: #{search_results.data.length} contacts"
end
rescue KapsoClientRuby::Errors::GraphApiError => e
puts "Contact management error: #{e.message}"
end
# Example 4: Call Management
puts "\n--- Call Management ---"
begin
# List recent calls
calls = kapso_client.calls.list(
phone_number_id: phone_number_id,
direction: 'INBOUND',
limit: 5
)
puts "Recent calls: #{calls.data.length}"
calls.data.each do |call|
puts "Call #{call.id}:"
puts " Direction: #{call.direction}"
puts " Status: #{call.status}"
puts " Duration: #{call.duration_seconds} seconds"
puts " Started: #{call.started_at}"
end
# Initiate a call (example - requires proper setup)
begin
call_response = kapso_client.calls.connect(
phone_number_id: phone_number_id,
to: '+1234567890',
session: {
sdp_type: 'offer',
sdp: 'v=0\r\no=- 123456789 123456789 IN IP4 127.0.0.1\r\n...'
}
)
puts "Call initiated: #{call_response.calls.first['id']}"
rescue KapsoClientRuby::Errors::GraphApiError => e
puts "Call initiation error (expected in example): #{e.message}"
end
rescue KapsoClientRuby::Errors::GraphApiError => e
puts "Call management error: #{e.message}"
end
# Example 5: Advanced Error Handling and Monitoring
puts "\n--- Advanced Error Handling ---"
class WhatsAppMonitor
def initialize(client)
@client = client
@error_counts = Hash.new(0)
@last_errors = []
end
def send_with_monitoring(method, *args, **kwargs)
start_time = Time.now
begin
result = @client.messages.public_send(method, **kwargs)
duration = Time.now - start_time
puts "✓ #{method} succeeded in #{duration.round(2)}s"
result
rescue KapsoClientRuby::Errors::GraphApiError => e
duration = Time.now - start_time
@error_counts[e.category] += 1
@last_errors << {
timestamp: Time.now,
method: method,
error: e,
duration: duration
}
puts "✗ #{method} failed in #{duration.round(2)}s"
puts " Category: #{e.category}"
puts " Message: #{e.message}"
puts " Retry: #{e.retry_hint[:action]}"
# Automatic retry logic
case e.retry_hint[:action]
when :retry
if kwargs[:_retry_count].to_i < 3
retry_count = kwargs[:_retry_count].to_i + 1
puts " Auto-retrying (#{retry_count}/3)..."
sleep(retry_count)
return send_with_monitoring(method, **kwargs.merge(_retry_count: retry_count))
end
when :retry_after
if e.retry_hint[:retry_after_ms] && e.retry_hint[:retry_after_ms] < 30000
delay = e.retry_hint[:retry_after_ms] / 1000.0
puts " Waiting #{delay}s for rate limit..."
sleep(delay)
return send_with_monitoring(method, **kwargs)
end
end
raise
end
end
def print_statistics
puts "\n--- Error Statistics ---"
puts "Total error categories: #{@error_counts.keys.length}"
@error_counts.each do |category, count|
puts " #{category}: #{count} errors"
end
if @last_errors.any?
puts "\nRecent errors:"
@last_errors.last(3).each do |error_info|
puts " #{error_info[:timestamp]}: #{error_info[:method]} -> #{error_info[:error].category}"
end
end
end
end
# Test the monitoring system
monitor = WhatsAppMonitor.new(kapso_client)
# Test various operations with monitoring
test_operations = [
[:send_text, {
phone_number_id: phone_number_id,
to: '+1234567890',
body: 'Test message from monitoring system'
}],
[:send_template, {
phone_number_id: phone_number_id,
to: '+1234567890',
name: 'nonexistent_template',
language: 'en_US'
}],
[:send_image, {
phone_number_id: phone_number_id,
to: '+1234567890',
image: { link: 'https://invalid-url.example/image.jpg' }
}]
]
test_operations.each do |method, kwargs|
begin
monitor.send_with_monitoring(method, **kwargs)
rescue => e
puts "Final error for #{method}: #{e.message}"
end
sleep(1) # Rate limiting prevention
end
monitor.print_statistics
# Example 6: Webhook Signature Verification (helper function)
puts "\n--- Webhook Signature Verification ---"
def verify_webhook_signature(payload, signature, app_secret)
require 'openssl'
# Extract signature from header (format: "sha256=...")
sig_hash = signature.sub('sha256=', '')
# Calculate expected signature
expected_sig = OpenSSL::HMAC.hexdigest('sha256', app_secret, payload)
# Secure comparison
sig_hash == expected_sig
end
# Example webhook payload verification
webhook_payload = '{"object":"whatsapp_business_account","entry":[...]}'
webhook_signature = 'sha256=abcdef123456...' # From X-Hub-Signature-256 header
app_secret = ENV['WHATSAPP_APP_SECRET']
if app_secret
is_valid = verify_webhook_signature(webhook_payload, webhook_signature, app_secret)
puts "Webhook signature valid: #{is_valid}"
else
puts "Set WHATSAPP_APP_SECRET to test webhook verification"
end
puts "\n=== Advanced Features Examples Completed ==="