whatsapp_sdk 0.12.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.cursorrules +53 -0
  3. data/.github/workflows/codeql-analysis.yml +8 -6
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +0 -3
  6. data/CHANGELOG.md +19 -7
  7. data/Gemfile +1 -4
  8. data/Gemfile.lock +9 -23
  9. data/README.md +148 -223
  10. data/SECURITY.md +21 -0
  11. data/example.rb +217 -125
  12. data/lib/whatsapp_sdk/api/api_configuration.rb +2 -5
  13. data/lib/whatsapp_sdk/api/business_profile.rb +12 -26
  14. data/lib/whatsapp_sdk/api/client.rb +31 -43
  15. data/lib/whatsapp_sdk/api/medias.rb +31 -46
  16. data/lib/whatsapp_sdk/api/messages.rb +28 -142
  17. data/lib/whatsapp_sdk/api/phone_numbers.rb +32 -36
  18. data/lib/whatsapp_sdk/api/request.rb +2 -2
  19. data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +12 -30
  20. data/lib/whatsapp_sdk/api/responses/http_response_error.rb +18 -0
  21. data/lib/whatsapp_sdk/api/responses/id_response.rb +15 -0
  22. data/lib/whatsapp_sdk/api/responses/message_data_response.rb +18 -37
  23. data/lib/whatsapp_sdk/api/responses/pagination_records.rb +17 -0
  24. data/lib/whatsapp_sdk/api/responses/success_response.rb +3 -20
  25. data/lib/whatsapp_sdk/api/templates.rb +30 -63
  26. data/lib/whatsapp_sdk/configuration.rb +2 -24
  27. data/lib/whatsapp_sdk/error.rb +0 -1
  28. data/lib/whatsapp_sdk/resource/address.rb +1 -30
  29. data/lib/whatsapp_sdk/resource/address_type.rb +3 -8
  30. data/lib/whatsapp_sdk/resource/business_profile.rb +28 -3
  31. data/lib/whatsapp_sdk/resource/button_parameter.rb +4 -20
  32. data/lib/whatsapp_sdk/resource/component.rb +12 -43
  33. data/lib/whatsapp_sdk/resource/contact.rb +1 -30
  34. data/lib/whatsapp_sdk/resource/contact_response.rb +1 -9
  35. data/lib/whatsapp_sdk/resource/currency.rb +0 -8
  36. data/lib/whatsapp_sdk/resource/date_time.rb +0 -6
  37. data/lib/whatsapp_sdk/resource/email.rb +1 -10
  38. data/lib/whatsapp_sdk/resource/errors.rb +3 -17
  39. data/lib/whatsapp_sdk/resource/interactive.rb +6 -27
  40. data/lib/whatsapp_sdk/resource/interactive_action.rb +10 -24
  41. data/lib/whatsapp_sdk/resource/interactive_action_reply_button.rb +5 -19
  42. data/lib/whatsapp_sdk/resource/interactive_action_section.rb +1 -11
  43. data/lib/whatsapp_sdk/resource/interactive_action_section_row.rb +0 -11
  44. data/lib/whatsapp_sdk/resource/interactive_body.rb +0 -10
  45. data/lib/whatsapp_sdk/resource/interactive_footer.rb +0 -10
  46. data/lib/whatsapp_sdk/resource/interactive_header.rb +25 -53
  47. data/lib/whatsapp_sdk/resource/location.rb +44 -0
  48. data/lib/whatsapp_sdk/resource/media.rb +28 -114
  49. data/lib/whatsapp_sdk/resource/media_component.rb +89 -0
  50. data/lib/whatsapp_sdk/resource/media_types.rb +0 -3
  51. data/lib/whatsapp_sdk/resource/message.rb +0 -5
  52. data/lib/whatsapp_sdk/resource/message_template_namespace.rb +17 -0
  53. data/lib/whatsapp_sdk/resource/name.rb +1 -28
  54. data/lib/whatsapp_sdk/resource/org.rb +1 -13
  55. data/lib/whatsapp_sdk/resource/parameter_object.rb +71 -74
  56. data/lib/whatsapp_sdk/resource/phone_number.rb +40 -22
  57. data/lib/whatsapp_sdk/resource/phone_number_component.rb +25 -0
  58. data/lib/whatsapp_sdk/resource/template.rb +43 -36
  59. data/lib/whatsapp_sdk/resource/url.rb +1 -10
  60. data/lib/whatsapp_sdk/version.rb +1 -2
  61. data/lib/whatsapp_sdk.rb +0 -4
  62. data/whatsapp_sdk.gemspec +1 -4
  63. metadata +13 -62
  64. data/lib/whatsapp_sdk/api/response.rb +0 -51
  65. data/lib/whatsapp_sdk/api/responses/business_profile_data_response.rb +0 -56
  66. data/lib/whatsapp_sdk/api/responses/data_response.rb +0 -26
  67. data/lib/whatsapp_sdk/api/responses/error_response.rb +0 -32
  68. data/lib/whatsapp_sdk/api/responses/media_data_response.rb +0 -48
  69. data/lib/whatsapp_sdk/api/responses/message_error_response.rb +0 -13
  70. data/lib/whatsapp_sdk/api/responses/message_template_namespace_data_response.rb +0 -37
  71. data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +0 -60
  72. data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +0 -39
  73. data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +0 -34
  74. data/lib/whatsapp_sdk/api/responses/template_data_response.rb +0 -51
  75. data/lib/whatsapp_sdk/api/responses/templates_data_response.rb +0 -39
  76. data/sorbet/config +0 -6
  77. data/sorbet/rbi/annotations/faraday.rbi +0 -17
  78. data/sorbet/rbi/annotations/mocha.rbi +0 -34
  79. data/sorbet/rbi/annotations/rainbow.rbi +0 -269
  80. data/sorbet/rbi/gems/faraday-multipart@1.0.4.rbi +0 -270
  81. data/sorbet/rbi/gems/faraday-net_http@2.0.3.rbi +0 -182
  82. data/sorbet/rbi/gems/faraday@2.3.0.rbi +0 -2494
  83. data/sorbet/rbi/gems/method_source@1.0.0.rbi +0 -272
  84. data/sorbet/rbi/gems/minitest@5.16.1.rbi +0 -1459
  85. data/sorbet/rbi/gems/mocha@1.14.0.rbi +0 -60
  86. data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +0 -239
  87. data/sorbet/rbi/gems/netrc@0.11.0.rbi +0 -150
  88. data/sorbet/rbi/gems/oj@3.13.14.rbi +0 -589
  89. data/sorbet/rbi/gems/zeitwerk@2.6.0.rbi +0 -867
  90. data/sorbet/rbi/todo.rbi +0 -8
  91. data/sorbet/shims/request.rbi +0 -10
  92. data/sorbet/tapioca/config.yml +0 -13
  93. data/sorbet/tapioca/require.rb +0 -4
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
- ```
185
+ # Get the details of your business
186
+ client.business_profiles.get(phone_number_id)
183
187
 
184
- Update the details of your business
185
-
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
- ```
198
+ # Get the list of phone numbers registered
199
+ client.phone_numbers.list(business_id)
201
200
 
