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
data/example.rb CHANGED
@@ -33,7 +33,7 @@ if ACCESS_TOKEN == "<TODO replace>"
33
33
  exit
34
34
  end
35
35
 
36
- puts "\n\n\n\n\n\n *************** Starting calling the Cloud API *************** \n"
36
+ puts "\n\n\n\ ------------------ Starting calling the Cloud API -------------------\n"
37
37
 
38
38
  ################# Initialize Client #################
39
39
  WhatsappSdk.configure do |config|
@@ -41,38 +41,34 @@ WhatsappSdk.configure do |config|
41
41
  end
42
42
 
43
43
  ################# HELPERS ########################
44
- def print_message_sent(message_response)
45
- if message_response.ok?
46
- puts "Message sent to: #{message_response.data.contacts.first.input}"
47
- else
48
- puts "Error: #{message_response.error&.to_s}"
49
- end
44
+ def print_message_sent(message_response, type = "")
45
+ puts "Message #{type} sent to: #{message_response.contacts.first.input}"
50
46
  end
51
47
 
52
- def print_data_or_error(response, identifier)
53
- if response.error?
54
- return "Error: #{response.error&.to_s}"
48
+ def run_and_catch_error(message, &block)
49
+ begin
50
+ yield
51
+ rescue WhatsappSdk::Api::Responses::HttpResponseError => e
52
+ puts "Error: #{e}"
55
53
  end
56
-
57
- return identifier
58
54
  end
59
55
  ##################################################
60
56
 
57
+ client = WhatsappSdk::Api::Client.new
58
+
61
59
 
62
- medias_api = WhatsappSdk::Api::Medias.new
63
- messages_api = WhatsappSdk::Api::Messages.new
64
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
65
- business_profile_api = WhatsappSdk::Api::BusinessProfile.new
66
- templates_api = WhatsappSdk::Api::Templates.new
67
60
 
68
- ############################## Templates API ##############################
69
- ## Get list of templates
70
- templates = templates_api.templates(business_id: BUSINESS_ID)
71
- puts "GET Templates list : #{print_data_or_error(templates, templates.data&.templates.map { |r| r.template.name })}"
61
+
62
+ # ############################## Templates API ##############################
63
+ puts "\n\n ------------------ Testing Templates API ------------------------"
64
+
65
+ # ## Get list of templates
66
+ templates = client.templates.list(business_id: BUSINESS_ID)
67
+ puts "GET Templates list : #{ templates.records.map(&:name) }"
72
68
 
73
69
  ## Get message templates namespace
74
- template_namespace = templates_api.get_message_template_namespace(business_id: BUSINESS_ID)
75
- puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
70
+ template_namespace = client.templates.get_message_template_namespace(business_id: BUSINESS_ID)
71
+ puts "GET template by namespace: #{template_namespace.id}"
76
72
 
77
73
  # Create a template
78
74
  components_json = [
@@ -104,11 +100,15 @@ components_json = [
104
100
  }
105
101
  ]
106
102
 
107
- new_template = templates_api.create(
108
- business_id: BUSINESS_ID, name: "seasonal_promotion", language: "ka", category: "MARKETING",
109
- components_json: components_json, allow_category_change: true
110
- )
111
- puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
103
+ new_template = nil
104
+
105
+ run_and_catch_error("Create a template") do
106
+ new_template = client.templates.create(
107
+ business_id: BUSINESS_ID, name: "seasonal_promotion_2", language: "ka", category: "MARKETING",
108
+ components_json: components_json, allow_category_change: true
109
+ )
110
+ puts "GET template by namespace: #{template_namespace.id}"
111
+ end
112
112
 
113
113
  # Update a template
114
114
  components_json = [
@@ -124,150 +124,234 @@ components_json = [
124
124
  ]
125
125
  }
126
126
  ]
127
- updated_template = templates_api.update(template_id: "1624560287967996", category: "UTILITY")
128
- puts "UPDATE template by id: #{print_data_or_error(updated_template, updated_template.data&.id)}"
127
+
128
+ if new_template
129
+ updated_template = client.templates.update(template_id: new_template.id, category: "UTILITY")
130
+ puts "UPDATE template by id: #{updated_template.id}"
131
+ end
129
132
 
