whatsapp_sdk 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/Gemfile.lock +9 -8
- data/README.md +192 -0
- data/lib/whatsapp_sdk/api/business_account.rb +15 -0
- data/lib/whatsapp_sdk/api/client.rb +118 -17
- data/lib/whatsapp_sdk/api/flows.rb +95 -0
- data/lib/whatsapp_sdk/api/medias.rb +46 -0
- data/lib/whatsapp_sdk/api/messages.rb +123 -34
- data/lib/whatsapp_sdk/api/phone_numbers.rb +83 -2
- data/lib/whatsapp_sdk/api/request.rb +4 -2
- data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +4 -0
- data/lib/whatsapp_sdk/api/responses/message_data_response.rb +4 -2
- data/lib/whatsapp_sdk/api/templates.rb +9 -2
- data/lib/whatsapp_sdk/configuration.rb +34 -4
- data/lib/whatsapp_sdk/resource/business_account.rb +36 -0
- data/lib/whatsapp_sdk/resource/contact_response.rb +7 -2
- data/lib/whatsapp_sdk/resource/errors.rb +3 -0
- data/lib/whatsapp_sdk/resource/message.rb +5 -1
- data/lib/whatsapp_sdk/resource/parameter_object.rb +4 -0
- data/lib/whatsapp_sdk/resource/template_analytic.rb +18 -0
- data/lib/whatsapp_sdk/version.rb +1 -1
- data/whatsapp_sdk.gemspec +1 -1
- metadata +6 -8
|
@@ -117,6 +117,52 @@ module WhatsappSdk
|
|
|
117
117
|
Api::Responses::SuccessResponse.success_response?(response: response)
|
|
118
118
|
end
|
|
119
119
|
|
|
120
|
+
# Create a Graph upload session for template media or a business profile photo.
|
|
121
|
+
# Returns the raw response containing the upload session "id".
|
|
122
|
+
# Uses the client's API version and token unless access_token is supplied.
|
|
123
|
+
#
|
|
124
|
+
# @param app_id [String, Integer] Meta app ID that owns the upload session.
|
|
125
|
+
# @param file_path [String] Path to the local file whose name and size are sent.
|
|
126
|
+
# @param type [String] File MIME type, such as image/jpeg.
|
|
127
|
+
# @param access_token [String, nil] Token override; nil uses the client's token.
|
|
128
|
+
# @return [Hash] Raw Graph response containing the upload session "id".
|
|
129
|
+
# @raise [FileNotFoundError] If file_path does not identify a regular file.
|
|
130
|
+
# @raise [Api::Responses::HttpResponseError] If Graph returns an API error or a server error.
|
|
131
|
+
# @raise [SystemCallError] If the file metadata cannot be read.
|
|
132
|
+
def create_upload_session(app_id:, file_path:, type:, access_token: nil)
|
|
133
|
+
raise FileNotFoundError.new(file_path: file_path) unless File.file?(file_path)
|
|
134
|
+
|
|
135
|
+
headers = { 'Content-Type' => 'application/json' }
|
|
136
|
+
headers['Authorization'] = "Bearer #{access_token}" if access_token
|
|
137
|
+
send_request(
|
|
138
|
+
endpoint: "./#{app_id}/uploads",
|
|
139
|
+
params: { file_name: File.basename(file_path), file_length: File.size(file_path), file_type: type },
|
|
140
|
+
headers: headers
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Upload the complete file to a Graph upload session, starting at offset zero.
|
|
145
|
+
# Returns the raw response containing the reusable media handle "h".
|
|
146
|
+
# This does not resume partial uploads or retry failed requests.
|
|
147
|
+
#
|
|
148
|
+
# @param session_id [String] Upload session ID returned by create_upload_session, including any signature.
|
|
149
|
+
# @param file_path [String] Path to the local file to upload in full.
|
|
150
|
+
# @param access_token [String, nil] Token override; nil uses the client's token.
|
|
151
|
+
# @return [Hash] Raw Graph response containing the reusable media handle "h".
|
|
152
|
+
# @raise [FileNotFoundError] If file_path does not identify a regular file.
|
|
153
|
+
# @raise [Api::Responses::HttpResponseError] If Graph returns an API error or a server error.
|
|
154
|
+
# @raise [SystemCallError] If the file cannot be read.
|
|
155
|
+
def upload_file_to_session(session_id:, file_path:, access_token: nil)
|
|
156
|
+
raise FileNotFoundError.new(file_path: file_path) unless File.file?(file_path)
|
|
157
|
+
|
|
158
|
+
headers = { 'Content-Type' => 'application/octet-stream', 'file_offset' => '0' }
|
|
159
|
+
headers['Authorization'] = "Bearer #{access_token}" if access_token
|
|
160
|
+
File.open(file_path, 'rb') do |file|
|
|
161
|
+
headers['Content-Length'] = file.size.to_s
|
|
162
|
+
send_request(endpoint: "./#{session_id}", params: file, headers: headers)
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
120
166
|
private
|
|
121
167
|
|
|
122
168
|
def map_media_type_to_content_type_header(media_type)
|
|
@@ -13,8 +13,9 @@ module WhatsappSdk
|
|
|
13
13
|
# @param recipient_number [Integer] Recipient' Phone number.
|
|
14
14
|
# @param message [String] Text to send.
|
|
15
15
|
# @param message_id [String] The id of the message to reply to.
|
|
16
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
16
17
|
# @return [MessageDataResponse] Response object.
|
|
17
|
-
def send_text(sender_id:,
|
|
18
|
+
def send_text(sender_id:, message:, recipient_number: nil, message_id: nil, recipient: nil)
|
|
18
19
|
params = {
|
|
19
20
|
messaging_product: "whatsapp",
|
|
20
21
|
to: recipient_number,
|
|
@@ -23,6 +24,7 @@ module WhatsappSdk
|
|
|
23
24
|
text: { body: message }
|
|
24
25
|
}
|
|
25
26
|
params[:context] = { message_id: message_id } if message_id
|
|
27
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
26
28
|
|
|
27
29
|
response = send_request(
|
|
28
30
|
endpoint: endpoint(sender_id),
|
|
@@ -42,9 +44,10 @@ module WhatsappSdk
|
|
|
42
44
|
# @param name [String] Location name.
|
|
43
45
|
# @param address [String] Location address.
|
|
44
46
|
# @param message_id [String] The id of the message to reply to.
|
|
47
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
45
48
|
# @return [MessageDataResponse] Response object.
|
|
46
49
|
def send_location(
|
|
47
|
-
sender_id:,
|
|
50
|
+
sender_id:, longitude:, latitude:, name:, address:, recipient_number: nil, message_id: nil, recipient: nil
|
|
48
51
|
)
|
|
49
52
|
params = {
|
|
50
53
|
messaging_product: "whatsapp",
|
|
@@ -59,6 +62,7 @@ module WhatsappSdk
|
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
64
|
params[:context] = { message_id: message_id } if message_id
|
|
65
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
62
66
|
|
|
63
67
|
response = send_request(
|
|
64
68
|
endpoint: endpoint(sender_id),
|
|
@@ -77,9 +81,10 @@ module WhatsappSdk
|
|
|
77
81
|
# @param link [String] Image link.
|
|
78
82
|
# @param caption [String] Image caption.
|
|
79
83
|
# @param message_id [String] The id of the message to reply to.
|
|
84
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
80
85
|
# @return [MessageDataResponse] Response object.
|
|
81
86
|
def send_image(
|
|
82
|
-
sender_id:, recipient_number
|
|
87
|
+
sender_id:, recipient_number: nil, image_id: nil, link: nil, caption: "", message_id: nil, recipient: nil
|
|
83
88
|
)
|
|
84
89
|
raise Resource::Errors::MissingArgumentError, "image_id or link is required" if !image_id && !link
|
|
85
90
|
|
|
@@ -95,6 +100,7 @@ module WhatsappSdk
|
|
|
95
100
|
{ id: image_id, caption: caption }
|
|
96
101
|
end
|
|
97
102
|
params[:context] = { message_id: message_id } if message_id
|
|
103
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
98
104
|
|
|
99
105
|
response = send_request(
|
|
100
106
|
endpoint: endpoint(sender_id),
|
|
@@ -112,8 +118,12 @@ module WhatsappSdk
|
|
|
112
118
|
# @param audio_id [String] Audio ID.
|
|
113
119
|
# @param link [String] Audio link.
|
|
114
120
|
# @param message_id [String] The id of the message to reply to.
|
|
121
|
+
# @param voice [Boolean] Send an Ogg/Opus voice message instead of basic audio. Defaults to false.
|
|
122
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
115
123
|
# @return [MessageDataResponse] Response object.
|
|
116
|
-
def send_audio(
|
|
124
|
+
def send_audio(
|
|
125
|
+
sender_id:, recipient_number: nil, audio_id: nil, link: nil, message_id: nil, voice: false, recipient: nil
|
|
126
|
+
)
|
|
117
127
|
raise Resource::Errors::MissingArgumentError, "audio_id or link is required" if !audio_id && !link
|
|
118
128
|
|
|
119
129
|
params = {
|
|
@@ -122,8 +132,9 @@ module WhatsappSdk
|
|
|
122
132
|
recipient_type: "individual",
|
|
123
133
|
type: "audio"
|
|
124
134
|
}
|
|
125
|
-
params[:audio] = link ? { link: link } : { id: audio_id }
|
|
135
|
+
params[:audio] = link ? { link: link, voice: voice } : { id: audio_id, voice: voice }
|
|
126
136
|
params[:context] = { message_id: message_id } if message_id
|
|
137
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
127
138
|
|
|
128
139
|
response = send_request(
|
|
129
140
|
endpoint: endpoint(sender_id),
|
|
@@ -143,9 +154,10 @@ module WhatsappSdk
|
|
|
143
154
|
# @param link [String] Image link.
|
|
144
155
|
# @param caption [String] Image caption.
|
|
145
156
|
# @param message_id [String] The id of the message to reply to.
|
|
157
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
146
158
|
# @return [MessageDataResponse] Response object.
|
|
147
159
|
def send_video(
|
|
148
|
-
sender_id:, recipient_number
|
|
160
|
+
sender_id:, recipient_number: nil, video_id: nil, link: nil, caption: "", message_id: nil, recipient: nil
|
|
149
161
|
)
|
|
150
162
|
raise Resource::Errors::MissingArgumentError, "video_id or link is required" if !video_id && !link
|
|
151
163
|
|
|
@@ -161,6 +173,7 @@ module WhatsappSdk
|
|
|
161
173
|
{ id: video_id, caption: caption }
|
|
162
174
|
end
|
|
163
175
|
params[:context] = { message_id: message_id } if message_id
|
|
176
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
164
177
|
|
|
165
178
|
response = send_request(
|
|
166
179
|
endpoint: endpoint(sender_id),
|
|
@@ -179,9 +192,11 @@ module WhatsappSdk
|
|
|
179
192
|
# @param link [String] Image link.
|
|
180
193
|
# @param caption [String] Image caption.
|
|
181
194
|
# @param message_id [String] The id of the message to reply to.
|
|
195
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
182
196
|
# @return [MessageDataResponse] Response object.
|
|
183
197
|
def send_document(
|
|
184
|
-
sender_id:, recipient_number
|
|
198
|
+
sender_id:, recipient_number: nil, document_id: nil, link: nil, caption: "", message_id: nil,
|
|
199
|
+
filename: nil, recipient: nil
|
|
185
200
|
)
|
|
186
201
|
if !document_id && !link
|
|
187
202
|
raise Resource::Errors::MissingArgumentError,
|
|
@@ -201,6 +216,7 @@ module WhatsappSdk
|
|
|
201
216
|
end
|
|
202
217
|
params[:document] = params[:document].merge({ filename: filename }) if filename
|
|
203
218
|
params[:context] = { message_id: message_id } if message_id
|
|
219
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
204
220
|
|
|
205
221
|
response = send_request(
|
|
206
222
|
endpoint: endpoint(sender_id),
|
|
@@ -211,15 +227,16 @@ module WhatsappSdk
|
|
|
211
227
|
Api::Responses::MessageDataResponse.build_from_response(response: response)
|
|
212
228
|
end
|
|
213
229
|
|
|
214
|
-
# Send a
|
|
230
|
+
# Send a sticker.
|
|
215
231
|
#
|
|
216
232
|
# @param sender_id [Integer] Sender' phone number.
|
|
217
233
|
# @param recipient_number [Integer] Recipient' Phone number.
|
|
218
234
|
# @param sticker_id [String] The sticker ID.
|
|
219
235
|
# @param link [String] Image link.
|
|
220
236
|
# @param message_id [String] The id of the message to reply to.
|
|
237
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
221
238
|
# @return [MessageDataResponse] Response object.
|
|
222
|
-
def send_sticker(sender_id:, recipient_number
|
|
239
|
+
def send_sticker(sender_id:, recipient_number: nil, sticker_id: nil, link: nil, message_id: nil, recipient: nil)
|
|
223
240
|
raise Resource::Errors::MissingArgumentError, "sticker or link is required" if !sticker_id && !link
|
|
224
241
|
|
|
225
242
|
params = {
|
|
@@ -230,6 +247,7 @@ module WhatsappSdk
|
|
|
230
247
|
}
|
|
231
248
|
params[:sticker] = link ? { link: link } : { id: sticker_id }
|
|
232
249
|
params[:context] = { message_id: message_id } if message_id
|
|
250
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
233
251
|
|
|
234
252
|
response = send_request(
|
|
235
253
|
endpoint: endpoint(sender_id),
|
|
@@ -248,9 +266,10 @@ module WhatsappSdk
|
|
|
248
266
|
# @param contacts [Array<Contact>] Contacts.
|
|
249
267
|
# @param contacts_json [Json] Contacts.
|
|
250
268
|
# @param message_id [String] The id of the message to reply to.
|
|
269
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
251
270
|
# @return [MessageDataResponse] Response object.
|
|
252
271
|
def send_contacts(
|
|
253
|
-
sender_id:, recipient_number
|
|
272
|
+
sender_id:, recipient_number: nil, contacts: nil, contacts_json: {}, message_id: nil, recipient: nil
|
|
254
273
|
)
|
|
255
274
|
params = {
|
|
256
275
|
messaging_product: "whatsapp",
|
|
@@ -260,6 +279,7 @@ module WhatsappSdk
|
|
|
260
279
|
}
|
|
261
280
|
params[:contacts] = contacts ? contacts.map(&:to_h) : contacts_json
|
|
262
281
|
params[:context] = { message_id: message_id } if message_id
|
|
282
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
263
283
|
|
|
264
284
|
response = send_request(
|
|
265
285
|
endpoint: endpoint(sender_id),
|
|
@@ -284,9 +304,10 @@ module WhatsappSdk
|
|
|
284
304
|
# @param interactive_json [Json] The interactive object as a Json.
|
|
285
305
|
# If you pass interactive_json, you can't pass interactive.
|
|
286
306
|
# @param message_id [String] The id of the message to reply to.
|
|
307
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
287
308
|
# @return [MessageDataResponse] Response object.
|
|
288
309
|
def send_interactive_message(
|
|
289
|
-
sender_id:, recipient_number
|
|
310
|
+
sender_id:, recipient_number: nil, interactive: nil, interactive_json: nil, message_id: nil, recipient: nil
|
|
290
311
|
)
|
|
291
312
|
if !interactive && !interactive_json
|
|
292
313
|
raise Resource::Errors::MissingArgumentError,
|
|
@@ -306,6 +327,7 @@ module WhatsappSdk
|
|
|
306
327
|
interactive.to_json
|
|
307
328
|
end
|
|
308
329
|
params[:context] = { message_id: message_id } if message_id
|
|
330
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
309
331
|
|
|
310
332
|
response = send_request(
|
|
311
333
|
endpoint: endpoint(sender_id),
|
|
@@ -348,31 +370,16 @@ module WhatsappSdk
|
|
|
348
370
|
# @param language [String] template language.
|
|
349
371
|
# @param components [Component] Component.
|
|
350
372
|
# @param components_json [Json] The component as a Json. If you pass components_json, you can't pass components.
|
|
373
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
374
|
+
# Not allowed for authentication templates.
|
|
351
375
|
# @return [MessageDataResponse] Response object.
|
|
376
|
+
# @raise [Resource::Errors::MissingArgumentError] If language, components or a destination are missing.
|
|
377
|
+
# @raise [ArgumentError] If both components and components_json are given.
|
|
352
378
|
def send_template(
|
|
353
|
-
sender_id:,
|
|
379
|
+
sender_id:, name:, language:, recipient_number: nil, components: nil, components_json: nil, recipient: nil
|
|
354
380
|
)
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
"components or components_json is required"
|
|
358
|
-
end
|
|
359
|
-
|
|
360
|
-
params = {
|
|
361
|
-
messaging_product: "whatsapp",
|
|
362
|
-
recipient_type: "individual",
|
|
363
|
-
to: recipient_number,
|
|
364
|
-
type: "template",
|
|
365
|
-
template: {
|
|
366
|
-
name: name
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
params[:template][:language] = { code: language } if language
|
|
371
|
-
params[:template][:components] = if components.nil?
|
|
372
|
-
components_json
|
|
373
|
-
else
|
|
374
|
-
components.map(&:to_json)
|
|
375
|
-
end
|
|
381
|
+
params = template_params(name: name, language: language, recipient_number: recipient_number,
|
|
382
|
+
components: components, components_json: components_json, recipient: recipient)
|
|
376
383
|
|
|
377
384
|
response = send_request(
|
|
378
385
|
endpoint: endpoint(sender_id),
|
|
@@ -383,14 +390,44 @@ module WhatsappSdk
|
|
|
383
390
|
Api::Responses::MessageDataResponse.build_from_response(response: response)
|
|
384
391
|
end
|
|
385
392
|
|
|
393
|
+
# Send an approved marketing template via Marketing Messages API.
|
|
394
|
+
# @param sender_id [String, Integer] Business phone number ID.
|
|
395
|
+
# @param name [String] Approved marketing template name.
|
|
396
|
+
# @param language [String] Template language code.
|
|
397
|
+
# @param recipient_number [String, Integer, nil] Phone number; takes precedence over recipient.
|
|
398
|
+
# @param components [Array<Resource::Component>, nil] Template component objects.
|
|
399
|
+
# @param components_json [Array<Hash>, nil] Raw components; use [] for a template without parameters.
|
|
400
|
+
# @param recipient [String, nil] BSUID or parent BSUID, used when the phone number is omitted.
|
|
401
|
+
# @param product_policy [String, Symbol, nil] STRICT or CLOUD_API_FALLBACK (any case); nil leaves it to Meta.
|
|
402
|
+
# @param message_activity_sharing [Boolean, nil] Turns message activity sharing on or off; nil leaves it to Meta.
|
|
403
|
+
# @return [Api::Responses::MessageDataResponse] IDs, contacts, and any message_status returned by Meta.
|
|
404
|
+
# @raise [Resource::Errors::MissingArgumentError] If language, components or a destination are missing.
|
|
405
|
+
# @raise [ArgumentError] If both components and components_json are given.
|
|
406
|
+
# @raise [Api::Responses::HttpResponseError] If Meta rejects the message.
|
|
407
|
+
def send_marketing_template(
|
|
408
|
+
sender_id:, name:, language:, recipient_number: nil, components: nil, components_json: nil, recipient: nil,
|
|
409
|
+
product_policy: nil, message_activity_sharing: nil, raw_response: false
|
|
410
|
+
)
|
|
411
|
+
params = template_params(name: name, language: language, recipient_number: recipient_number,
|
|
412
|
+
components: components, components_json: components_json, recipient: recipient)
|
|
413
|
+
params[:product_policy] = product_policy.to_s.upcase unless product_policy.nil?
|
|
414
|
+
params[:message_activity_sharing] = message_activity_sharing unless message_activity_sharing.nil?
|
|
415
|
+
response = send_request(endpoint: marketing_endpoint(sender_id), params: params, headers: DEFAULT_HEADERS,
|
|
416
|
+
**(raw_response ? { raw_response: true } : {}))
|
|
417
|
+
return response if raw_response
|
|
418
|
+
|
|
419
|
+
Api::Responses::MessageDataResponse.build_from_response(response: response)
|
|
420
|
+
end
|
|
421
|
+
|
|
386
422
|
# Send reaction
|
|
387
423
|
#
|
|
388
424
|
# @param sender_id [Integer] Sender' phone number.
|
|
389
425
|
# @param recipient_number [Integer] Recipient' Phone number.
|
|
390
426
|
# @param message_id [String] the id of the message to reaction.
|
|
391
427
|
# @param emoji [String] unicode of the emoji to send.
|
|
428
|
+
# @param recipient [String] Recipient' Business-Scoped User ID (BSUID). Optional alternative to recipient_number.
|
|
392
429
|
# @return [MessageDataResponse] Response object.
|
|
393
|
-
def send_reaction(sender_id:,
|
|
430
|
+
def send_reaction(sender_id:, message_id:, emoji:, recipient_number: nil, recipient: nil)
|
|
394
431
|
params = {
|
|
395
432
|
messaging_product: "whatsapp",
|
|
396
433
|
recipient_type: "individual",
|
|
@@ -401,6 +438,7 @@ module WhatsappSdk
|
|
|
401
438
|
emoji: emoji
|
|
402
439
|
}
|
|
403
440
|
}
|
|
441
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
404
442
|
|
|
405
443
|
response = send_request(
|
|
406
444
|
endpoint: endpoint(sender_id),
|
|
@@ -438,9 +476,60 @@ module WhatsappSdk
|
|
|
438
476
|
|
|
439
477
|
private
|
|
440
478
|
|
|
479
|
+
def template_params(name:, language:, recipient_number:, components:, components_json:, recipient:)
|
|
480
|
+
validate_template_args!(language, components, components_json)
|
|
481
|
+
|
|
482
|
+
params = {
|
|
483
|
+
messaging_product: "whatsapp",
|
|
484
|
+
recipient_type: "individual",
|
|
485
|
+
to: recipient_number,
|
|
486
|
+
type: "template",
|
|
487
|
+
template: {
|
|
488
|
+
name: name
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
params[:template][:language] = { code: language }
|
|
493
|
+
params[:template][:components] = if components.nil?
|
|
494
|
+
components_json
|
|
495
|
+
else
|
|
496
|
+
components.map(&:to_json)
|
|
497
|
+
end
|
|
498
|
+
apply_recipient!(params, recipient_number, recipient)
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
def validate_template_args!(language, components, components_json)
|
|
502
|
+
if !components && !components_json
|
|
503
|
+
raise Resource::Errors::MissingArgumentError,
|
|
504
|
+
"components or components_json is required"
|
|
505
|
+
end
|
|
506
|
+
raise ArgumentError, "pass components or components_json, not both" if components && components_json
|
|
507
|
+
raise Resource::Errors::MissingArgumentError, "language is required" if language.to_s.strip.empty?
|
|
508
|
+
end
|
|
509
|
+
|
|
441
510
|
def endpoint(sender_id)
|
|
442
511
|
"#{sender_id}/messages"
|
|
443
512
|
end
|
|
513
|
+
|
|
514
|
+
def marketing_endpoint(sender_id)
|
|
515
|
+
"#{sender_id}/marketing_messages"
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
# Applies the destination to the payload.
|
|
519
|
+
# - Phone send (recipient_number present): keeps `to`, never adds `recipient` — payload is
|
|
520
|
+
# identical to the pre-BSUID behavior.
|
|
521
|
+
# - BSUID send (recipient_number absent, recipient present): drops `to` and sets `recipient`
|
|
522
|
+
# (the field Meta uses to address a Business-Scoped User ID).
|
|
523
|
+
# Requires at least one destination.
|
|
524
|
+
def apply_recipient!(params, recipient_number, recipient)
|
|
525
|
+
if (recipient_number.nil? ? recipient : recipient_number).to_s.strip.empty?
|
|
526
|
+
raise Resource::Errors::MissingArgumentError, "recipient_number or recipient is required"
|
|
527
|
+
end
|
|
528
|
+
|
|
529
|
+
params.delete(:to) if recipient_number.nil?
|
|
530
|
+
params[:recipient] = recipient if recipient_number.nil? && recipient
|
|
531
|
+
params
|
|
532
|
+
end
|
|
444
533
|
end
|
|
445
534
|
end
|
|
446
535
|
end
|
|
@@ -41,17 +41,74 @@ module WhatsappSdk
|
|
|
41
41
|
Resource::PhoneNumber.from_hash(response)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
|
+
# Add a phone number to a WABA. Verification and registration are separate calls.
|
|
45
|
+
# @param business_id [String, Integer] WhatsApp Business Account ID.
|
|
46
|
+
# @param country_code [String] Country calling code, without the plus sign.
|
|
47
|
+
# @param phone_number [String] Phone number forwarded unchanged to Graph.
|
|
48
|
+
# @param verified_name [String] Business display name submitted for review.
|
|
49
|
+
# @return [Responses::IdResponse] Created phone number ID.
|
|
50
|
+
# @raise [Responses::HttpResponseError] If Graph rejects the request.
|
|
51
|
+
def add(business_id:, country_code:, phone_number:, verified_name:, raw_response: false)
|
|
52
|
+
response = send_request(
|
|
53
|
+
endpoint: "#{business_id}/phone_numbers",
|
|
54
|
+
params: { cc: country_code, phone_number: phone_number, verified_name: verified_name },
|
|
55
|
+
headers: { 'Content-Type' => 'application/json' },
|
|
56
|
+
**(raw_response ? { raw_response: true } : {})
|
|
57
|
+
)
|
|
58
|
+
return response if raw_response
|
|
59
|
+
|
|
60
|
+
Responses::IdResponse.new(response['id'])
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Request a verification code without automatically verifying or registering the number.
|
|
64
|
+
# @param phone_number_id [String, Integer] Phone number ID.
|
|
65
|
+
# @param code_method [String] SMS or VOICE.
|
|
66
|
+
# @param language [String] Locale for code delivery, such as en_US.
|
|
67
|
+
# @return [Boolean] Whether Graph accepted the request.
|
|
68
|
+
# @raise [ArgumentError] If code_method is unsupported.
|
|
69
|
+
# @raise [Responses::HttpResponseError] If Graph rejects the request.
|
|
70
|
+
def request_code(phone_number_id:, code_method:, language:, raw_response: false)
|
|
71
|
+
raise ArgumentError, 'code_method must be SMS or VOICE' unless %w[SMS VOICE].include?(code_method)
|
|
72
|
+
|
|
73
|
+
response = send_request(
|
|
74
|
+
endpoint: "#{phone_number_id}/request_code", params: { code_method: code_method, language: language },
|
|
75
|
+
headers: { 'Content-Type' => 'application/json' },
|
|
76
|
+
**(raw_response ? { raw_response: true } : {})
|
|
77
|
+
)
|
|
78
|
+
return response if raw_response
|
|
79
|
+
|
|
80
|
+
Responses::SuccessResponse.success_response?(response: response)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Verify ownership of a phone number. Register it separately with register_number.
|
|
84
|
+
# @param phone_number_id [String, Integer] Phone number ID.
|
|
85
|
+
# @param code [String] Verification code; pass a string to preserve leading zeroes.
|
|
86
|
+
# @return [Boolean] Whether verification succeeded.
|
|
87
|
+
# @raise [Responses::HttpResponseError] If Graph rejects the request.
|
|
88
|
+
def verify_code(phone_number_id:, code:, raw_response: false)
|
|
89
|
+
response = send_request(
|
|
90
|
+
endpoint: "#{phone_number_id}/verify_code", params: { code: code },
|
|
91
|
+
headers: { 'Content-Type' => 'application/json' },
|
|
92
|
+
**(raw_response ? { raw_response: true } : {})
|
|
93
|
+
)
|
|
94
|
+
return response if raw_response
|
|
95
|
+
|
|
96
|
+
Responses::SuccessResponse.success_response?(response: response)
|
|
97
|
+
end
|
|
98
|
+
|
|
44
99
|
# Register a phone number.
|
|
45
100
|
#
|
|
46
101
|
# @param phone_number_id [Integer] The registered number we want to retrieve.
|
|
47
102
|
# @param pin [Integer] Pin of 6 digits.
|
|
48
103
|
# @return [Boolean] Whether the registration was successful.
|
|
49
|
-
def register_number(phone_number_id, pin)
|
|
104
|
+
def register_number(phone_number_id, pin, raw_response: false)
|
|
50
105
|
response = send_request(
|
|
51
106
|
http_method: "post",
|
|
52
107
|
endpoint: "#{phone_number_id}/register",
|
|
53
|
-
params: { messaging_product: 'whatsapp', pin: pin }
|
|
108
|
+
params: { messaging_product: 'whatsapp', pin: pin },
|
|
109
|
+
**(raw_response ? { raw_response: true } : {})
|
|
54
110
|
)
|
|
111
|
+
return response if raw_response
|
|
55
112
|
|
|
56
113
|
Api::Responses::SuccessResponse.success_response?(response: response)
|
|
57
114
|
end
|
|
@@ -70,6 +127,30 @@ module WhatsappSdk
|
|
|
70
127
|
Api::Responses::SuccessResponse.success_response?(response: response)
|
|
71
128
|
end
|
|
72
129
|
|
|
130
|
+
# Configure the public key used for Flow data exchange on this phone number.
|
|
131
|
+
# @param phone_number_id [String, Integer] Business phone number ID.
|
|
132
|
+
# @param business_public_key [String] PEM-encoded 2048-bit RSA public key, including newlines.
|
|
133
|
+
# @return [Boolean] Whether the key was accepted.
|
|
134
|
+
# @raise [Responses::HttpResponseError] If Graph rejects the request.
|
|
135
|
+
def set_public_key(phone_number_id:, business_public_key:, raw_response: false)
|
|
136
|
+
response = send_request(
|
|
137
|
+
endpoint: "#{phone_number_id}/whatsapp_business_encryption",
|
|
138
|
+
params: { business_public_key: business_public_key },
|
|
139
|
+
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' },
|
|
140
|
+
**(raw_response ? { raw_response: true } : {})
|
|
141
|
+
)
|
|
142
|
+
return response if raw_response
|
|
143
|
+
|
|
144
|
+
Responses::SuccessResponse.success_response?(response: response)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# @param phone_number_id [String, Integer] Business phone number ID.
|
|
148
|
+
# @return [Hash] Raw Graph response; public key and signature status are nested in the data array.
|
|
149
|
+
# @raise [Responses::HttpResponseError] If Graph rejects the request.
|
|
150
|
+
def get_public_key(phone_number_id:)
|
|
151
|
+
send_request(endpoint: "#{phone_number_id}/whatsapp_business_encryption", http_method: 'get')
|
|
152
|
+
end
|
|
153
|
+
|
|
73
154
|
# deprecated methods
|
|
74
155
|
def registered_numbers(business_id)
|
|
75
156
|
warn "[DEPRECATION] `registered_numbers` is deprecated. Please use `list` instead."
|
|
@@ -14,14 +14,16 @@ module WhatsappSdk
|
|
|
14
14
|
@client.download_file(url: url, content_type_header: content_type_header, file_path: file_path)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def send_request(endpoint: nil, full_url: nil, http_method: "post", params: {}, headers: {}, multipart: false
|
|
17
|
+
def send_request(endpoint: nil, full_url: nil, http_method: "post", params: {}, headers: {}, multipart: false,
|
|
18
|
+
raw_response: false)
|
|
18
19
|
@client.send_request(
|
|
19
20
|
http_method: http_method,
|
|
20
21
|
full_url: full_url,
|
|
21
22
|
endpoint: endpoint,
|
|
22
23
|
params: params,
|
|
23
24
|
headers: headers,
|
|
24
|
-
multipart: multipart
|
|
25
|
+
multipart: multipart,
|
|
26
|
+
**(raw_response ? { raw_response: true } : {})
|
|
25
27
|
)
|
|
26
28
|
end
|
|
27
29
|
end
|
|
@@ -16,10 +16,14 @@ module WhatsappSdk
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def self.response_error?(response:)
|
|
19
|
+
return false unless response.is_a?(Hash)
|
|
20
|
+
|
|
19
21
|
response["error"]
|
|
20
22
|
end
|
|
21
23
|
|
|
22
24
|
def self.build_from_response(response:)
|
|
25
|
+
return unless response.is_a?(Hash)
|
|
26
|
+
|
|
23
27
|
error_response = response["error"]
|
|
24
28
|
return unless error_response
|
|
25
29
|
|
|
@@ -23,11 +23,13 @@ module WhatsappSdk
|
|
|
23
23
|
private
|
|
24
24
|
|
|
25
25
|
def parse_message(message_json)
|
|
26
|
-
Resource::Message.new(id: message_json["id"])
|
|
26
|
+
Resource::Message.new(id: message_json["id"], message_status: message_json["message_status"])
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def parse_contact(contact_json)
|
|
30
|
-
Resource::ContactResponse.new(
|
|
30
|
+
Resource::ContactResponse.new(
|
|
31
|
+
input: contact_json["input"], wa_id: contact_json["wa_id"], user_id: contact_json["user_id"]
|
|
32
|
+
)
|
|
31
33
|
end
|
|
32
34
|
end
|
|
33
35
|
end
|
|
@@ -83,10 +83,12 @@ module WhatsappSdk
|
|
|
83
83
|
#
|
|
84
84
|
# @param business_id [Integer] The business ID.
|
|
85
85
|
# @param limit [Integer] Optional. Number of templates to return in a single page.
|
|
86
|
+
# @param after [String, nil] Optional. Cursor from the previous page; nil requests the first page.
|
|
86
87
|
# @return [Template] Pagination Record containing an array of Template objects.
|
|
87
|
-
def list(business_id:, limit: 100)
|
|
88
|
+
def list(business_id:, limit: 100, after: nil)
|
|
88
89
|
params = {}
|
|
89
90
|
params["limit"] = limit if limit
|
|
91
|
+
params["after"] = after if after
|
|
90
92
|
|
|
91
93
|
response = send_request(
|
|
92
94
|
endpoint: "#{business_id}/message_templates",
|
|
@@ -182,7 +184,12 @@ module WhatsappSdk
|
|
|
182
184
|
# @param start_timestamp [Integer] The start of the time range to retrieve analytics data, in Unix timestamp.
|
|
183
185
|
# @param end_timestamp [Integer] The end of the time range to retrieve analytics data, in Unix timestamp.
|
|
184
186
|
# @param template_ids [Array<String>] An array of template IDs for which to retrieve analytics data.
|
|
185
|
-
# @param metric_types [Array<String>]
|
|
187
|
+
# @param metric_types [Array<String>] Metric types to retrieve; defaults to an empty array.
|
|
188
|
+
# @param granularity [String] Aggregation interval; defaults to DAILY, the only supported value.
|
|
189
|
+
# @param after [String, nil] Cursor for the next page; nil requests the first page.
|
|
190
|
+
# @return [Api::Responses::PaginationRecords] Page of Resource::TemplateAnalytic records with pagination cursors.
|
|
191
|
+
# @raise [ArgumentError] If a metric type or granularity is unsupported.
|
|
192
|
+
# @raise [Api::Responses::HttpResponseError] If Graph returns an API error or a server error.
|
|
186
193
|
def template_analytics(
|
|
187
194
|
business_id:, start_timestamp:, end_timestamp:, template_ids:, metric_types: [],
|
|
188
195
|
granularity: WhatsappSdk::Resource::TemplateAnalytic::Granularity::DAILY,
|
|
@@ -9,10 +9,27 @@ module WhatsappSdk
|
|
|
9
9
|
#
|
|
10
10
|
# The gem have access to the client through WhatsappSdk.configuration.client
|
|
11
11
|
class Configuration
|
|
12
|
-
|
|
12
|
+
SETTINGS = %i[access_token api_version logger logger_options adapter request_options
|
|
13
|
+
multipart_request_options middleware].freeze
|
|
14
|
+
# Stored frozen, so editing them in place raises instead of being ignored by the cached client.
|
|
15
|
+
HASH_SETTINGS = %i[logger_options request_options multipart_request_options].freeze
|
|
16
|
+
REQUEST_OPTION_SETTINGS = %i[request_options multipart_request_options].freeze
|
|
13
17
|
|
|
14
18
|
# loggers like ActiveSupport::Logger (Rails.logger) is a subclass of Logger
|
|
15
|
-
|
|
19
|
+
attr_reader(*SETTINGS)
|
|
20
|
+
|
|
21
|
+
# Changing a setting makes #client build a new client on its next call. The old client is not closed, because
|
|
22
|
+
# other threads may still be using it.
|
|
23
|
+
SETTINGS.each do |name|
|
|
24
|
+
define_method(:"#{name}=") do |value|
|
|
25
|
+
value = value.dup.freeze if HASH_SETTINGS.include?(name)
|
|
26
|
+
Api::Client.validate_request_options(value) if REQUEST_OPTION_SETTINGS.include?(name)
|
|
27
|
+
@mutex.synchronize do
|
|
28
|
+
@client = nil
|
|
29
|
+
instance_variable_set(:"@#{name}", value)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
16
33
|
|
|
17
34
|
def initialize(
|
|
18
35
|
access_token = "",
|
|
@@ -23,11 +40,24 @@ module WhatsappSdk
|
|
|
23
40
|
@access_token = access_token
|
|
24
41
|
@api_version = api_version
|
|
25
42
|
@logger = logger
|
|
26
|
-
@logger_options = logger_options
|
|
43
|
+
@logger_options = logger_options.dup.freeze
|
|
44
|
+
@adapter = nil
|
|
45
|
+
@request_options = {}.freeze
|
|
46
|
+
@multipart_request_options = {}.freeze
|
|
47
|
+
@middleware = nil
|
|
48
|
+
@mutex = Mutex.new
|
|
27
49
|
end
|
|
28
50
|
|
|
51
|
+
# One client is shared by every API object built from this configuration, so its connections are reused.
|
|
52
|
+
# @return [Api::Client]
|
|
29
53
|
def client
|
|
30
|
-
|
|
54
|
+
@mutex.synchronize do
|
|
55
|
+
@client ||= Api::Client.new(
|
|
56
|
+
access_token, api_version, logger, logger_options,
|
|
57
|
+
adapter: adapter, request_options: request_options, multipart_request_options: multipart_request_options,
|
|
58
|
+
middleware: middleware
|
|
59
|
+
)
|
|
60
|
+
end
|
|
31
61
|
end
|
|
32
62
|
end
|
|
33
63
|
end
|