-
Notifications
You must be signed in to change notification settings - Fork 34
/
example.rb
403 lines (333 loc) · 14.8 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# frozen_string_literal: true
# 1) Copy this code into a file and save it `example.rb`
# 2) Replace the `ACCESS_TOKEN` constant with a valid `access_token`.
# 3) Run the file with the command `ruby example.rb`
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "whatsapp_sdk", path: "../"
gem "pry"
gem "pry-nav"
end
require 'whatsapp_sdk'
require "pry"
require "pry-nav"
################# UPDATE CONSTANTS #################
ACCESS_TOKEN = "<TODO replace>"
SENDER_ID = "<TODO replace>"
RECIPIENT_NUMBER = "<TODO replace>"
BUSINESS_ID = "<TODO replace>"
IMAGE_LINK = "<TODO replace>"
if ACCESS_TOKEN == "<TODO replace>"
puts "\n\n**** Please update the ACCESS_TOKEN constant in this file. ****\n\n"
exit
end
puts "\n\n\n\n\n\n *************** Starting calling the Cloud API *************** \n"
################# Initialize Client #################
WhatsappSdk.configure do |config|
config.access_token = ACCESS_TOKEN
end
################# HELPERS ########################
def print_message_sent(message_response)
if message_response.ok?
puts "Message sent to: #{message_response.data.contacts.first.input}"
else
puts "Error: #{message_response.error&.to_s}"
end
end
def print_data_or_error(response, identifier)
if response.error?
return "Error: #{response.error&.to_s}"
end
return identifier
end
##################################################
medias_api = WhatsappSdk::Api::Medias.new
messages_api = WhatsappSdk::Api::Messages.new
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
business_profile_api = WhatsappSdk::Api::BusinessProfile.new
templates_api = WhatsappSdk::Api::Templates.new
############################## Templates API ##############################
## Get list of templates
templates = templates_api.templates(business_id: BUSINESS_ID)
puts "GET Templates list : #{print_data_or_error(templates, templates.data&.templates.map { |r| r.template.name })}"
## Get message templates namespace
template_namespace = templates_api.get_message_template_namespace(business_id: BUSINESS_ID)
puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
# Create a template
components_json = [
{
"type": "BODY",
"text": "Thank you for your order, {{1}}! Your confirmation number is {{2}}. If you have any questions, please use the buttons below to contact support. Thank you for being a customer!",
"example": {
"body_text": [
[
"Ignacio","860198-230332"
]
]
}
},
{
"type": "BUTTONS",
"buttons": [
{
"type": "PHONE_NUMBER",
"text": "Call",
"phone_number": "59898400766"
},
{
"type": "URL",
"text": "Contact Support",
"url": "https://www.luckyshrub.com/support"
}
]
}
]
new_template = templates_api.create(
business_id: BUSINESS_ID, name: "seasonal_promotion", language: "ka", category: "MARKETING",
components_json: components_json, allow_category_change: true
)
puts "GET template by namespace: #{print_data_or_error(template_namespace, template_namespace.data&.id)}"
# Update a template
components_json = [
{
"type" => "header",
"parameters" => [
{
"type" => "image",
"image" => {
"link" => "https://www.google.com/imgres?imgurl=https%3A%2F%2Fqph.cf2.quoracdn.net%2Fmain-qimg-6d977408fdd90a09a1fee7ba9e2f777c-lq&imgrefurl=https%3A%2F%2Fwww.quora.com%2FHow-can-I-find-my-WhatsApp-ID&tbnid=lDAx1vzXwqCakM&vet=12ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ..i&docid=s-DNQVCrZmhJYM&w=602&h=339&q=example%20whatsapp%20image%20id&ved=2ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ"
}
}
]
}
]
updated_template = templates_api.update(template_id: "1624560287967996", category: "UTILITY")
puts "UPDATE template by id: #{print_data_or_error(updated_template, updated_template.data&.id)}"
## Delete a template
delete_template = templates_api.delete(business_id: BUSINESS_ID, name: "seasonal_promotion") # delete by name
puts "DELETE template by id: #{print_data_or_error(delete_template, delete_template.data&.id) }"
# templates_api.delete(business_id: BUSINESS_ID, name: "name2", hsm_id: "243213188351928") # delete by name and id
############################## Business API ##############################
business_profile = business_profile_api.details(SENDER_ID)
puts "DELETE Business Profile by id: #{print_data_or_error(delete_template, business_profile.data&.id) }"
updated_bp = business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
puts "UPDATE Business Profile by id: #{print_data_or_error(updated_bp, updated_bp.data&.id) }"
############################## Phone Numbers API ##############################
registered_number = phone_numbers_api.registered_number(SENDER_ID)
puts "GET Registered number: #{print_data_or_error(registered_number, registered_number.data&.id)}"
registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
puts "GET Registered numbers: #{print_data_or_error(registered_number, registered_numbers.data&.phone_numbers.map(&:id))}"
############################## Media API ##############################
##### Image #####
# upload a Image
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
# get a media Image
if uploaded_media.data&.id
media = medias_api.media(media_id: uploaded_media.data&.id)
puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
# download media Image
download_image = medias_api.download(url: media.data.url, file_path: 'tmp/downloaded_image.png', media_type: "image/png")
puts "Downloaded: #{print_data_or_error(download_image, download_image.data.success?)}"
# delete a media
deleted_media = medias_api.delete(media_id: media.data.id)
puts "DELETE: #{print_data_or_error(deleted_media, deleted_media.data.success?)}"
end
#### Audio ####
# upload an audio
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/downloaded_audio.ogg", type: "audio/ogg")
puts "Uploaded media id: #{print_data_or_error(uploaded_media, uploaded_media.data&.id)}"
if uploaded_media.data&.id
media_id = uploaded_media.data&.id
media_id = uploaded_media.data&.id
puts "Uploaded media id: #{media_id}"
media_id = uploaded_media.data&.id
puts "Uploaded media id: #{media_id}"
# get a media audio
media = medias_api.media(media_id: media_id)
puts "GET Media id: #{print_data_or_error(media, media.data&.id)}"
# get a media audio
audio_link = media.data.url
download_image = medias_api.download(url: audio_link, file_path: 'tmp/downloaded_audio2.ogg', media_type: "audio/ogg")
puts "Download Media Audio: #{print_data_or_error(download_image, download_image.data.success?)}"
end
############################## Messages API ##############################
######### SEND A TEXT MESSAGE
message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "Hey there! it's Whatsapp Ruby SDK")
print_message_sent(message_sent)
######### React to a message
message_id = message_sent.data.messages.first.id
reaction_1_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "\u{1f550}")
reaction_2_sent = messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "⛄️")
puts "Message Reaction 1: #{print_data_or_error(reaction_1_sent, reaction_1_sent.data&.messages.first&.id)}"
puts "Message Reaction 2: #{print_data_or_error(reaction_2_sent, reaction_2_sent.data&.messages.first&.id)}"
######### Reply to a message
message_to_reply_id = message_sent.data.messages.first.id
reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply", message_id: message_to_reply_id)
print_message_sent(reply)
######### Send location
location_sent = messages_api.send_location(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
)
print_message_sent(location_sent)
######### READ A MESSAGE
# messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
######### SEND AN IMAGE
# Send an image with a link
if media.data&.id
image_sent = messages_api.send_image(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.data.url, caption: "Ignacio Chiazzo Profile"
)
print_message_sent(image_sent)
# Send an image with an id
messages_api.send_image(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.data.id, caption: "Ignacio Chiazzo Profile"
)
print_message_sent(image_sent)
end
######### SEND AUDIOS
## with a link
audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
print_message_sent(audio_sent)
## with an audio id
audio_sent = messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
print_message_sent(audio_sent)
######### SEND DOCUMENTS
## with a link
document_sent = messages_api.send_document(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
)
print_message_sent(document_sent)
## with a document id
document_sent = messages_api.send_document(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
)
print_message_sent(document_sent)
######### SEND STICKERS
## with a link
sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
print_message_sent(sticker_sent)
## with a sticker_id
sticker_sent = messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
print_message_sent(sticker_sent)
######### SEND A TEMPLATE
# Note: The template must have been created previously.
# Send a template with no component
response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
name: "hello_world", language: "en_US", components: [])
puts response_with_object
# Send a template with components (Remember to create the template first).
header_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Header
)
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
location = WhatsappSdk::Resource::Location.new(
latitude: 25.779510, longitude: -80.338631, name: "miami store", address: "820 nw 87th ave, miami, fl"
)
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
type: "image",
image: image
)
parameter_document = WhatsappSdk::Resource::ParameterObject.new(
type: "document",
document: document
)
parameter_video = WhatsappSdk::Resource::ParameterObject.new(
type: "video",
video: video
)
parameter_text = WhatsappSdk::Resource::ParameterObject.new(
type: "text",
text: "I am a text"
)
parameter_location = WhatsappSdk::Resource::ParameterObject.new(
type: "location",
location: location
)
header_component.add_parameter(parameter_text)
header_component.add_parameter(parameter_image)
header_component.add_parameter(parameter_video)
header_component.add_parameter(parameter_document)
header_component.add_parameter(parameter_location)
header_component.to_json
body_component = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Body
)
body_component.add_parameter(parameter_text)
body_component.add_parameter(parameter_image)
body_component.add_parameter(parameter_video)
body_component.add_parameter(parameter_document)
body_component.to_json
button_component_1 = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Button,
index: 0,
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
payload: "payload")]
)
button_component_2 = WhatsappSdk::Resource::Component.new(
type: WhatsappSdk::Resource::Component::Type::Button,
index: 1,
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
payload: "payload")]
)
# Send a template with component_json
response_with_json = messages_api.send_template(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
components_json: [
{
"type" => "header",
"parameters" => [
{
"type" => "image",
"image" => {
"link" => "https://www.google.com/imgres?imgurl=https%3A%2F%2Fqph.cf2.quoracdn.net%2Fmain-qimg-6d977408fdd90a09a1fee7ba9e2f777c-lq&imgrefurl=https%3A%2F%2Fwww.quora.com%2FHow-can-I-find-my-WhatsApp-ID&tbnid=lDAx1vzXwqCakM&vet=12ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ..i&docid=s-DNQVCrZmhJYM&w=602&h=339&q=example%20whatsapp%20image%20id&ved=2ahUKEwjKupLviJX4AhVrrHIEHQpGD9MQMygAegUIARC9AQ"
}
}
]
}
]
)
puts response_with_json
######### SEND INTERACTIVE MESSAGES
## with reply buttons
interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
text: "I'm the header!"
)
interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
text: "I'm the body!"
)
interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
text: "I'm the footer!"
)
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton,
)
interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
title: "I'm a reply button 1",
id: "button_1"
)
interactive_action.add_reply_button(interactive_reply_button_1)
interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
title: "I'm a reply button 2",
id: "button_2"
)
interactive_action.add_reply_button(interactive_reply_button_2)
interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
type: WhatsappSdk::Resource::Interactive::Type::ReplyButton,
header: interactive_header,
body: interactive_body,
footer: interactive_footer,
action: interactive_action
)
messages_api.send_interactive_reply_buttons(
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
interactive: interactive_reply_buttons
)