130
133
  ## Delete a template
131
- delete_template = templates_api.delete(business_id: BUSINESS_ID, name: "seasonal_promotion") # delete by name
132
- puts "DELETE template by id: #{print_data_or_error(delete_template, delete_template.data&.id) }"
133
- # templates_api.delete(business_id: BUSINESS_ID, name: "name2", hsm_id: "243213188351928") # delete by name and id
134
+ run_and_catch_error("Delete a template") do
135
+ delete_template = client.templates.delete(business_id: BUSINESS_ID, name: "seasonal_promotion") # delete by name
136
+ puts "Delete template by id: #{delete_template.id}"
137
+ end
138
+
139
+ # client.templates.delete(business_id: BUSINESS_ID, name: "name2", hsm_id: "243213188351928") # delete by name and id
140
+
141
+
142
+
143
+ # ############################## Business API ##############################
144
+ puts "\n\n\n ------------------ Testing Business API -----------------------"
145
+
146
+ business_profile = client.business_profiles.get(SENDER_ID)
147
+ puts "GET Business Profile by id: #{business_profile.about}"
148
+
149
+ updated_bp = client.business_profiles.update(phone_number_id: SENDER_ID, params: { websites: ["www.ignaciochiazzo.com"] } )
150
+ puts "UPDATE Business Profile by id: #{updated_bp} }"
151
+
152
+ run_and_catch_error("Update business profile") do
153
+ # about can't be set
154
+ updated_bp = client.business_profiles.update(phone_number_id: SENDER_ID, params: { about: "A cool business" } )
155
+ end
134
156
 
135
- ############################## Business API ##############################
136
- business_profile = business_profile_api.details(SENDER_ID)
137
- puts "DELETE Business Profile by id: #{print_data_or_error(delete_template, business_profile.data&.about) }"
138
157
 
139
- updated_bp = business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
140
- puts "UPDATE Business Profile by id: #{print_data_or_error(updated_bp, updated_bp.data&.success?) }"
141
158
 
142
159
  ############################## Phone Numbers API ##############################
143
- registered_number = phone_numbers_api.registered_number(SENDER_ID)
144
- puts "GET Registered number: #{print_data_or_error(registered_number, registered_number.data&.id)}"
160
+ puts "\n\n\n ------------------ Testing Phone Numbers API -----------------------"
161
+
162
+ # Get phone numbers
163
+ registered_number = client.phone_numbers.get(SENDER_ID)
164
+ puts "GET Registered number: #{registered_number.id}"
165
+
166
+ # Get phone number
167
+ registered_numbers = client.phone_numbers.list(BUSINESS_ID)
168
+ puts "GET Registered numbers: #{registered_numbers.records.map(&:id)}"
169
+
170
+ # Deregister a phone number - I skip registering so that the number can upload media
171
+ # run_and_catch_error("Deregister a phone number") do
172
+ # deregister_number_result = client.phone_numbers.deregister_number(SENDER_ID)
173
+ # puts "DEREGISTER number: #{deregister_number_result}"
174
+ # end
175
+
176
+ # Register a phone number
177
+ run_and_catch_error("Register a phone number") do
178
+ register_number_result = client.phone_numbers.register_number(SENDER_ID, 123456)
179
+ puts "REGISTER number: #{register_number_result}"
180
+ end
145
181
 
146
- registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
147
- puts "GET Registered numbers: #{print_data_or_error(registered_number, registered_numbers.data&.phone_numbers.map(&:id))}"
182
+ # Register a fake number
183
+ begin
184
+ fake_number = "1234567890"
185
+ client.phone_numbers.register_number(fake_number, 123456)
186
+ rescue WhatsappSdk::Api::Responses::HttpResponseError => e
187
+ puts "Error: #{e}"
188
+ end
148
189
 
149
- ############################## Media API ##############################
150
190
 
191
+ ############################## Media API ##############################
192
+ puts "\n\n\n ------------------ Testing Media API"
151
193
  ##### Image #####
152
194
  # upload a Image