202
- Get the a phone number by id
201
+ # Get the a phone number by id
202
+ client.phone_numbers.get(phone_number_id)
203
203
 
204
- ```ruby
205
- phone_numbers_api.registered_numbers(123456) # accepts a phone_number_id
206
- ```
207
-
208
- Register a phone number
204
+ # Register a phone number
205
+ client.phone_numbers.register_number(phone_number_id, pin)
209
206
 
210
- ```ruby
211
- phone_numbers_api.register_number(phone_number_id, pin)
212
- ```
213
-
214
- Deregister a phone number
215
-
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,188 +214,141 @@ phone_numbers_api.deregister_number(phone_number_id)
223
214
 
224
215
  <details>
225
216
 
226
- Upload a media
227
-
228
- ```ruby
229
- medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
230
- ```
231
-
232
- Get a media
233
-
234
217
  ```ruby
235
- media = medias_api.media(media_id: MEDIA_ID)
236
- ```
218
+ # Upload a media
219
+ client.media.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
237
220
 
238
- Download media
221
+ # Get a media
222
+ media = client.media.get(media_id: MEDIA_ID)
239
223
 
240
- ```ruby
241
- medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png', media_type: "image/png")
242
- ```
224
+ # Download media
225
+ client.media.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png', media_type: "image/png")
243
226
 
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
- ```
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).
335
285
 
336
- **Send a sticker message**
337
- It could use a link or a sticker_id.
338
-
339
- ```ruby
340
- # with a link
341
- 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")
342
290
 
343
291
  # with a sticker_id
344
- messages_api.send_sticker(sender_id: 123123, recipient_number: 56789, sticker_id: "1234")
345
- ```
292
+ client.messages.send_sticker(sender_id: 123123, recipient_number: 56789, sticker_id: "1234")
346
293
 
347
- **Send contacts message**
348
- 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)])
349
297
 
350
- ```ruby
351
- contacts = [create_contact(params)]
352
- messages_api.send_contacts(sender_id: 123123, recipient_number: 56789, contacts: contacts)
353
- ```
298
+ # Alternatively, you could pass a plain json like this:
299
+ client.messages.send_contacts(sender_id: 123123, recipient_number: 56789, contacts_json: {...})
354
300
 
355
- 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.
356
303
 
357
- ```ruby
358
- 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)
359
305
  ```
360
306
 
361
- **Send a template message**
362
- 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.
363
-
364
- **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)
365
-
366
307
  <details> <summary>Component's example</summary>
367
308
 
368
309
  ```ruby
