whatsapp_sdk 0.13.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.cursorrules +53 -0
  3. data/CHANGELOG.md +10 -0
  4. data/Gemfile +1 -0
  5. data/Gemfile.lock +9 -4
  6. data/README.md +115 -170
  7. data/example.rb +197 -113
  8. data/lib/whatsapp_sdk/api/business_profile.rb +12 -14
  9. data/lib/whatsapp_sdk/api/client.rb +26 -2
  10. data/lib/whatsapp_sdk/api/medias.rb +23 -31
  11. data/lib/whatsapp_sdk/api/messages.rb +24 -61
  12. data/lib/whatsapp_sdk/api/phone_numbers.rb +32 -22
  13. data/lib/whatsapp_sdk/api/request.rb +2 -0
  14. data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +4 -4
  15. data/lib/whatsapp_sdk/api/responses/http_response_error.rb +18 -0
  16. data/lib/whatsapp_sdk/api/responses/{message_error_response.rb → id_response.rb} +6 -3
  17. data/lib/whatsapp_sdk/api/responses/message_data_response.rb +17 -20
  18. data/lib/whatsapp_sdk/api/responses/pagination_records.rb +17 -0
  19. data/lib/whatsapp_sdk/api/responses/success_response.rb +3 -16
  20. data/lib/whatsapp_sdk/api/templates.rb +27 -36
  21. data/lib/whatsapp_sdk/resource/business_profile.rb +28 -0
  22. data/lib/whatsapp_sdk/resource/errors.rb +2 -0
  23. data/lib/whatsapp_sdk/resource/media.rb +18 -68
  24. data/lib/whatsapp_sdk/resource/media_component.rb +89 -0
  25. data/lib/whatsapp_sdk/resource/message_template_namespace.rb +17 -0
  26. data/lib/whatsapp_sdk/resource/phone_number.rb +41 -11
  27. data/lib/whatsapp_sdk/resource/phone_number_component.rb +25 -0
  28. data/lib/whatsapp_sdk/resource/template.rb +20 -0
  29. data/lib/whatsapp_sdk/version.rb +1 -1
  30. data/whatsapp_sdk.gemspec +1 -1
  31. metadata +10 -15
  32. data/lib/whatsapp_sdk/api/response.rb +0 -32
  33. data/lib/whatsapp_sdk/api/responses/business_profile_data_response.rb +0 -33
  34. data/lib/whatsapp_sdk/api/responses/data_response.rb +0 -17
  35. data/lib/whatsapp_sdk/api/responses/error_response.rb +0 -25
  36. data/lib/whatsapp_sdk/api/responses/media_data_response.rb +0 -30
  37. data/lib/whatsapp_sdk/api/responses/message_template_namespace_data_response.rb +0 -27
  38. data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +0 -42
  39. data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +0 -32
  40. data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +0 -29
  41. data/lib/whatsapp_sdk/api/responses/template_data_response.rb +0 -46
  42. data/lib/whatsapp_sdk/api/responses/templates_data_response.rb +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c730759fac4bf2321be036813acb1708c92c9476fc4af1b330a81a249ef8f1d9
4
- data.tar.gz: 71a10b4c8449b2e2e240ad25be6352e19cf91870c6caa68b84849bbf6129a997
3
+ metadata.gz: b916f88ff7d75987b2173c55eb05efa2761951667f5d09daa2d6febba5e2ad63
4
+ data.tar.gz: 9c1974df055bdab421285fb4095f205b4713a22ca5363841737157c3f5ddf40a
5
5
  SHA512:
6
- metadata.gz: 2b0b1e93dd3370eb032c7f07fa14a2fc0c6561f96c091e862fb11f8c1fb0d8d4bcd008a5ffcef0b7abcc46b73262e86e52e1ef68bceadd9dd01066ed18b122b5
7
- data.tar.gz: 9e028ba82392db6092a4c9f7d831df4f7f5ce4912ee916938d8c88fbad86a4d8f2471577f6c04cfa0ed9311953843d00a4f0b7148142fdc04e8c2770a09d0f68
6
+ metadata.gz: 74f451bf6122b298a40aebae35806e49ad630ed1a7e54ca021b2179c3339361366190594dfe5327a575964203a49c0064cf4f0d14a6772a2b70ff0e9a1ea1d78
7
+ data.tar.gz: 28766015d65c3e7bac47cbc50bef0dd2e509bb326d20d360ed3e2cb9993c60adebe3059af882f8b9c01519485a868db7c7ca3ca2590e7e44cae3ae7b3a6e7eb6
data/.cursorrules ADDED
@@ -0,0 +1,53 @@
1
+ You are an expert Ruby developer with experience using the Whatsapp Cloud API.
2
+
3
+ # WhatsApp Cloud API Ruby SDK Development Guidelines
4
+
5
+ ## Language and Frameworks used
6
+ language: ruby
7
+ Test: minitest
8
+ Linter: rubocop
9
+
10
+ ## File Structure
11
+ source_files: `lib/**/*.rb`
12
+ test_files: `test/**/*_test.rb`
13
+ example_files: `example.rb`
14
+
15
+ ## Dependencies
16
+ gemfile: Gemfile
17
+
18
+ ## Documentation
19
+ docs:
20
+ - README.md
21
+ - CONTRIBUTING.MD
22
+ - CHANGELOG.MD
23
+
24
+ # Main API classes (lib/whatsapp_sdk/api/)
25
+ api_classes:
26
+ - Messages
27
+ - Medias
28
+ - PhoneNumbers
29
+ - BusinessProfile
30
+ - Templates
31
+
32
+ ## Code Style and Structure 
33
+ - Write concise, idiomatic Ruby code with accurate examples. 
34
+ - Follow Ruby conventions and best practices. 
35
+ - Use object-oriented and functional programming patterns as appropriate. 
36
+ - Follow the Ruby Style Guide (https://rubystyle.guide/) 
37
+
38
+
39
+ ## Code Style and Structure
40
+ - Write concise, idiomatic Ruby code with accurate examples
41
+ - Follow Ruby conventions and best practices
42
+ - Use object-oriented and functional programming patterns as appropriate
43
+ - Adhere to the Ruby Style Guide (https://rubystyle.guide/)
44
+ - Implement proper error logging and user-friendly messages
45
+
46
+ ## Development Guidelines
47
+ 1. Ensure comprehensive test coverage for all API classes
48
+ 2. Document public methods and classes using YARD syntax
49
+ 3. Keep the SDK modular and extensible for future WhatsApp API updates
50
+ 4. Implement robust error handling and provide clear error messages
51
+ 5. Use semantic versioning for releases
52
+ 6. Maintain backwards compatibility when possible
53
+
data/CHANGELOG.md CHANGED
@@ -1,4 +1,14 @@
1
1
  # Unreleased
2
+
3
+ # v 1.0.0
4
+ - Implement a Better response API [159](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/156)
5
+ - Implement a Better API for calling the Cloud API [156](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/156)
6
+ - Used [VCR gem]() for tests @igacio-chiazzo [153](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/153)
7
+ - Added support for API v20.0 and v21.0 @guizaols [152](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/152)
8
+ - Increased minimum required Ruby version to 2.7.0 @ignacio-chiazzo [151](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/151)
9
+
10
+ # v 0.13.0
11
+
2
12
  - Remove sorbet dependency @ignacio-chiazzo [148](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/148)
3
13
  - Added the ability to specify the `filename` [via the document](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages) API. @sahilas [#137](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/137)
4
14
 
data/Gemfile CHANGED
@@ -10,6 +10,7 @@ group(:test) do
10
10
  gem('rubocop', require: false)
11
11
  gem('rubocop-minitest', require: false)
12
12
  gem('rubocop-performance', require: false)
13
+ gem('vcr')
13
14
  gem('webmock')
14
15
  end
15
16
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- whatsapp_sdk (0.13.0)
4
+ whatsapp_sdk (1.0.0)
5
5
  faraday (~> 2)
6
6
  faraday-multipart (~> 1)
7
7
  zeitwerk (~> 2)
@@ -27,7 +27,7 @@ GEM
27
27
  method_source (1.0.0)
28
28
  minitest (5.16.1)
29
29
  mocha (1.14.0)
30
- multipart-post (2.4.0)
30
+ multipart-post (2.4.1)
31
31
  parallel (1.22.1)
32
32
  parser (3.1.2.0)
33
33
  ast (~> 2.4.1)
@@ -40,7 +40,8 @@ GEM
40
40
  rainbow (3.1.1)
41
41
  rake (12.3.3)
42
42
  regexp_parser (2.5.0)
43
- rexml (3.2.5)
43
+ rexml (3.3.6)
44
+ strscan
44
45
  rubocop (1.30.1)
45
46
  parallel (~> 1.10)
46
47
  parser (>= 3.1.0.0)
@@ -59,12 +60,15 @@ GEM
59
60
  rubocop-ast (>= 0.4.0)
60
61
  ruby-progressbar (1.11.0)
61
62
  ruby2_keywords (0.0.5)
63
+ strscan (3.1.0)
62
64
  unicode-display_width (2.2.0)
65
+ vcr (6.3.1)
66
+ base64
63
67
  webmock (3.18.1)
64
68
  addressable (>= 2.8.0)
65
69
  crack (>= 0.3.2)
66
70
  hashdiff (>= 0.4.0, < 2.0.0)
67
- zeitwerk (2.6.13)
71
+ zeitwerk (2.6.18)
68
72
 
69
73
  PLATFORMS
70
74
  arm64-darwin-21
@@ -82,6 +86,7 @@ DEPENDENCIES
82
86
  rubocop
83
87
  rubocop-minitest
84
88
  rubocop-performance
89
+ vcr
85
90
  webmock
86
91
  whatsapp_sdk!
87
92
 
data/README.md CHANGED
@@ -35,11 +35,7 @@ There are three primary resources, `Messages`, `Media` and `PhoneNumbers`. `Mess
35
35
 
36
36
  To use `Messages`, `Media` or `PhoneNumbers`, you need to initialize the `Client` that contains auth information. There are two ways to do it.
37
37
 
38
- 1. Use an initializer
39
-
40
- Note:
41
- Optionally, you can specify the desired API version to use (defaults to the latest version if omitted).
42
- Available API version can be found [here](https://developers.facebook.com/docs/graph-api/changelog/versions).
38
+ #### Option 1) Use an initializer
43
39
 
44
40
  ```ruby
45
41
  # config/initializers/whatsapp_sdk.rb
@@ -50,23 +46,25 @@ WhatsappSdk.configure do |config|
50
46
  config.logger_options = { bodies: true } # optional, they are all valid logger_options for Faraday
51
47
  end
52
48
  ```
53
- More Details on Faraday Logger Options are [here](https://lostisland.github.io/faraday/#/middleware/included/logging?id=logging).
54
49
 
55
- OR 2) Create a `Client` instance and pass it to the `Messages`, `Medias` or `PhoneNumbers` instance like this:
50
+ Notes:
51
+ - Optionally, you can specify the desired API version to use (defaults to the latest version if omitted).
52
+ Available API version can be found [here](https://developers.facebook.com/docs/graph-api/changelog/versions).
53
+ - You can attach a logger. More Details on Faraday Logger Options are [here](https://lostisland.github.io/faraday/#/middleware/included/logging?id=logging).
54
+
55
+
56
+ #### Option 2) Create a `Client` instance :
56
57
 
57
- **Without Logger:**
58
58
  ```ruby
59
+
60
+ # without
59
61
  client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
60
- messages_api = WhatsappSdk::Api::Messages.new(client)
61
- ```
62
- **With Logger:**
63
- ```ruby
62
+
63
+ # OR optionally use a logger, api_version and
64
64
  logger = Logger.new(STDOUT)
65
65
  logger_options = { bodies: true }
66
- client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>", "<API VERSION>", logger, logger_options) # replace this with a valid access token
67
- messages_api = WhatsappSdk::Api::Messages.new(client)
66
+ client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>", "<API VERSION>", logger, logger_options)
68
67
  ```
69
- Each API operation returns a `WhatsappSdk::Api::Response` that contains `data` and `error` and a couple of helpful functions such as `ok?` and `error?`. There are three types of responses `WhatsappSdk::Api::MessageDataResponse`, `WhatsappSdk::Api::PhoneNumberDataResponse` and `WhatsappSdk::Api::PhoneNumbersDataResponse`. Each of them contains different attributes.
70
68
 
71
69
  ## Set up a Meta app
72
70
 
@@ -119,15 +117,13 @@ end
119
117
  Phone Numbers API
120
118
 
121
119
  ```ruby
122
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
123
- registered_number = phone_numbers_api.registered_number(SENDER_ID)
120
+ registered_number = client.phone_numbers.registered_number(SENDER_ID)
124
121
  ```
125
122
 
126
123
  Messages API
127
124
 
128
125
  ```ruby
129
- messages_api = WhatsappSdk::Api::Messages.new
130
- message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
126
+ message_sent = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
131
127
  message: "Hey there! it's Whatsapp Ruby SDK")
132
128
  ```
133
129
 
@@ -135,19 +131,26 @@ Check the [example.rb file](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk
135
131
 
136
132
  </details>
137
133
 
138
- ## Operations
139
-
140
- First, create the client and then create an instance `WhatsappSdk::Api::Messages` that requires a client as a param like this:
134
+ ## Sneak Peek
141
135
 
142
136
  ```ruby
143
- messages_api = WhatsappSdk::Api::Messages.new
144
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
145
- medias_api = WhatsappSdk::Api::Medias.new
146
- business_profile_api = WhatsappSdk::Api::BusinessProfile.new
147
- templates_api = WhatsappSdk::Api::Templates.new
148
- ```
137
+ client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
138
+
139
+ client.phone_numbers.register_number(SENDER_ID, 123456) # register the phone number to uplaod media and send message from.
140
+
141
+ # send a text and a location
142
+ client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,message: "Hey there! it's Whatsapp Ruby SDK")
149
143
 
150
- Note: Remember to initialize the client first!
144
+ client.messages.send_location(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house")
145
+
146
+ # upload a photo and send it
147
+ image = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")image = client.media.get(media_id: uploaded_media.id)
148
+ client.messages.send_image(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: image.id)
149
+
150
+ # upload a sticker and send it
151
+ sticker = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/sticker.webp", type: "image/webp")
152
+ client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id)
153
+ ```
151
154
 
152
155
  ## APIs
153
156
 
@@ -157,64 +160,52 @@ Note: Remember to initialize the client first!
157
160
 
158
161
  ```ruby
159
162
  # Get list of templates
160
- templates_api.templates(business_id: BUSINESS_ID)
163
+ client.templates.list(business_id: BUSINESS_ID)
164
+
165
+ # Get Message Template Namespace
166
+ # The message template namespace is required to send messages using the message templates.
167
+ client.templates.get_message_template_namespace(business_id: BUSINESS_ID)
161
168
 
162
169
  # Create a template
163
- new_template = templates_api.create(
170
+ client.templates.create(
164
171
  business_id: BUSINESS_ID, name: "seasonal_promotion", language: "en_US", category: "MARKETING",
165
172
  components_json: components_json, allow_category_change: true
166
173
  )
167
174
 
168
175
  # Delete a template
169
- templates_api.delete(business_id: BUSINESS_ID, name: "my_name") # delete by name
176
+ client.templates.delete(business_id: BUSINESS_ID, name: "my_name") # delete by name
170
177
  ```
171
-
172
178
  </details>
173
179
 
174
180
  ### Business Profile API
175
181
 
176
182
  <details>
177
183
 
178
- Get the details of your business
179
-
180
184
  ```ruby
181
- business_profile = business_profile_api.details(123456)
182
- ```
183
-
184
- Update the details of your business
185
+ # Get the details of your business
186
+ client.business_profiles.get(phone_number_id)
185
187
 
186
- ```ruby
187
- business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
188
+ # Update the details of your business
189
+ client.business_profiles.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
188
190
  ```
189
-
190
191
  </details>
191
192
 
192
193
  ### Phone numbers API
193
194
 
194
195
  <details>
195
196
 
196
- Get the list of phone numbers registered
197
-
198
197
  ```ruby
199
- phone_numbers_api.registered_numbers(123456) # accepts a business_id
200
- ```
201
-
202
- Get the a phone number by id
198
+ # Get the list of phone numbers registered
199
+ client.phone_numbers.list(business_id)
203
200
 
204
- ```ruby
205
- phone_numbers_api.registered_numbers(123456) # accepts a phone_number_id
206
- ```
207
-
208
- Register a phone number
209
-
210
- ```ruby
211
- phone_numbers_api.register_number(phone_number_id, pin)
212
- ```
201
+ # Get the a phone number by id
202
+ client.phone_numbers.get(phone_number_id)
213
203
 
214
- Deregister a phone number
204
+ # Register a phone number
205
+ client.phone_numbers.register_number(phone_number_id, pin)
215
206
 
216
- ```ruby
217
- phone_numbers_api.deregister_number(phone_number_id)
207
+ # Deregister a phone number
208
+ client.phone_numbers.deregister_number(phone_number_id)
218
209
  ```
219
210
 
220
211
  </details>
@@ -223,159 +214,107 @@ phone_numbers_api.deregister_number(phone_number_id)
223
214
 
224
215
  <details>
225
216
 
226
- Upload a media
227
-
228
217
  ```ruby
229
- medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
230
- ```
218
+ # Upload a media
219
+ client.media.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
231
220
 
232
- Get a media
233
-
234
- ```ruby
235
- media = medias_api.media(media_id: MEDIA_ID)
236
- ```
221
+ # Get a media
222
+ media = client.media.get(media_id: MEDIA_ID)
237
223
 
238
- Download media
224
+ # Download media
225
+ client.media.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png', media_type: "image/png")
239
226
 
240
- ```ruby
241
- medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png', media_type: "image/png")
242
- ```
243
-
244
- Delete a media
245
-
246
- ```ruby
247
- medias_api.delete(media_id: MEDIA_ID)
227
+ # Delete a media
228
+ client.media.delete(media_id: MEDIA_ID)
248
229
  ```
249
-
250
230
  </details>
251
231
 
252
232
  ### Messages API
253
233
 
254
234
  <details>
255
235
 
256
- **Send a text message**
257
-
258
- ```ruby
259
- messages_api.send_text(sender_id: 1234, recipient_number: 112345678, message: "hola")
260
- ```
261
-
262
- **Read a message**
263
-
264
- ```ruby
265
- messages_api.read_message(sender_id: 1234, message_id: "wamid.HBgLMTM0M12345678910=")
266
- ```
267
-
268
- Note: To get the `message_id` you can set up [Webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) that will listen and fire an event when a message is received.
269
-
270
- **Send a reaction to message**
271
- To send a reaction to a message, you need to obtain the message id and look for the emoji's unicode you want to use.
272
-
273
236
  ```ruby
274
- messages_api.send_reaction(sender_id: 123_123, recipient_number: 56_789, message_id: "12345", emoji: "\u{1f550}")
237
+ # Send a text message
238
+ client.messages.send_text(sender_id: 1234, recipient_number: 112345678, message: "hola")
275
239
 
276
- messages_api.send_reaction(sender_id: 123_123, recipient_number: 56_789, message_id: "12345", emoji: "⛄️")
277
- ```
240
+ # Read a message
241
+ client.messages.read_message(sender_id: 1234, message_id: "wamid.HBgLMTM0M12345678910=")
278
242
 
279
- **Reply to a message**
280
- To reply to a message, just include the id of the message in the `messages_api` methods. For example, to reply to a text message include the following:
243
+ # Note: To get the `message_id` you can set up [Webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) that will listen and fire an event when a message is received.
281
244
 
282
- ```ruby
283
- messages_api.send_text(sender_id: 123_123, recipient_number: 56_789, message: "I'm a reply", message_id: "wamid.1234")
284
- ```
245
+ # Send a reaction to message
246
+ # To send a reaction to a message, you need to obtain the message id and look for the emoji's unicode you want to use.
247
+ client.messages.send_reaction(sender_id: 123_123, recipient_number: 56_789, message_id: "12345", emoji: "\u{1f550}")
248
+ client.messages.send_reaction(sender_id: 123_123, recipient_number: 56_789, message_id: "12345", emoji: "⛄️")
285
249
 
286
- **Send a location message**
250
+ # Reply to a message
251
+ # To reply to a message, just include the id of the message in the `client.messages` methods. For example, to reply to a text message include the following:
252
+ client.messages.send_text(sender_id: 123_123, recipient_number: 56_789, message: "I'm a reply", message_id: "wamid.1234")
287
253
 
288
- ```ruby
289
- messages_api.send_location(
254
+ # Send a location message
255
+ client.messages.send_location(
290
256
  sender_id: 123123, recipient_number: 56789,
291
257
  longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
292
258
  )
293
- ```
294
259
 
295
- **Send an image message**
296
- It could use a link or an image_id.
297
-
298
- ```ruby
299
- # with a link
300
- messages_api.send_image(
301
- sender_id: 123123, recipient_number: 56789, link: "image_link", caption: "Ignacio Chiazzo Profile"
302
- )
260
+ # Send an image message
261
+ # It uses a link or an image_id.
262
+ # with a link
263
+ client.messages.send_image(sender_id: 123123, recipient_number: 56789, link: "image_link", caption: "Ignacio Chiazzo Profile")
303
264
 
304
265
  # with an image id
305
- messages_api.send_image(
306
- sender_id: 123123, recipient_number: 56789, image_id: "1234", caption: "Ignacio Chiazzo Profile"
307
- )
308
- ```
266
+ client.messages.send_image(sender_id: 123123, recipient_number: 56789, image_id: "1234", caption: "Ignacio Chiazzo Profile")
309
267
 
310
- **Send an audio message**
311
- It could use a link or an audio_id.
312
268
 
313
- ```ruby
269
+ # Send an audio message
270
+ # It uses a link or an audio_id.
314
271
  # with a link
315
- messages_api.send_audio(sender_id: 123123, recipient_number: 56789, link: "audio_link")
272
+ client.messages.send_audio(sender_id: 123123, recipient_number: 56789, link: "audio_link")
316
273
 
317
274
  # with an audio id
318
- messages_api.send_audio(sender_id: 123123, recipient_number: 56789, audio_id: "1234")
319
- ```
320
-
321
- **Send a document message**
322
- It could use a link or a document_id.
275
+ client.messages.send_audio(sender_id: 123123, recipient_number: 56789, audio_id: "1234")
323
276
 
324
- ```ruby
277
+ # Send a document message
278
+ # It uses a link or a document_id.
325
279
  # with a link
326
- messages_api.send_document(
327
- sender_id: 123123, recipient_number: 56789, link: "document_link", caption: "Ignacio Chiazzo"
328
- )
280
+ client.messages.send_document(sender_id: 123123, recipient_number: 56789, link: "document_link", caption: "Ignacio Chiazzo")
329
281
 
330
282
  # with a document id
331
- messages_api.send_document(
332
- sender_id: 123123, recipient_number: 56789, document_id: "1234", caption: "Ignacio Chiazzo"
333
- )
334
- ```
335
-
336
- Note, you can specify the filename via file [`filename` argument](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages).
337
-
338
- **Send a sticker message**
339
- It could use a link or a sticker_id.
283
+ client.messages.send_document(sender_id: 123123, recipient_number: 56789, document_id: "1234", caption: "Ignacio Chiazzo")
284
+ # Note, you can specify the filename via argument [`filename`](https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages).
340
285
 
341
- ```ruby
342
- # with a link
343
- messages_api.send_sticker(sender_id: 123123, recipient_number: 56789, link: "link")
286
+ # Send a sticker message
287
+ # It could use a link or a sticker_id.
288
+ # with a link
289
+ client.messages.send_sticker(sender_id: 123123, recipient_number: 56789, link: "link")
344
290
 
345
291
  # with a sticker_id
346
- messages_api.send_sticker(sender_id: 123123, recipient_number: 56789, sticker_id: "1234")
347
- ```
292
+ client.messages.send_sticker(sender_id: 123123, recipient_number: 56789, sticker_id: "1234")
348
293
 
349
- **Send contacts message**
350
- To send a contact, you need to create a Contact instance object that contain objects embedded like `addresses`, `birthday`, `emails`, `name`, `org`. See this [guide](/test/contact_helper.rb) to learn how to create contacts objects.
294
+ # Send contacts message
295
+ # To send a contact, you need to create a Contact instance object that contain objects embedded like `addresses`, `birthday`, `emails`, `name`, `org`. See this [guide](/test/contact_helper.rb) to learn how to create contacts objects.
296
+ client.messages.send_contacts(sender_id: 123123, recipient_number: 56789, contacts: [create_contact(params)])
351
297
 
352
- ```ruby
353
- contacts = [create_contact(params)]
354
- messages_api.send_contacts(sender_id: 123123, recipient_number: 56789, contacts: contacts)
355
- ```
298
+ # Alternatively, you could pass a plain json like this:
299
+ client.messages.send_contacts(sender_id: 123123, recipient_number: 56789, contacts_json: {...})
356
300
 
357
- Alternatively, you could pass a plain json like this:
301
+ # Send a template message
302
+ # WhatsApp message templates are specific message formats that businesses use to send out notifications or customer care messages to people that have opted in to notifications. Messages can include appointment reminders, shipping information, issue resolution or payment updates.
358
303
 
359
- ```ruby
360
- messages_api.send_contacts(sender_id: 123123, recipient_number: 56789, contacts_json: {...})
304
+ # Before sending a message template, you need to create one. visit the [Official API Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates)
361
305
  ```
362
306
 
363
- **Send a template message**
364
- WhatsApp message templates are specific message formats that businesses use to send out notifications or customer care messages to people that have opted in to notifications. Messages can include appointment reminders, shipping information, issue resolution or payment updates.
365
-
366
- **Before sending a message template, you need to create one.** visit the [Official API Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates)
367
-
368
307
  <details> <summary>Component's example</summary>
369
308
 
370
309
  ```ruby
371
310
  currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
372
311
  date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
373
- image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
312
+ media_component = WhatsappSdk::Resource::MediaComponent.new(type: "image", link: "http(s)://URL")
374
313
  location = WhatsappSdk::Resource::Location.new(
375
314
  latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
376
315
  )
377
316
 
378
- parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
317
+ parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: media_component)
379
318
  parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING")
380
319
  parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency)
381
320
  parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time)
@@ -407,7 +346,9 @@ button_component2 = WhatsappSdk::Resource::Component.new(
407
346
  )
408
347
 
409
348
  location_component = WhatsappSdk::Resource::Component.new(type: "header", parameters: [parameter_location])
410
- @messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [component_1])
349
+ client.messages.send_template(
350
+ sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components: [...]
351
+ )
411
352
  ```
412
353
 
413
354
  </details>
@@ -415,7 +356,7 @@ location_component = WhatsappSdk::Resource::Component.new(type: "header", parame
415
356
  Alternatively, you could pass a plain json like this:
416
357
 
417
358
  ```ruby
418
- @messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [{...}])
359
+ client.messages.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [{...}])
419
360
  ```
420
361
 
421
362
  **Send interactive messages**
@@ -449,8 +390,8 @@ interactive_list_messages = WhatsappSdk::Resource::Interactive.new(
449
390
  action: interactive_action
450
391
  )
451
392
 
452
- messages_api.send_interactive_list_messages(
453
- sender_id: 12_345, recipient_number: 1234567890,
393
+ client.messages.send_interactive_list_messages(
394
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
454
395
  interactive: interactive_list_messages
455
396
  )
456
397
  ```
@@ -460,7 +401,7 @@ messages_api.send_interactive_list_messages(
460
401
  Alternatively, you could pass a plain json like this:
461
402
 
462
403
  ```ruby
463
- messages_api.send_interactive_list_messages(
404
+ client.messages.send_interactive_list_messages(
464
405
  sender_id: 12_345, recipient_number: 1234567890
465
406
  interactive_json: {...}
466
407
  )
@@ -495,7 +436,7 @@ interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
495
436
  action: interactive_action
496
437
  )
497
438
 
498
- messages_api.send_interactive_reply_buttons(
439
+ client.messages.send_interactive_reply_buttons(
499
440
  sender_id: 12_345, recipient_number: 1234567890,
500
441
  interactive: interactive_reply_buttons
501
442
  )
@@ -506,7 +447,7 @@ messages_api.send_interactive_reply_buttons(
506
447
  Alternative, you could pass a plain json like this:
507
448
 
508
449
  ```ruby
509
- messages_api.send_interactive_reply_buttons(
450
+ client.messages.send_interactive_reply_buttons(
510
451
  sender_id: 12_345, recipient_number: 1234567890
511
452
  interactive_json: {...}
512
453
  )
@@ -514,6 +455,10 @@ messages_api.send_interactive_reply_buttons(
514
455
 
515
456
  </details>
516
457
 
458
+ ### Errors
459
+
460
+ If the API returns an error then an exception `WhatsappSdk::Api::Responses::HttpResponseError` is raised. The object contains information returned by the Cloud API. For more information about the potential error check the [official documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes/).
461
+
517
462
  ## Examples
518
463
 
519
464
  Visit [the example file](/example.rb) with examples to call the API in a single file.