153
- uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
154
- puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
195
+ run_and_catch_error("Upload a Image") do
196
+ uploaded_media = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")
197
+ puts "Uploaded image id: #{uploaded_media&.id}"
198
+ end
155
199
 
156
200
  # get a media Image
157
- if uploaded_media.data&.id
158
- media = medias_api.media(media_id: uploaded_media.data&.id)
159
- puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
201
+ if uploaded_media&.id
202
+ image = client.media.get(media_id: uploaded_media.id)
203
+ puts "GET image id: #{image.id}"
160
204
 
161
205
  # download media Image
162
- download_image = medias_api.download(url: media.data.url, file_path: 'tmp/downloaded_image.png', media_type: "image/png")
163
- puts "Downloaded: #{print_data_or_error(download_image, download_image.data.success?)}"
206
+ download_image = client.media.download(url: image.url, file_path: 'test/fixtures/assets/downloaded_image.png', media_type: "image/png")
207
+ puts "Downloaded Image: #{download_image}"
164
208
 
209
+ uploaded_media = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/whatsapp.png", type: "image/png")
165
210
  # delete a media
166
- deleted_media = medias_api.delete(media_id: media.data.id)
167
- puts "DELETE: #{print_data_or_error(deleted_media, deleted_media.data.success?)}"
211
+ deleted_media = client.media.delete(media_id: uploaded_media.id)
212
+ puts "Delete image: #{deleted_media.success?}"
213
+ else
214
+ puts "No media to download and delete"
168
215
  end
169
216
 
217
+ #### Video ####
218
+ # upload a video
219
+ uploaded_video = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/riquelme.mp4", type: "video/mp4")
220
+ puts "Uploaded video: #{uploaded_video.id}"
221
+
222
+ video = client.media.get(media_id: uploaded_video.id)
223
+
224
+ # upload a video
225
+ uploaded_video = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/riquelme.mp4", type: "video/mp4")
226
+ puts "Uploaded video id: #{uploaded_video.id}"
227
+
170
228
  #### Audio ####
171
229
  # upload an audio
172
- uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/downloaded_audio.ogg", type: "audio/ogg")
173
- puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
174
-
175
- if uploaded_media.data&.id
176
- media_id = uploaded_media.data&.id
177
- media_id = uploaded_media.data&.id
178
- puts "Uploaded media id: #{media_id}"
179
- media_id = uploaded_media.data&.id
180
- puts "Uploaded media id: #{media_id}"
230
+ audio_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/downloaded_audio.ogg", type: "audio/ogg")
231
+ puts "Uploaded audio id: #{audio_response.id}"
181
232
 
233
+ if audio_response&.id
234
+ audio_id = audio_response&.id
182
235
  # get a media audio
183
- media = medias_api.media(media_id: media_id)
184
- puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
236
+ audio = client.media.get(media_id: audio_id)
237
+ puts "GET Audio id: #{audio.id}"
185
238
 
186
239
  # get a media audio
187
- audio_link = media.data.url
188
- download_image = medias_api.download(url: audio_link, file_path: 'tmp/downloaded_audio2.ogg', media_type: "audio/ogg")
189
- puts "Download Media Audio: #{print_data_or_error(download_image, download_image.data.success?)}"
240
+ audio_link = audio.url
241
+ download_image = client.media.download(url: audio_link, file_path: 'test/fixtures/assets/downloaded_audio2.ogg', media_type: "audio/ogg")
242
+ puts "Download Audio: #{download_image}"
190
243
  end
191
244
 
245
+ # upload a document
246
+ document_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/document.pdf", type: "application/pdf")
247
+ puts "Uploaded document id: #{document_response.id}"
248
+
249
+ document = client.media.get(media_id: document_response.id)
250
+ puts "GET document id: #{document.id}"
251
+
252
+ # upload a sticker
253
+ sticker_response = client.media.upload(sender_id: SENDER_ID, file_path: "test/fixtures/assets/sticker.webp", type: "image/webp")
254
+ puts "Uploaded sticker id: #{sticker_response.id}"
255
+
256
+ sticker = client.media.get(media_id: sticker_response.id)
257
+ puts "GET Sticker id: #{sticker.id}"
258
+
259
+
260
+
192
261
  ############################## Messages API ##############################
