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.
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 +477 -477
  6. data/README.md +1053 -752
  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 +387 -387
  18. data/examples/rails/models.rb +239 -239
  19. data/examples/rails/notifications_controller.rb +226 -226
  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 +75 -75
  25. data/lib/kapso_client_ruby/rails/generators/templates/env.erb +20 -20
  26. data/lib/kapso_client_ruby/rails/generators/templates/initializer.rb.erb +32 -32
  27. data/lib/kapso_client_ruby/rails/generators/templates/message_service.rb.erb +137 -137
  28. data/lib/kapso_client_ruby/rails/generators/templates/webhook_controller.rb.erb +61 -61
  29. data/lib/kapso_client_ruby/rails/railtie.rb +54 -54
  30. data/lib/kapso_client_ruby/rails/service.rb +188 -188
  31. data/lib/kapso_client_ruby/rails/tasks.rake +166 -166
  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 -74
  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 +12 -3
@@ -1,55 +1,55 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails/railtie'
4
-
5
- module KapsoClientRuby
6
- module Rails
7
- # Rails integration for KapsoClientRuby
8
- class Railtie < ::Rails::Railtie
9
- railtie_name :kapso_client_ruby
10
-
11
- # Add kapso configuration to Rails application config
12
- config.kapso = ActiveSupport::OrderedOptions.new
13
-
14
- # Set default configuration values
15
- config.before_configuration do |app|
16
- app.config.kapso.api_key = nil
17
- app.config.kapso.phone_number_id = nil
18
- app.config.kapso.business_account_id = nil
19
- app.config.kapso.api_host = 'https://graph.facebook.com'
20
- app.config.kapso.api_version = 'v23.0'
21
- app.config.kapso.timeout = 30
22
- app.config.kapso.debug = false
23
- app.config.kapso.logger = nil
24
- app.config.kapso.retry_on_failure = true
25
- app.config.kapso.max_retries = 3
26
- end
27
-
28
- # Initialize Kapso client after Rails application initialization
29
- initializer 'kapso_client_ruby.configure' do |app|
30
- KapsoClientRuby.configure do |config|
31
- config.api_key = app.config.kapso.api_key || ENV['KAPSO_API_KEY']
32
- config.phone_number_id = app.config.kapso.phone_number_id || ENV['KAPSO_PHONE_NUMBER_ID']
33
- config.business_account_id = app.config.kapso.business_account_id || ENV['KAPSO_BUSINESS_ACCOUNT_ID']
34
- config.api_host = app.config.kapso.api_host || ENV.fetch('KAPSO_API_HOST', 'https://graph.facebook.com')
35
- config.api_version = app.config.kapso.api_version || ENV.fetch('KAPSO_API_VERSION', 'v23.0')
36
- config.timeout = app.config.kapso.timeout || ENV.fetch('KAPSO_TIMEOUT', 30).to_i
37
- config.debug = app.config.kapso.debug || ENV.fetch('KAPSO_DEBUG', 'false') == 'true'
38
- config.logger = app.config.kapso.logger || ::Rails.logger
39
- config.retry_on_failure = app.config.kapso.retry_on_failure
40
- config.max_retries = app.config.kapso.max_retries || ENV.fetch('KAPSO_MAX_RETRIES', 3).to_i
41
- end
42
- end
43
-
44
- # Add rake tasks
45
- rake_tasks do
46
- load 'kapso_client_ruby/rails/tasks.rake'
47
- end
48
-
49
- # Add generators
50
- generators do
51
- require 'kapso_client_ruby/rails/generators/install_generator'
52
- end
53
- end
54
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module KapsoClientRuby
6
+ module Rails
7
+ # Rails integration for KapsoClientRuby
8
+ class Railtie < ::Rails::Railtie
9
+ railtie_name :kapso_client_ruby
10
+
11
+ # Add kapso configuration to Rails application config
12
+ config.kapso = ActiveSupport::OrderedOptions.new
13
+
14
+ # Set default configuration values
15
+ config.before_configuration do |app|
16
+ app.config.kapso.api_key = nil
17
+ app.config.kapso.phone_number_id = nil
18
+ app.config.kapso.business_account_id = nil
19
+ app.config.kapso.api_host = 'https://graph.facebook.com'
20
+ app.config.kapso.api_version = 'v23.0'
21
+ app.config.kapso.timeout = 30
22
+ app.config.kapso.debug = false
23
+ app.config.kapso.logger = nil
24
+ app.config.kapso.retry_on_failure = true
25
+ app.config.kapso.max_retries = 3
26
+ end
27
+
28
+ # Initialize Kapso client after Rails application initialization
29
+ initializer 'kapso_client_ruby.configure' do |app|
30
+ KapsoClientRuby.configure do |config|
31
+ config.api_key = app.config.kapso.api_key || ENV['KAPSO_API_KEY']
32
+ config.phone_number_id = app.config.kapso.phone_number_id || ENV['KAPSO_PHONE_NUMBER_ID']
33
+ config.business_account_id = app.config.kapso.business_account_id || ENV['KAPSO_BUSINESS_ACCOUNT_ID']
34
+ config.api_host = app.config.kapso.api_host || ENV.fetch('KAPSO_API_HOST', 'https://graph.facebook.com')
35
+ config.api_version = app.config.kapso.api_version || ENV.fetch('KAPSO_API_VERSION', 'v23.0')
36
+ config.timeout = app.config.kapso.timeout || ENV.fetch('KAPSO_TIMEOUT', 30).to_i
37
+ config.debug = app.config.kapso.debug || ENV.fetch('KAPSO_DEBUG', 'false') == 'true'
38
+ config.logger = app.config.kapso.logger || ::Rails.logger
39
+ config.retry_on_failure = app.config.kapso.retry_on_failure
40
+ config.max_retries = app.config.kapso.max_retries || ENV.fetch('KAPSO_MAX_RETRIES', 3).to_i
41
+ end
42
+ end
43
+
44
+ # Add rake tasks
45
+ rake_tasks do
46
+ load 'kapso_client_ruby/rails/tasks.rake'
47
+ end
48
+
49
+ # Add generators
50
+ generators do
51
+ require 'kapso_client_ruby/rails/generators/install_generator'
52
+ end
53
+ end
54
+ end
55
55
  end
