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.
- checksums.yaml +4 -4
- data/.cursorrules +53 -0
- data/.github/workflows/codeql-analysis.yml +8 -6
- data/.gitignore +1 -0
- data/.rubocop.yml +0 -3
- data/CHANGELOG.md +19 -7
- data/Gemfile +1 -4
- data/Gemfile.lock +9 -23
- data/README.md +148 -223
- data/SECURITY.md +21 -0
- data/example.rb +217 -125
- data/lib/whatsapp_sdk/api/api_configuration.rb +2 -5
- data/lib/whatsapp_sdk/api/business_profile.rb +12 -26
- data/lib/whatsapp_sdk/api/client.rb +31 -43
- data/lib/whatsapp_sdk/api/medias.rb +31 -46
- data/lib/whatsapp_sdk/api/messages.rb +28 -142
- data/lib/whatsapp_sdk/api/phone_numbers.rb +32 -36
- data/lib/whatsapp_sdk/api/request.rb +2 -2
- data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +12 -30
- data/lib/whatsapp_sdk/api/responses/http_response_error.rb +18 -0
- data/lib/whatsapp_sdk/api/responses/id_response.rb +15 -0
- data/lib/whatsapp_sdk/api/responses/message_data_response.rb +18 -37
- data/lib/whatsapp_sdk/api/responses/pagination_records.rb +17 -0
- data/lib/whatsapp_sdk/api/responses/success_response.rb +3 -20
- data/lib/whatsapp_sdk/api/templates.rb +30 -63
- data/lib/whatsapp_sdk/configuration.rb +2 -24
- data/lib/whatsapp_sdk/error.rb +0 -1
- data/lib/whatsapp_sdk/resource/address.rb +1 -30
- data/lib/whatsapp_sdk/resource/address_type.rb +3 -8
- data/lib/whatsapp_sdk/resource/business_profile.rb +28 -3
- data/lib/whatsapp_sdk/resource/button_parameter.rb +4 -20
- data/lib/whatsapp_sdk/resource/component.rb +12 -43
- data/lib/whatsapp_sdk/resource/contact.rb +1 -30
- data/lib/whatsapp_sdk/resource/contact_response.rb +1 -9
- data/lib/whatsapp_sdk/resource/currency.rb +0 -8
- data/lib/whatsapp_sdk/resource/date_time.rb +0 -6
- data/lib/whatsapp_sdk/resource/email.rb +1 -10
- data/lib/whatsapp_sdk/resource/errors.rb +3 -17
- data/lib/whatsapp_sdk/resource/interactive.rb +6 -27
- data/lib/whatsapp_sdk/resource/interactive_action.rb +10 -24
- data/lib/whatsapp_sdk/resource/interactive_action_reply_button.rb +5 -19
- data/lib/whatsapp_sdk/resource/interactive_action_section.rb +1 -11
- data/lib/whatsapp_sdk/resource/interactive_action_section_row.rb +0 -11
- data/lib/whatsapp_sdk/resource/interactive_body.rb +0 -10
- data/lib/whatsapp_sdk/resource/interactive_footer.rb +0 -10
- data/lib/whatsapp_sdk/resource/interactive_header.rb +25 -53
- data/lib/whatsapp_sdk/resource/location.rb +44 -0
- data/lib/whatsapp_sdk/resource/media.rb +28 -114
- data/lib/whatsapp_sdk/resource/media_component.rb +89 -0
- data/lib/whatsapp_sdk/resource/media_types.rb +0 -3
- data/lib/whatsapp_sdk/resource/message.rb +0 -5
- data/lib/whatsapp_sdk/resource/message_template_namespace.rb +17 -0
- data/lib/whatsapp_sdk/resource/name.rb +1 -28
- data/lib/whatsapp_sdk/resource/org.rb +1 -13
- data/lib/whatsapp_sdk/resource/parameter_object.rb +71 -74
- data/lib/whatsapp_sdk/resource/phone_number.rb +40 -22
- data/lib/whatsapp_sdk/resource/phone_number_component.rb +25 -0
- data/lib/whatsapp_sdk/resource/template.rb +43 -36
- data/lib/whatsapp_sdk/resource/url.rb +1 -10
- data/lib/whatsapp_sdk/version.rb +1 -2
- data/lib/whatsapp_sdk.rb +0 -4
- data/whatsapp_sdk.gemspec +1 -4
- metadata +13 -62
- data/lib/whatsapp_sdk/api/response.rb +0 -51
- data/lib/whatsapp_sdk/api/responses/business_profile_data_response.rb +0 -56
- data/lib/whatsapp_sdk/api/responses/data_response.rb +0 -26
- data/lib/whatsapp_sdk/api/responses/error_response.rb +0 -32
- data/lib/whatsapp_sdk/api/responses/media_data_response.rb +0 -48
- data/lib/whatsapp_sdk/api/responses/message_error_response.rb +0 -13
- data/lib/whatsapp_sdk/api/responses/message_template_namespace_data_response.rb +0 -37
- data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +0 -60
- data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +0 -39
- data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +0 -34
- data/lib/whatsapp_sdk/api/responses/template_data_response.rb +0 -51
- data/lib/whatsapp_sdk/api/responses/templates_data_response.rb +0 -39
- data/sorbet/config +0 -6
- data/sorbet/rbi/annotations/faraday.rbi +0 -17
- data/sorbet/rbi/annotations/mocha.rbi +0 -34
- data/sorbet/rbi/annotations/rainbow.rbi +0 -269
- data/sorbet/rbi/gems/faraday-multipart@1.0.4.rbi +0 -270
- data/sorbet/rbi/gems/faraday-net_http@2.0.3.rbi +0 -182
- data/sorbet/rbi/gems/faraday@2.3.0.rbi +0 -2494
- data/sorbet/rbi/gems/method_source@1.0.0.rbi +0 -272
- data/sorbet/rbi/gems/minitest@5.16.1.rbi +0 -1459
- data/sorbet/rbi/gems/mocha@1.14.0.rbi +0 -60
- data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +0 -239
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +0 -150
- data/sorbet/rbi/gems/oj@3.13.14.rbi +0 -589
- data/sorbet/rbi/gems/zeitwerk@2.6.0.rbi +0 -867
- data/sorbet/rbi/todo.rbi +0 -8
- data/sorbet/shims/request.rbi +0 -10
- data/sorbet/tapioca/config.yml +0 -13
- data/sorbet/tapioca/require.rb +0 -4
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\
|
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
|
-
|
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
|
53
|
-
|
54
|
-
|
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
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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 =
|
75
|
-
puts "GET template by namespace: #{
|
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 =
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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,161 +124,247 @@ components_json = [
|
|
124
124
|
]
|
125
125
|
}
|
126
126
|
]
|
127
|
-
|
128
|
-
|
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
|
-
|
132
|
-
|
133
|
-
|
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&.id) }"
|
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&.id) }"
|
141
158
|
|
142
159
|
############################## Phone Numbers API ##############################
|
143
|
-
|
144
|
-
|
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
|
-
|
147
|
-
|
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
|
-
|
154
|
-
|
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
|
158
|
-
|
159
|
-
puts "GET
|
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 =
|
163
|
-
puts "Downloaded: #{
|
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 =
|
167
|
-
puts "
|
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
|
-
|
173
|
-
puts "Uploaded
|
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
|
-
|
184
|
-
puts "GET
|
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 =
|
188
|
-
download_image =
|
189
|
-
puts "Download
|
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 =
|
196
|
-
|
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
|
200
|
-
reaction_1_sent =
|
201
|
-
|
202
|
-
|
203
|
-
|
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.
|
207
|
-
reply =
|
208
|
-
|
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
|
-
|
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
|
-
#
|
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
|
223
|
-
image_sent =
|
224
|
-
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link:
|
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
|
-
|
230
|
-
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id:
|
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 =
|
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 =
|
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
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
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 =
|
253
|
-
|
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
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
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 =
|
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
|
|
274
|
-
# Send a template with components
|
358
|
+
# Send a template with components (Remember to create the template first).
|
275
359
|
header_component = WhatsappSdk::Resource::Component.new(
|
276
|
-
type: WhatsappSdk::Resource::Component::Type::
|
360
|
+
type: WhatsappSdk::Resource::Component::Type::HEADER
|
361
|
+
)
|
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")
|
365
|
+
location = WhatsappSdk::Resource::Location.new(
|
366
|
+
latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
|
277
367
|
)
|
278
|
-
|
279
|
-
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
|
280
|
-
document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
|
281
|
-
video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
|
282
368
|
|
283
369
|
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
|
284
370
|
type: "image",
|
@@ -300,14 +386,20 @@ parameter_text = WhatsappSdk::Resource::ParameterObject.new(
|
|
300
386
|
text: "I am a text"
|
301
387
|
)
|
302
388
|
|
389
|
+
parameter_location = WhatsappSdk::Resource::ParameterObject.new(
|
390
|
+
type: "location",
|
391
|
+
location: location
|
392
|
+
)
|
393
|
+
|
303
394
|
header_component.add_parameter(parameter_text)
|
304
395
|
header_component.add_parameter(parameter_image)
|
305
396
|
header_component.add_parameter(parameter_video)
|
306
397
|
header_component.add_parameter(parameter_document)
|
398
|
+
header_component.add_parameter(parameter_location)
|
307
399
|
header_component.to_json
|
308
400
|
|
309
401
|
body_component = WhatsappSdk::Resource::Component.new(
|
310
|
-
type: WhatsappSdk::Resource::Component::Type::
|
402
|
+
type: WhatsappSdk::Resource::Component::Type::BODY
|
311
403
|
)
|
312
404
|
body_component.add_parameter(parameter_text)
|
313
405
|
body_component.add_parameter(parameter_image)
|
@@ -316,23 +408,23 @@ body_component.add_parameter(parameter_document)
|
|
316
408
|
body_component.to_json
|
317
409
|
|
318
410
|
button_component_1 = WhatsappSdk::Resource::Component.new(
|
319
|
-
type: WhatsappSdk::Resource::Component::Type::
|
411
|
+
type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
320
412
|
index: 0,
|
321
|
-
sub_type: WhatsappSdk::Resource::Component::Subtype::
|
322
|
-
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::
|
413
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
414
|
+
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::PAYLOAD,
|
323
415
|
payload: "payload")]
|
324
416
|
)
|
325
417
|
|
326
418
|
button_component_2 = WhatsappSdk::Resource::Component.new(
|
327
|
-
type: WhatsappSdk::Resource::Component::Type::
|
419
|
+
type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
328
420
|
index: 1,
|
329
|
-
sub_type: WhatsappSdk::Resource::Component::Subtype::
|
330
|
-
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::
|
421
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
422
|
+
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::PAYLOAD,
|
331
423
|
payload: "payload")]
|
332
424
|
)
|
333
425
|
|
334
426
|
# Send a template with component_json
|
335
|
-
response_with_json =
|
427
|
+
response_with_json = client.messages.send_template(
|
336
428
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
|
337
429
|
components_json: [
|
338
430
|
{
|
@@ -353,7 +445,7 @@ puts response_with_json
|
|
353
445
|
######### SEND INTERACTIVE MESSAGES
|
354
446
|
## with reply buttons
|
355
447
|
interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
|
356
|
-
type: WhatsappSdk::Resource::InteractiveHeader::Type::
|
448
|
+
type: WhatsappSdk::Resource::InteractiveHeader::Type::TEXT,
|
357
449
|
text: "I'm the header!"
|
358
450
|
)
|
359
451
|
|
@@ -366,7 +458,7 @@ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
|
|
366
458
|
)
|
367
459
|
|
368
460
|
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
|
369
|
-
type: WhatsappSdk::Resource::InteractiveAction::Type::
|
461
|
+
type: WhatsappSdk::Resource::InteractiveAction::Type::REPLY_BUTTON
|
370
462
|
)
|
371
463
|
|
372
464
|
interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
|
@@ -382,14 +474,14 @@ interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton
|
|
382
474
|
interactive_action.add_reply_button(interactive_reply_button_2)
|
383
475
|
|
384
476
|
interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
|
385
|
-
type: WhatsappSdk::Resource::Interactive::Type::
|
477
|
+
type: WhatsappSdk::Resource::Interactive::Type::REPLY_BUTTON,
|
386
478
|
header: interactive_header,
|
387
479
|
body: interactive_body,
|
388
480
|
footer: interactive_footer,
|
389
481
|
action: interactive_action
|
390
482
|
)
|
391
483
|
|
392
|
-
|
484
|
+
client.messages.send_interactive_reply_buttons(
|
393
485
|
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
394
486
|
interactive: interactive_reply_buttons
|
395
487
|
)
|
@@ -1,13 +1,10 @@
|
|
1
|
-
# typed: strict
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
module WhatsappSdk
|
5
4
|
module Api
|
6
5
|
module ApiConfiguration
|
7
|
-
|
8
|
-
|
9
|
-
DEFAULT_API_VERSION = T.let("v19.0", String)
|
10
|
-
API_URL = T.let("https://graph.facebook.com", String)
|
6
|
+
DEFAULT_API_VERSION = "v19.0"
|
7
|
+
API_URL = "https://graph.facebook.com"
|
11
8
|
end
|
12
9
|
end
|
13
10
|
end
|
@@ -1,8 +1,6 @@
|
|
1
|
-
# typed: strict
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require_relative "request"
|
5
|
-
require_relative "response"
|
6
4
|
require_relative "../resource/business_profile"
|
7
5
|
|
8
6
|
module WhatsappSdk
|
@@ -11,12 +9,8 @@ module WhatsappSdk
|
|
11
9
|
DEFAULT_FIELDS = 'about,address,description,email,profile_picture_url,websites,vertical'
|
12
10
|
|
13
11
|
class InvalidVertical < StandardError
|
14
|
-
extend T::Sig
|
15
|
-
|
16
|
-
sig { returns(String) }
|
17
12
|
attr_accessor :message
|
18
13
|
|
19
|
-
sig { params(vertical: String).void }
|
20
14
|
def initialize(vertical:)
|
21
15
|
@message = "invalid vertical #{vertical}. See the supported types in the official documentation " \
|
22
16
|
"https://developers.facebook.com/docs/whatsapp/cloud-api/reference/business-profiles"
|
@@ -27,9 +21,8 @@ module WhatsappSdk
|
|
27
21
|
# Get the details of business profile.
|
28
22
|
#
|
29
23
|
# @param phone_number_id [Integer] Phone Number Id.
|
30
|
-
# @return [
|
31
|
-
|
32
|
-
def details(phone_number_id, fields: nil)
|
24
|
+
# @return [Resource::BusinessProfile] Response object.
|
25
|
+
def get(phone_number_id, fields: nil)
|
33
26
|
fields = if fields
|
34
27
|
fields.join(',')
|
35
28
|
else
|
@@ -41,25 +34,22 @@ module WhatsappSdk
|
|
41
34
|
endpoint: "#{phone_number_id}/whatsapp_business_profile?fields=#{fields}"
|
42
35
|
)
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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)
|
48
44
|
end
|
49
45
|
|
50
46
|
# Update the details of business profile.
|
51
47
|
#
|
52
48
|
# @param phone_number_id [Integer] Phone Number Id.
|
53
49
|
# @param params [Hash] Params to update.
|
54
|
-
# @return [
|
55
|
-
sig do
|
56
|
-
params(
|
57
|
-
phone_number_id: Integer, params: T::Hash[T.untyped, T.untyped]
|
58
|
-
).returns(Api::Response)
|
59
|
-
end
|
50
|
+
# @return [Boolean] Whether the update was successful.
|
60
51
|
def update(phone_number_id:, params:)
|
61
|
-
#
|
62
|
-
params[:messaging_product] = 'whatsapp'
|
52
|
+
params[:messaging_product] = 'whatsapp' # messaging_products is a required field
|
63
53
|
return raise InvalidVertical.new(vertical: params[:vertical]) unless valid_vertical?(params)
|
64
54
|
|
65
55
|
response = send_request(
|
@@ -68,15 +58,11 @@ module WhatsappSdk
|
|
68
58
|
params: params
|
69
59
|
)
|
70
60
|
|
71
|
-
Api::
|
72
|
-
response: response,
|
73
|
-
data_class_type: Api::Responses::SuccessResponse
|
74
|
-
)
|
61
|
+
Api::Responses::SuccessResponse.success_response?(response: response)
|
75
62
|
end
|
76
63
|
|
77
64
|
private
|
78
65
|
|
79
|
-
sig { params(params: T::Hash[T.untyped, T.untyped]).returns(T::Boolean) }
|
80
66
|
def valid_vertical?(params)
|
81
67
|
return true unless params[:vertical]
|
82
68
|
|