262
+ puts "\n\n\n ------------------ Testing Messages API -----------------------"
193
263
 
194
264
  ######### SEND A TEXT MESSAGE
195
- message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "Hey there! it's Whatsapp Ruby SDK")
196
- print_message_sent(message_sent)
265
+ message_sent = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
266
+ message: "Hey there! it's Whatsapp Ruby SDK")
267
+ print_message_sent(message_sent, "text")
197
268
 
198
269
  ######### React to a message
199
- message_id = message_sent.data.messages.first.id
200
- reaction_1_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "\u{1f550}")
201
- reaction_2_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "⛄️")
202
- puts "Message Reaction 1: #{print_data_or_error(reaction_1_sent, reaction_1_sent.data&.messages.first&.id)}"
203
- puts "Message Reaction 2: #{print_data_or_error(reaction_2_sent, reaction_2_sent.data&.messages.first&.id)}"
270
+ message_id = message_sent&.messages.first.id
271
+ reaction_1_sent = client.messages.send_reaction(
272
+ sender_id: SENDER_ID,
273
+ recipient_number: RECIPIENT_NUMBER,
274
+ message_id: message_id,
275
+ emoji: "\u{1f550}"
276
+ ) if message_id
277
+
278
+ reaction_2_sent = client.messages.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
279
+ message_id: message_id, emoji: "⛄️") if message_id
280
+ puts "Message Reaction 1: #{reaction_1_sent&.messages.first&.id}"
281
+ puts "Message Reaction 2: #{reaction_2_sent&.messages.first&.id}"
204
282
 
205
283
  ######### Reply to a message
206
- message_to_reply_id = message_sent.data.messages.first.id
207
- reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply", message_id: message_to_reply_id)
208
- print_message_sent(reply)
284
+ message_to_reply_id = message_sent.messages.first.id
285
+ reply = client.messages.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply",
286
+ message_id: message_to_reply_id)
287
+ print_message_sent(reply, "reply")
209
288
 
210
289
  ######### Send location
211
- location_sent = messages_api.send_location(
290
+
291
+ location_sent = client.messages.send_location(
212
292
  sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
213
293
  longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
214
294
  )
215
- print_message_sent(location_sent)
295
+ print_message_sent(location_sent, "location")
216
296
 
217
297
  ######### READ A MESSAGE
218
- # messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
298
+ # client.messages.read_message(sender_id: SENDER_ID, message_id: msg_id)
219
299
 
220
300
  ######### SEND AN IMAGE
221
301
  # Send an image with a link
222
- if media.data&.id
223
- image_sent = messages_api.send_image(
224
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.data.url, caption: "Ignacio Chiazzo Profile"
302
+ if image&.id
303
+ image_sent = client.messages.send_image(
304
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: image.url, caption: "Ignacio Chiazzo Profile"
225
305
  )
226
- print_message_sent(image_sent)
306
+ print_message_sent(image_sent, "image via url")
227
307
 
228
308
  # Send an image with an id
229
- messages_api.send_image(
230
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.data.id, caption: "Ignacio Chiazzo Profile"
309
+ client.messages.send_image(
310
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: image.id, caption: "Ignacio Chiazzo Profile"
231
311
  )
232
- print_message_sent(image_sent)
312
+ print_message_sent(image_sent, "image via id")
233
313
  end
234
314
 
235
315
  ######### SEND AUDIOS
236
316
  ## with a link
237
- audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
238
- print_message_sent(audio_sent)
317
+ audio_sent = client.messages.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: audio.url)
318
+ print_message_sent(audio_sent, "audio via url")
239
319
 
240
320
  ## with an audio id
241
- audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
242
- print_message_sent(audio_sent)
321
+ audio_sent = client.messages.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: audio.id)
322
+ print_message_sent(audio_sent, "audio via id")
243
323
 
244
324
  ######### SEND DOCUMENTS
245
325
  ## with a link
246
- document_sent = messages_api.send_document(
247
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
248
- )
249
- print_message_sent(document_sent)
326
+ if document&.id
327
+ document_sent = client.messages.send_document(
328
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: document&.url, caption: "Ignacio Chiazzo"
329
+ )
330
+ print_message_sent(document_sent, "document via url")
250
331
 
251
- ## with a document id
252
- document_sent = messages_api.send_document(
253
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
254
- )
255
- print_message_sent(document_sent)
332
+ ## with a document id
333
+ document_sent = client.messages.send_document(
334
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: document&.id, caption: "Ignacio Chiazzo"
335
+ ) # modify
336
+ print_message_sent(document_sent, "document via id")
337
+ end
256
338
 
257
339
  ######### SEND STICKERS
258
- ## with a link
259
- sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
260
- print_message_sent(sticker_sent)
261
-
262
- ## with a sticker_id
263
- sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
264
- print_message_sent(sticker_sent)
340
+ if sticker&.id
341
+ ## with a link
342
+ sticker_sent = client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: sticker.url)
343
+ print_message_sent(sticker_sent, "sticker via url")
344
+
345
+ ## with a sticker_id
346
+ sticker_sent = client.messages.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: sticker.id)
347
+ print_message_sent(sticker_sent, "sticker via id")
348
+ end
265
349
 