@@ -1,189 +1,189 @@
1
- # frozen_string_literal: true
2
-
3
- module KapsoClientRuby
4
- module Rails
5
- # Service class for Rails integration with KapsoClientRuby
6
- # Provides a convenient interface for Rails applications to interact with the Kapso API
7
- class Service
8
- include ActiveSupport::Configurable
9
-
10
- # @return [KapsoClientRuby::Client] The configured Kapso client
11
- attr_reader :client
12
-
13
- def initialize(client = nil)
14
- @client = client || KapsoClientRuby::Client.new
15
- end
16
-
17
- # Send a text message
18
- # @param to [String] The recipient's phone number
19
- # @param text [String] The message text
20
- # @param options [Hash] Additional options
21
- # @return [Hash] API response
22
- def send_text_message(to:, text:, **options)
23
- Rails.logger.info "Sending text message to #{to}: #{text.truncate(50)}"
24
-
25
- result = client.messages.send_text(
26
- to: to,
27
- text: text,
28
- **options
29
- )
30
-
31
- Rails.logger.info "Message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
32
- result
33
- rescue KapsoClientRuby::Error => e
34
- Rails.logger.error "Failed to send text message: #{e.message}"
35
- raise
36
- end
37
-
38
- # Send a template message
39
- # @param to [String] The recipient's phone number
40
- # @param template_name [String] The template name
41
- # @param language [String] The language code (default: 'en')
42
- # @param components [Array] Template components
43
- # @return [Hash] API response
44
- def send_template_message(to:, template_name:, language: 'en', components: [])
45
- Rails.logger.info "Sending template message '#{template_name}' to #{to}"
46
-
47
- result = client.messages.send_template(
48
- to: to,
49
- name: template_name,
50
- language: language,
51
- components: components
52
- )
53
-
54
- Rails.logger.info "Template message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
55
- result
56
- rescue KapsoClientRuby::Error => e
57
- Rails.logger.error "Failed to send template message: #{e.message}"
58
- raise
59
- end
60
-
61
- # Send a media message
62
- # @param to [String] The recipient's phone number
63
- # @param media_type [String] Type of media ('image', 'document', 'video', 'audio')
64
- # @param media_url [String] URL of the media file
65
- # @param options [Hash] Additional options
66
- # @return [Hash] API response
67
- def send_media_message(to:, media_type:, media_url:, **options)
68
- Rails.logger.info "Sending #{media_type} message to #{to}: #{media_url}"
69
-
70
- result = client.messages.send_media(
71
- to: to,
72
- type: media_type,
73
- media_url: media_url,
74
- **options
75
- )
76
-
77
- Rails.logger.info "Media message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
78
- result
79
- rescue KapsoClientRuby::Error => e
80
- Rails.logger.error "Failed to send media message: #{e.message}"
81
- raise
82
- end
83
-
84
- # Upload media file
85
- # @param file_path [String] Path to the media file
86
- # @param media_type [String] Type of media
87
- # @return [Hash] Upload response with media ID
88
- def upload_media(file_path:, media_type:)
89
- Rails.logger.info "Uploading media file: #{file_path}"
90
-
91
- result = client.media.upload(
92
- file_path: file_path,
93
- type: media_type
94
- )
95
-
96
- Rails.logger.info "Media uploaded successfully. ID: #{result['id']}"
97
- result
98
- rescue KapsoClientRuby::Error => e
99
- Rails.logger.error "Failed to upload media: #{e.message}"
100
- raise
101
- end
102
-
103
- # Get message status
104
- # @param message_id [String] The message ID
105
- # @return [Hash] Message status
106
- def get_message_status(message_id)
107
- Rails.logger.debug "Getting status for message: #{message_id}"
108
-
109
- result = client.messages.get_status(message_id)
110
-
111
- Rails.logger.debug "Message status: #{result['status']}"
112
- result
113
- rescue KapsoClientRuby::Error => e
114
- Rails.logger.error "Failed to get message status: #{e.message}"
115
- raise
116
- end
117
-
118
- # Get templates
119
- # @param options [Hash] Query options
120
- # @return [Array] List of templates
121
- def get_templates(**options)
122
- Rails.logger.debug "Fetching templates with options: #{options}"
123
-
124
- result = client.templates.list(**options)
125
-
126
- Rails.logger.debug "Found #{result['data']&.length || 0} templates"
127
- result
128
- rescue KapsoClientRuby::Error => e
129
- Rails.logger.error "Failed to fetch templates: #{e.message}"
130
- raise
131
- end
132
-
133
- # Process incoming webhook
134
- # @param webhook_data [Hash] The webhook payload
135
- # @return [Hash] Processed webhook data
136
- def process_webhook(webhook_data)
137
- Rails.logger.info "Processing webhook: #{webhook_data}"
138
-
139
- # Process webhook data based on type
140
- entries = webhook_data['entry'] || []
141
-
142
- entries.each do |entry|
143
- changes = entry['changes'] || []
144
-
145
- changes.each do |change|
146
- case change['field']
147
- when 'messages'
148
- process_message_webhook(change['value'])
149
- when 'message_template_status_update'
150
- process_template_status_webhook(change['value'])
151
- else
152
- Rails.logger.warn "Unknown webhook field: #{change['field']}"
153
- end
154
- end
155
- end
156
-
157
- { status: 'processed' }
158
- rescue => e
159
- Rails.logger.error "Failed to process webhook: #{e.message}"
160
- raise
161
- end
162
-
163
- private
164
-
165
- def process_message_webhook(value)
166
- messages = value['messages'] || []
167
- statuses = value['statuses'] || []
168
-
169
- messages.each do |message|
170
- Rails.logger.info "Received message: #{message['id']} from #{message['from']}"
171
- # Trigger Rails callback or event
172
- ActiveSupport::Notifications.instrument('kapso.message_received', message: message)
173
- end
174
-
175
- statuses.each do |status|
176
- Rails.logger.info "Message status update: #{status['id']} -> #{status['status']}"
177
- # Trigger Rails callback or event
178
- ActiveSupport::Notifications.instrument('kapso.message_status_updated', status: status)
179
- end
180
- end
181
-
182
- def process_template_status_webhook(value)
183
- Rails.logger.info "Template status update: #{value}"
184
- # Trigger Rails callback or event
185
- ActiveSupport::Notifications.instrument('kapso.template_status_updated', template_status: value)
186
- end
187
- end
188
- end
1
+ # frozen_string_literal: true
2
+
3
+ module KapsoClientRuby
4
+ module Rails
5
+ # Service class for Rails integration with KapsoClientRuby
6
+ # Provides a convenient interface for Rails applications to interact with the Kapso API
7
+ class Service
8
+ include ActiveSupport::Configurable
9
+
10
+ # @return [KapsoClientRuby::Client] The configured Kapso client
11
+ attr_reader :client
12
+
13
+ def initialize(client = nil)
14
+ @client = client || KapsoClientRuby::Client.new
15
+ end
16
+
17
+ # Send a text message
18
+ # @param to [String] The recipient's phone number
19
+ # @param text [String] The message text
20
+ # @param options [Hash] Additional options
21
+ # @return [Hash] API response
22
+ def send_text_message(to:, text:, **options)
23
+ Rails.logger.info "Sending text message to #{to}: #{text.truncate(50)}"
24
+
25
+ result = client.messages.send_text(
26
+ to: to,
27
+ text: text,
28
+ **options
29
+ )
30
+
31
+ Rails.logger.info "Message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
32
+ result
33
+ rescue KapsoClientRuby::Error => e
34
+ Rails.logger.error "Failed to send text message: #{e.message}"
35
+ raise
36
+ end
37
+
38
+ # Send a template message
39
+ # @param to [String] The recipient's phone number
40
+ # @param template_name [String] The template name
41
+ # @param language [String] The language code (default: 'en')
42
+ # @param components [Array] Template components
43
+ # @return [Hash] API response
44
+ def send_template_message(to:, template_name:, language: 'en', components: [])
45
+ Rails.logger.info "Sending template message '#{template_name}' to #{to}"
46
+
47
+ result = client.messages.send_template(
48
+ to: to,
49
+ name: template_name,
50
+ language: language,
51
+ components: components
52
+ )
53
+
54
+ Rails.logger.info "Template message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
55
+ result
56
+ rescue KapsoClientRuby::Error => e
57
+ Rails.logger.error "Failed to send template message: #{e.message}"
58
+ raise
59
+ end
60
+
61
+ # Send a media message
62
+ # @param to [String] The recipient's phone number
63
+ # @param media_type [String] Type of media ('image', 'document', 'video', 'audio')
64
+ # @param media_url [String] URL of the media file
65
+ # @param options [Hash] Additional options
66
+ # @return [Hash] API response
67
+ def send_media_message(to:, media_type:, media_url:, **options)
68
+ Rails.logger.info "Sending #{media_type} message to #{to}: #{media_url}"
69
+
70
+ result = client.messages.send_media(
71
+ to: to,
72
+ type: media_type,
73
+ media_url: media_url,
74
+ **options
75
+ )
76
+
77
+ Rails.logger.info "Media message sent successfully. ID: #{result.dig('messages', 0, 'id')}"
78
+ result
79
+ rescue KapsoClientRuby::Error => e
80
+ Rails.logger.error "Failed to send media message: #{e.message}"
81
+ raise
82
+ end
83
+
84
+ # Upload media file
85
+ # @param file_path [String] Path to the media file
86
+ # @param media_type [String] Type of media
87
+ # @return [Hash] Upload response with media ID
88
+ def upload_media(file_path:, media_type:)
89
+ Rails.logger.info "Uploading media file: #{file_path}"
90
+
91
+ result = client.media.upload(
92
+ file_path: file_path,
93
+ type: media_type
94
+ )
95
+
96
+ Rails.logger.info "Media uploaded successfully. ID: #{result['id']}"
97
+ result
98
+ rescue KapsoClientRuby::Error => e
99
+ Rails.logger.error "Failed to upload media: #{e.message}"
100
+ raise
101
+ end
102
+
103
+ # Get message status
104
+ # @param message_id [String] The message ID
105
+ # @return [Hash] Message status
106
+ def get_message_status(message_id)
107
+ Rails.logger.debug "Getting status for message: #{message_id}"
108
+
109
+ result = client.messages.get_status(message_id)
110
+
111
+ Rails.logger.debug "Message status: #{result['status']}"
112
+ result
113
+ rescue KapsoClientRuby::Error => e
114
+ Rails.logger.error "Failed to get message status: #{e.message}"
115
+ raise
116
+ end
117
+
118
+ # Get templates
119
+ # @param options [Hash] Query options
120
+ # @return [Array] List of templates
121
+ def get_templates(**options)
122
+ Rails.logger.debug "Fetching templates with options: #{options}"
123
+
124
+ result = client.templates.list(**options)
125
+
126
+ Rails.logger.debug "Found #{result['data']&.length || 0} templates"
127
+ result
128
+ rescue KapsoClientRuby::Error => e
129
+ Rails.logger.error "Failed to fetch templates: #{e.message}"
130
+ raise
131
+ end
132
+
133
+ # Process incoming webhook
134
+ # @param webhook_data [Hash] The webhook payload
135
+ # @return [Hash] Processed webhook data
136
+ def process_webhook(webhook_data)
137
+ Rails.logger.info "Processing webhook: #{webhook_data}"
138
+
139
+ # Process webhook data based on type
140
+ entries = webhook_data['entry'] || []
141
+
142
+ entries.each do |entry|
143
+ changes = entry['changes'] || []
144
+
145
+ changes.each do |change|
146
+ case change['field']
147
+ when 'messages'
148
+ process_message_webhook(change['value'])
149
+ when 'message_template_status_update'
150
+ process_template_status_webhook(change['value'])
151
+ else
152
+ Rails.logger.warn "Unknown webhook field: #{change['field']}"
153
+ end
154
+ end
155
+ end
156
+
157
+ { status: 'processed' }
158
+ rescue => e
159
+ Rails.logger.error "Failed to process webhook: #{e.message}"
160
+ raise
161
+ end
162
+
163
+ private
164
+
165
+ def process_message_webhook(value)
166
+ messages = value['messages'] || []
167
+ statuses = value['statuses'] || []
168
+
169
+ messages.each do |message|
170
+ Rails.logger.info "Received message: #{message['id']} from #{message['from']}"
171
+ # Trigger Rails callback or event
172
+ ActiveSupport::Notifications.instrument('kapso.message_received', message: message)
173
+ end
174
+
175
+ statuses.each do |status|
176
+ Rails.logger.info "Message status update: #{status['id']} -> #{status['status']}"
177
+ # Trigger Rails callback or event
178
+ ActiveSupport::Notifications.instrument('kapso.message_status_updated', status: status)
179
+ end
180
+ end
181
+
182
+ def process_template_status_webhook(value)
183
+ Rails.logger.info "Template status update: #{value}"
184
+ # Trigger Rails callback or event
185
+ ActiveSupport::Notifications.instrument('kapso.template_status_updated', template_status: value)
186
+ end
187
+ end
188
+ end
189
189
  end