369
310
  currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
370
311
  date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
371
- image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
372
-
373
- parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Image, image: image)
374
- # You can also use a plain string as type e.g.
375
- # parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
376
- parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Text, text: "TEXT_STRING")
377
- parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Currency, currency: currency)
378
- parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::DateTime, date_time: date_time)
379
-
380
- header_component = WhatsappSdk::Resource::Component.new(
381
- type: WhatsappSdk::Resource::Component::Type::Header,
382
- parameters: [parameter_image]
312
+ media_component = WhatsappSdk::Resource::MediaComponent.new(type: "image", link: "http(s)://URL")
313
+ location = WhatsappSdk::Resource::Location.new(
314
+ latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
383
315
  )
384
316
 
317
+ parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: media_component)
318
+ parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING")
319
+ parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency)
320
+ parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time)
321
+ parameter_location = WhatsappSdk::Resource::ParameterObject.new(type: "location", location: location)
322
+
323
+ header_component = WhatsappSdk::Resource::Component.new(type: "header", parameters: [parameter_image])
324
+
385
325
  body_component = WhatsappSdk::Resource::Component.new(
386
- type: WhatsappSdk::Resource::Component::Type::Body,
326
+ type: "body",
387
327
  parameters: [parameter_text, parameter_currency, parameter_date_time]
388
328
  )
389
329
 
390
330
  button_component1 = WhatsappSdk::Resource::Component.new(
391
- type: WhatsappSdk::Resource::Component::Type::Button,
331
+ type: "button",
392
332
  index: 0,
393
- sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
333
+ sub_type: "quick_reply",
394
334
  parameters: [
395
- WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload, payload: "PAYLOAD")
335
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
396
336
  ]
397
337
  )
398
338
 
399
339
  button_component2 = WhatsappSdk::Resource::Component.new(
400
- type: WhatsappSdk::Resource::Component::Type::Button,
340
+ type: "button",
401
341
  index: 1,
402
- sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
342
+ sub_type: "quick_reply",
403
343
  parameters: [
404
- WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload, payload: "PAYLOAD")
344
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
405
345
  ]
406
346
  )
407
- @messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [component_1])
347
+
348
+ location_component = WhatsappSdk::Resource::Component.new(type: "header", parameters: [parameter_location])
349
+ client.messages.send_template(
350
+ sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components: [...]
351
+ )
408
352
  ```
409
353
 
410
354
  </details>
@@ -412,7 +356,7 @@ button_component2 = WhatsappSdk::Resource::Component.new(
412
356
  Alternatively, you could pass a plain json like this:
413
357
 
414
358
  ```ruby
415
- @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: [{...}])
416
360
  ```
417
361
 
418
362
  **Send interactive messages**
@@ -422,28 +366,14 @@ Visit the [Official API Documentation](https://developers.facebook.com/docs/what
422
366
  <summary>List Message's example</summary>
423
367
 
424
368
  ```ruby
425
- interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
426
- type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
427
- text: "I am the header!"
428
- )
429
-
430
- interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
431
- text: "I am the body!"
432
- )
433
-
434
- interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
435
- text: "I am the footer!"
436
- )
437
-
438
- interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
439
- type: WhatsappSdk::Resource::InteractiveAction::Type::ListMessage
440
- )
369
+ interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(type: "text", text: "I am the header!")
370
+ interactive_body = WhatsappSdk::Resource::InteractiveBody.new(text: "I am the body!")
371
+ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(text: "I am the footer!")
372
+ interactive_action = WhatsappSdk::Resource::InteractiveAction.new(type: "list_message")
441
373
 
442
374
  interactive_action.button = "I am the button CTA"
443
375
 