266
350
  ######### SEND A TEMPLATE
267
351
  # Note: The template must have been created previously.
268
352
 
269
353
  # Send a template with no component
270
- response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
354
+ response_with_object = client.messages.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
271
355
  name: "hello_world", language: "en_US", components: [])
272
356
  puts response_with_object
273
357
 
@@ -275,9 +359,9 @@ puts response_with_object
275
359
  header_component = WhatsappSdk::Resource::Component.new(
276
360
  type: WhatsappSdk::Resource::Component::Type::HEADER
277
361
  )
278
- image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
279
- document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
280
- video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
362
+ image = WhatsappSdk::Resource::MediaComponent.new(type: "image", link: "http(s)://URL", caption: "caption")
363
+ document = WhatsappSdk::Resource::MediaComponent.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
364
+ video = WhatsappSdk::Resource::MediaComponent.new(type: "video", id: "123")
281
365
  location = WhatsappSdk::Resource::Location.new(
282
366
  latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
283
367
  )
@@ -340,7 +424,7 @@ button_component_2 = WhatsappSdk::Resource::Component.new(
340
424
  )
341
425
 
342
426
  # Send a template with component_json
343
- response_with_json = messages_api.send_template(
427
+ response_with_json = client.messages.send_template(
344
428
  sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
345
429
  components_json: [
346
430
  {
@@ -374,7 +458,7 @@ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
374
458
  )
375
459
 
376
460
  interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
377
- type: WhatsappSdk::Resource::InteractiveAction::Type::REPLY_BUTTON,
461
+ type: WhatsappSdk::Resource::InteractiveAction::Type::REPLY_BUTTON
378
462
  )
379
463
 
380
464
  interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
@@ -397,7 +481,7 @@ interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
397
481
  action: interactive_action
398
482
  )
399
483
 
400
- messages_api.send_interactive_reply_buttons(
484
+ client.messages.send_interactive_reply_buttons(
401
485
  sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
402
486
  interactive: interactive_reply_buttons
403
487
  )
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "request"
4
- require_relative "response"
5
4
  require_relative "../resource/business_profile"
6
5
 
7
6
  module WhatsappSdk
@@ -22,8 +21,8 @@ module WhatsappSdk
22
21
  # Get the details of business profile.
23
22
  #
24
23
  # @param phone_number_id [Integer] Phone Number Id.
25
- # @return [Api::Response] Response object.
26
- def details(phone_number_id, fields: nil)
24
+ # @return [Resource::BusinessProfile] Response object.
25
+ def get(phone_number_id, fields: nil)
27
26
  fields = if fields
28
27
  fields.join(',')
29
28
  else
@@ -35,20 +34,22 @@ module WhatsappSdk
35
34
  endpoint: "#{phone_number_id}/whatsapp_business_profile?fields=#{fields}"
36
35
  )
37
36
 
38
- Api::Response.new(
39
- response: response,
40
- data_class_type: Api::Responses::BusinessProfileDataResponse
41
- )
37
+ # In the future it might have multiple business profiles.
38
+ Resource::BusinessProfile.from_hash(response["data"][0])
39
+ end
40
+
41
+ def details(phone_number_id, fields: nil)
42
+ warn "[DEPRECATION] `details` is deprecated. Please use `get` instead."
43
+ get(phone_number_id, fields: fields)
42
44
  end
43
45
 
44
46
  # Update the details of business profile.
45
47
  #
46
48
  # @param phone_number_id [Integer] Phone Number Id.
47
49
  # @param params [Hash] Params to update.
48
- # @return [Api::Response] Response object.
50
+ # @return [Boolean] Whether the update was successful.
49
51
  def update(phone_number_id:, params:)
50
- # this is a required field
51
- params[:messaging_product] = 'whatsapp'
52
+ params[:messaging_product] = 'whatsapp' # messaging_products is a required field
52
53
  return raise InvalidVertical.new(vertical: params[:vertical]) unless valid_vertical?(params)
53
54
 
54
55
  response = send_request(
@@ -57,10 +58,7 @@ module WhatsappSdk
57
58
  params: params
58
59
  )
59
60
 
60
- Api::Response.new(
61
- response: response,
62
- data_class_type: Api::Responses::SuccessResponse
63
- )
61
+ Api::Responses::SuccessResponse.success_response?(response: response)
64
62
  end
65
63
 
66
64
  private
@@ -7,14 +7,14 @@ module WhatsappSdk
7
7
  module Api
8
8
  class Client
9
9
  API_VERSIONS = [
10
- 'v19.0', 'v18.0', 'v17.0', 'v16.0', 'v15.0', 'v14.0', 'v13.0', 'v12.0',
10
+ 'v21.0', 'v20.0', 'v19.0', 'v18.0', 'v17.0', 'v16.0', 'v15.0', 'v14.0', 'v13.0', 'v12.0',
11
11
  'v11.0', 'v10.0', 'v9.0', 'v8.0', 'v7.0', 'v6.0', 'v5.0', 'v4.0', 'v3.3',
12
12
  'v3.2', 'v3.1', 'v3.0', 'v2.12', 'v2.11', 'v2.10', 'v2.9', 'v2.8', 'v2.7',
13
13
  'v2.6', 'v2.5', 'v2.4', 'v2.3', 'v2.2', 'v2.1'
14
14
  ].freeze
15
15
 
16
16
  def initialize(
17
- access_token,
17
+ access_token = WhatsappSdk.configuration.access_token,
18
18
  api_version = ApiConfiguration::DEFAULT_API_VERSION,
19
19
  logger = nil,
20
20
  logger_options = {}
@@ -27,6 +27,26 @@ module WhatsappSdk
27
27
  @api_version = api_version
28
28
  end
29
29
 
30
+ def media
31
+ @media ||= WhatsappSdk::Api::Medias.new
32
+ end
33
+
34
+ def messages
35
+ @messages ||= WhatsappSdk::Api::Messages.new
36
+ end
37
+
38
+ def phone_numbers
39
+ @phone_numbers ||= WhatsappSdk::Api::PhoneNumbers.new
40
+ end
41
+
42
+ def business_profiles
43
+ @business_profiles ||= WhatsappSdk::Api::BusinessProfile.new
44
+ end
45
+
46
+ def templates
47
+ @templates ||= WhatsappSdk::Api::Templates.new
48
+ end
49
+
30
50
  def send_request(endpoint: "", full_url: nil, http_method: "post", params: {}, headers: {}, multipart: false)
31
51
  url = full_url || "#{ApiConfiguration::API_URL}/#{@api_version}/"
32
52
 
@@ -34,6 +54,10 @@ module WhatsappSdk
34
54
 
35
55
  response = faraday_request.public_send(http_method, endpoint, request_params(params, headers), headers)
36
56
 
57
+ if response.status > 499 || Api::Responses::GenericErrorResponse.response_error?(response: response.body)
58
+ raise Api::Responses::HttpResponseError.new(http_status: response.status, body: JSON.parse(response.body))
59
+ end
60
+
37
61
  return nil if response.body == ""
38
62
 
39
63
  JSON.parse(response.body)