444
- interactive_section_1 = WhatsappSdk::Resource::InteractiveActionSection.new(
445
- title: "I am the section 1"
446
- )
376
+ interactive_section_1 = WhatsappSdk::Resource::InteractiveActionSection.new(title: "I am the section 1")
447
377
  interactive_section_1_row_1 = WhatsappSdk::Resource::InteractiveActionSectionRow.new(
448
378
  title: "I am the row 1 title",
449
379
  id: "section_1_row_1",
@@ -453,15 +383,15 @@ interactive_section_1.add_row(interactive_section_1_row_1)
453
383
  interactive_action.add_section(interactive_section_1)
454
384
 
455
385
  interactive_list_messages = WhatsappSdk::Resource::Interactive.new(
456
- type: WhatsappSdk::Resource::Interactive::Type::ListMessage,
386
+ type: "list",
457
387
  header: interactive_header,
458
388
  body: interactive_body,
459
389
  footer: interactive_footer,
460
390
  action: interactive_action
461
391
  )
462
392
 
463
- messages_api.send_interactive_list_messages(
464
- sender_id: 12_345, recipient_number: 1234567890,
393
+ client.messages.send_interactive_list_messages(
394
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
465
395
  interactive: interactive_list_messages
466
396
  )
467
397
  ```
@@ -471,7 +401,7 @@ messages_api.send_interactive_list_messages(
471
401
  Alternatively, you could pass a plain json like this:
472
402
 
473
403
  ```ruby
474
- messages_api.send_interactive_list_messages(
404
+ client.messages.send_interactive_list_messages(
475
405
  sender_id: 12_345, recipient_number: 1234567890
476
406
  interactive_json: {...}
477
407
  )
@@ -481,23 +411,11 @@ messages_api.send_interactive_list_messages(
481
411
  <summary>Reply Button's example</summary>
482
412
 
483
413
  ```ruby
484
- interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
485
- type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
486
- text: "I am the header!"
487
- )
488
-
489
- interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
490
- text: "I am the body!"
491
- )
492
-
493
- interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
494
- text: "I am the footer!"
495
- )
496
-
497
- interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
498
- type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton
499
- )
414
+ interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(type: "text", text: "I am the header!")
415
+ interactive_body = WhatsappSdk::Resource::InteractiveBody.new(text: "I am the body!")
416
+ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(text: "I am the footer!")
500
417
 
418
+ interactive_action = WhatsappSdk::Resource::InteractiveAction.new(type: "reply_button")
501
419
  interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
502
420
  title: "I am the reply button 1",
503
421
  id: "button_1"
@@ -511,14 +429,14 @@ interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton
511
429
  interactive_action.add_reply_button(interactive_reply_button_2)
512
430
 
513
431
  interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
514
- type: WhatsappSdk::Resource::Interactive::Type::ReplyButton,
432
+ type: "reply_button",
515
433
  header: interactive_header,
516
434
  body: interactive_body,
517
435
  footer: interactive_footer,
518
436
  action: interactive_action
519
437
  )
520
438
 
521
- messages_api.send_interactive_reply_buttons(
439
+ client.messages.send_interactive_reply_buttons(
522
440
  sender_id: 12_345, recipient_number: 1234567890,
523
441
  interactive: interactive_reply_buttons
524
442
  )
@@ -529,7 +447,7 @@ messages_api.send_interactive_reply_buttons(
529
447
  Alternative, you could pass a plain json like this:
530
448
 
531
449
  ```ruby
532
- messages_api.send_interactive_reply_buttons(
450
+ client.messages.send_interactive_reply_buttons(
533
451
  sender_id: 12_345, recipient_number: 1234567890
534
452
  interactive_json: {...}
535
453
  )
@@ -537,6 +455,10 @@ messages_api.send_interactive_reply_buttons(
537
455
 
538
456
  </details>
539
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
+
540
462
  ## Examples
541
463
 
542
464
  Visit [the example file](/example.rb) with examples to call the API in a single file.
@@ -548,7 +470,10 @@ Visit [the example file](/example.rb) with examples to call the API in a single
548
470
 
549
471
  ## Troubleshooting
550
472
 
551
- - If the API response is `success`, but the message is not delivered, ensure the device you're sending the message to is using a supported Whatsapp version. [Check documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/support/troubleshooting#message-not-delivered). Try also replying a message to the number you are registered on your Whatsapp.
473
+ If you try to send a text message directly without a message template created and approved in your Meta control panel, you can't start a chat with other people. But if you receive a message before, it's possible to send a message.
474
+
475
+ If the API response is still `success`, but the message is not delivered:
476
+ - ensure the device you're sending the message to is using a supported Whatsapp version. [Check documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/support/troubleshooting#message-not-delivered). Try also replying a message to the number you are registered on your Whatsapp.
552
477
  - Ensure your Meta App uses an API version greater than or equal to `v.14`.
553
478
  - Ensure that the Panel in the Facebook dashboard doesn't display any errors.
554
479
 
data/SECURITY.md ADDED
@@ -0,0 +1,21 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Use this section to tell people about which versions of your project are
6
+ currently being supported with security updates.
7
+
8
+ | Version | Supported |
9
+ | ------- | ------------------ |
10
+ | 5.1.x | :white_check_mark: |
11
+ | 5.0.x | :x: |
12
+ | 4.0.x | :white_check_mark: |
13
+ | < 4.0 | :x: |
14
+
15
+ ## Reporting a Vulnerability
16
+
17
+ Use this section to tell people how to report a vulnerability.
18
+
19
+ Tell them where to go, how often they can expect to get an update on a
20
+ reported vulnerability, what to expect if the vulnerability is accepted or
21
+ declined, etc.