whatsapp_sdk 0.7.3 → 0.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d38079d66c6380bef5439626decde3a7051629f303308bbdb9d504b7608444c6
4
- data.tar.gz: c7eaa83b9edc41a6044f5ddd119e381936edcd1f6c6c5430dd7de29624bb2048
3
+ metadata.gz: 5fb8ebc1170878767c0800ab380f6cca2cd853fe59ec7496921781ef85fde8fb
4
+ data.tar.gz: 67e312ba37cc9c8e612af0a5d0840c1b7933eb3dfcebb9641faee41194387d21
5
5
  SHA512:
6
- metadata.gz: 4f545dfd51217c6339a4b9ea423a529a95079b04be5d67e9e27f5b7d26faf0318a321af9391d82f0d10a72a0a3cd666f117d0a6fac3b25393f9cf25b8e0b51de
7
- data.tar.gz: 711d449f1358ba11e24e82390d12804771b4402ebf392ff2a4c065682a7222e7e97c139f6ab723d6c8b1102064e86ee4960f40b88575245c01c021911b622502
6
+ metadata.gz: 856af75ac80060b7f0c6e282fc64d396a102dc31c6cbd8bf09c15bcee55b265ee4e0e5ffbff7e3bab055659233cd4d2db087a6303598128d881ae935b5d45685
7
+ data.tar.gz: d709a2b39b758cb294a1cc682320bfa4be3a634d137561e01b79f66c3df042312a31e07925d774ab42e84b51e7597a1b20bb6ef1965503a84085cfb1827aca58
@@ -1,233 +1,38 @@
1
- # frozen_string_literal: true
2
-
3
- # 1) Copy this code into a file and save it `example.rb`
4
- # 2) Replace the `ACCESS_TOKEN` constant with a valid `access_token`.
5
- # 3) Run the file with the command `ruby example.rb`
6
-
7
- require 'bundler/inline'
8
-
9
- gemfile(true) do
10
- source 'https://rubygems.org'
11
-
12
- git_source(:github) { |repo| "https://github.com/#{repo}.git" }
13
-
14
- gem "whatsapp_sdk", path: "../"
15
- gem "pry"
16
- gem "pry-nav"
17
- end
18
-
19
- require 'whatsapp_sdk'
20
- require "pry"
21
- require "pry-nav"
22
-
23
- ################# UPDATE CONSTANTS #################
24
-
25
- ACCESS_TOKEN = "EAAZAvvr0DZBs0BAF7pwQ7UEDXxNi0fn471RhslKIc2OCfsHolo2bTjISXbISUzGrUDt4NrgLWmnhiCCXSLUxIPfsBXPJYyXiz5aOpsjyj6GuZBKhM0Sf40NGxVLtgrgZAI0EIpypwwF7W5hQa54WiaZCwoQWZBnfubhvMtF9C67A00CVcI66zGVZAC95CDjfO3hiLSUx87ZCM83DRCOltbFz"
26
- SENDER_ID = 111591145018464
27
- RECIPIENT_NUMBER = 13437772910
28
- BUSINESS_ID = 102261539298487
29
- IMAGE_LINK = "<TODO replace>"
30
-
31
- if ACCESS_TOKEN == "<TODO replace>"
32
- puts "\n\n**** Please update the ACCESS_TOKEN constant in this file. ****\n\n"
33
- exit
34
- end
35
-
36
- ################# Initialize Client #################
37
- WhatsappSdk.configure do |config|
38
- config.access_token = ACCESS_TOKEN
39
- end
40
-
41
- ################# HELPERS ########################
42
- def print_message_sent(message_response)
43
- if message_response.ok?
44
- puts "Message sent to: #{message_response.data.contacts.first.input}"
45
- else
46
- puts "Error: #{message_response.error&.to_s}"
47
- end
48
- end
49
- ##################################################
50
-
51
- medias_api = WhatsappSdk::Api::Medias.new
52
- messages_api = WhatsappSdk::Api::Messages.new
53
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
54
- business_profile_api = WhatsappSdk::Api::BusinessProfile.new
55
- binding.pry
56
-
57
- ############################## Business API ##############################
58
- business_profile = business_profile_api.details(SENDER_ID)
59
- business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
60
-
61
- ############################## Phone Numbers API ##############################
62
- registered_number = phone_numbers_api.registered_number(SENDER_ID)
63
- registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
64
-
65
- ############################## Media API ##############################
66
-
67
- # upload a media
68
- uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
69
- media_id = uploaded_media.data&.id
70
- puts "Uploaded media id: #{media_id}"
71
-
72
- # get a media
73
- media = medias_api.media(media_id: media_id).data
74
- puts "Media info: #{media.raw_data_response}"
75
-
76
- # download media
77
- download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
78
- puts "Downloaded: #{download_image.data.success?}"
79
-
80
- # delete a media
81
- deleted_media = medias_api.delete(media_id: media&.id)
82
- puts "Deleted: #{deleted_media.data.success?}"
83
-
84
- ############################## Messages API ##############################
85
-
86
- ######### SEND A TEXT MESSAGE
87
- message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
88
- message: "Hey there! it's Whatsapp Ruby SDK")
89
- print_message_sent(message_sent)
90
-
91
- ######### Reply to a message
92
- message_to_reply_id = message_sent.data.messages.first.id
93
- reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "GREAT WORK!!!! I'm a reply", message_id: "wamid.HBgLMTM0Mzc3NzI5MTAVAgARGBI3NzE5MjY4RTA0RDg1MDZFQTEA")
94
- print_message_sent(reply)
95
-
96
- ######### React to a message
97
- message_id = message_sent.data.messages.first.id
98
- messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "\u{1f550}")
99
- messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message_id: message_id, emoji: "⛄️")
100
-
101
- ######### Send location
102
- location_sent = messages_api.send_location(
103
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
104
- longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
105
- )
106
- print_message_sent(location_sent)
107
-
108
- ######### READ A MESSAGE
109
- # messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
110
-
111
- ######### SEND AN IMAGE
112
- # Send an image with a link
113
- image_sent = messages_api.send_image(
114
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.url, caption: "Ignacio Chiazzo Profile"
115
- )
116
- print_message_sent(image_sent)
117
-
118
- # Send an image with an id
119
- messages_api.send_image(
120
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.id, caption: "Ignacio Chiazzo Profile"
121
- )
122
-
123
- ######### SEND AUDIOS
124
- ## with a link
125
- messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
126
-
127
- ## with an audio id
128
- messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
129
-
130
- ######### SEND DOCUMENTS
131
- ## with a link
132
- messages_api.send_document(
133
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
134
- )
135
-
136
- ## with a document id
137
- messages_api.send_document(
138
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
139
- )
140
-
141
- ######### SEND STICKERS
142
- ## with a link
143
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
144
-
145
- ## with a sticker_id
146
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
147
-
148
- ######### SEND A TEMPLATE
149
- # Note: The template must have been created previously.
150
-
151
- # Send a template with no component
152
- response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
153
- name: "hello_world", language: "en_US", components: [])
154
- puts response_with_object
155
-
156
- # Send a template with components.Remember to create the template first.
157
- header_component = WhatsappSdk::Resource::Component.new(
158
- type: WhatsappSdk::Resource::Component::Type::Header
159
- )
160
-
161
- image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
162
- document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
163
- video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
164
-
165
- parameter_image = WhatsappSdk::Resource::ParameterObject.new(
166
- type: "image",
167
- image: image
168
- )
169
-
170
- parameter_document = WhatsappSdk::Resource::ParameterObject.new(
171
- type: "document",
172
- document: document
173
- )
174
-
175
- parameter_video = WhatsappSdk::Resource::ParameterObject.new(
176
- type: "video",
177
- video: video
178
- )
179
-
180
- parameter_text = WhatsappSdk::Resource::ParameterObject.new(
181
- type: "text",
182
- text: "I am a text"
183
- )
184
-
185
- header_component.add_parameter(parameter_text)
186
- header_component.add_parameter(parameter_image)
187
- header_component.add_parameter(parameter_video)
188
- header_component.add_parameter(parameter_document)
189
- header_component.to_json
190
-
191
- body_component = WhatsappSdk::Resource::Component.new(
192
- type: WhatsappSdk::Resource::Component::Type::Body
193
- )
194
- body_component.add_parameter(parameter_text)
195
- body_component.add_parameter(parameter_image)
196
- body_component.add_parameter(parameter_video)
197
- body_component.add_parameter(parameter_document)
198
- body_component.to_json
199
-
200
- button_component_1 = WhatsappSdk::Resource::Component.new(
201
- type: WhatsappSdk::Resource::Component::Type::Button,
202
- index: 0,
203
- sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
204
- parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
205
- payload: "payload")]
206
- )
207
-
208
- button_component_2 = WhatsappSdk::Resource::Component.new(
209
- type: WhatsappSdk::Resource::Component::Type::Button,
210
- index: 1,
211
- sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
212
- parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
213
- payload: "payload")]
214
- )
215
-
216
- # Send a template with component_json
217
- response_with_json = messages_api.send_template(
218
- sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
219
- components_json: [
220
- {
221
- "type" => "header",
222
- "parameters" => [
223
- {
224
- "type" => "image",
225
- "image" => {
226
- "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"
227
- }
228
- }
229
- ]
230
- }
231
- ]
232
- )
233
- puts response_with_json
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.6', '2.7', '3.0']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - name: Set up Ruby
30
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
+ # uses: ruby/setup-ruby@v1
33
+ uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
+ - name: Run tests
38
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -7,6 +7,8 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ example_copy.rb
11
+
10
12
  DELETE_ME
11
13
  REMOVE.rb
12
14
 
data/.rubocop.yml CHANGED
@@ -26,7 +26,13 @@ Layout/LineLength:
26
26
  Max: 120
27
27
 
28
28
  Metrics/MethodLength:
29
- Max: 40
29
+ Enabled: false
30
+
31
+ Metrics/BlockLength:
32
+ Enabled: false
33
+
34
+ Metrics/CyclomaticComplexity:
35
+ Enabled: false
30
36
 
31
37
  Naming/VariableNumber:
32
38
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Unreleased
2
2
 
3
+ # v 0.9.0
4
+ - Use binary mode to download files @ignacio-chiazzo [#88](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/87)
5
+ - Added support for downloading media by specifying the type @ignacio-chiazzo [#87](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/87)
6
+
7
+ # v 0.8.0
8
+ - Added Send interactive message @alienware [#82](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/82)
9
+ - Support JRuby @ignacio-chiazzo [#83](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/83)
10
+ - Send interactive Reply Buttons Message @alienware [#79](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/79)
11
+
3
12
  # v 0.7.3
4
13
  - Added the ability to reply messages. @alienware [#77](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/77)
5
14
 
data/Gemfile CHANGED
@@ -7,7 +7,6 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
7
7
 
8
8
  gem("faraday")
9
9
  gem("faraday-multipart")
10
- gem("oj")
11
10
  gem("rake", ">= 12.3.3")
12
11
  gem('sorbet-runtime')
13
12
  gem("zeitwerk", ">= 2.6.0")
data/Gemfile.lock CHANGED
@@ -1,10 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- whatsapp_sdk (0.7.3)
4
+ whatsapp_sdk (0.9.0)
5
5
  faraday (~> 2.3.0)
6
6
  faraday-multipart (~> 1.0.4)
7
- oj (~> 3.13.13)
8
7
  sorbet-runtime (~> 0.5.1)
9
8
  zeitwerk (~> 2.6.0)
10
9
 
@@ -30,7 +29,6 @@ GEM
30
29
  mocha (1.14.0)
31
30
  multipart-post (2.2.3)
32
31
  netrc (0.11.0)
33
- oj (3.13.14)
34
32
  parallel (1.22.1)
35
33
  parser (3.1.2.0)
36
34
  ast (~> 2.4.1)
@@ -119,7 +117,6 @@ DEPENDENCIES
119
117
  faraday-multipart
120
118
  minitest (~> 5.0)
121
119
  mocha
122
- oj
123
120
  pry
124
121
  pry-nav
125
122
  rake (>= 12.3.3)
data/README.md CHANGED
@@ -183,7 +183,7 @@ media = medias_api.media(media_id: MEDIA_ID)
183
183
 
184
184
  Download media
185
185
  ```ruby
186
- medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png')
186
+ medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png', media_type: "image/png")
187
187
  ```
188
188
 
189
189
  Delete a media
@@ -350,8 +350,125 @@ Alernative, you could pass a plain json like this:
350
350
  ```ruby
351
351
  @messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [{...}])
352
352
  ```
353
+
354
+ **Send interactive messages**
355
+ Visit the [Official API Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-messages#interactive-messages)
356
+
357
+ <details>
358
+ <summary>List Message's example</summary>
359
+
360
+ ```ruby
361
+ interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
362
+ type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
363
+ text: "I am the header!"
364
+ )
365
+
366
+ interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
367
+ text: "I am the body!"
368
+ )
369
+
370
+ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
371
+ text: "I am the footer!"
372
+ )
373
+
374
+ interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
375
+ type: WhatsappSdk::Resource::InteractiveAction::Type::ListMessage
376
+ )
377
+
378
+ interactive_action.button = "I am the button CTA"
379
+
380
+ interactive_section_1 = WhatsappSdk::Resource::InteractiveActionSection.new(
381
+ title: "I am the section 1"
382
+ )
383
+ interactive_section_1_row_1 = WhatsappSdk::Resource::InteractiveActionSectionRow.new(
384
+ title: "I am the row 1 title",
385
+ id: "section_1_row_1",
386
+ description: "I am the optional section 1 row 1 description"
387
+ )
388
+ interactive_section_1.add_row(interactive_section_1_row_1)
389
+ interactive_action.add_section(interactive_section_1)
390
+
391
+ interactive_list_messages = WhatsappSdk::Resource::Interactive.new(
392
+ type: WhatsappSdk::Resource::Interactive::Type::ListMessage,
393
+ header: interactive_header,
394
+ body: interactive_body,
395
+ footer: interactive_footer,
396
+ action: interactive_action
397
+ )
398
+
399
+ messages_api.send_interactive_list_messages(
400
+ sender_id: 12_345, recipient_number: 1234567890,
401
+ interactive: interactive_list_messages
402
+ )
403
+ ```
404
+ </details>
405
+
406
+ Alternative, you could pass a plain json like this:
407
+ ```ruby
408
+ messages_api.send_interactive_list_messages(
409
+ sender_id: 12_345, recipient_number: 1234567890
410
+ interactive_json: {...}
411
+ )
412
+ ```
413
+
414
+ <details>
415
+ <summary>Reply Button's example</summary>
416
+
417
+ ```ruby
418
+ interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
419
+ type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
420
+ text: "I am the header!"
421
+ )
422
+
423
+ interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
424
+ text: "I am the body!"
425
+ )
426
+
427
+ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
428
+ text: "I am the footer!"
429
+ )
430
+
431
+ interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
432
+ type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton
433
+ )
434
+
435
+ interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
436
+ title: "I am the reply button 1",
437
+ id: "button_1"
438
+ )
439
+ interactive_action.add_reply_button(interactive_reply_button_1)
440
+
441
+ interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
442
+ title: "I am the reply button 2",
443
+ id: "button_2"
444
+ )
445
+ interactive_action.add_reply_button(interactive_reply_button_2)
446
+
447
+ interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
448
+ type: WhatsappSdk::Resource::Interactive::Type::ReplyButton,
449
+ header: interactive_header,
450
+ body: interactive_body,
451
+ footer: interactive_footer,
452
+ action: interactive_action
453
+ )
454
+
455
+ messages_api.send_interactive_reply_buttons(
456
+ sender_id: 12_345, recipient_number: 1234567890,
457
+ interactive: interactive_reply_buttons
458
+ )
459
+ ```
353
460
  </details>
354
461
 
462
+ Alternative, you could pass a plain json like this:
463
+ ```ruby
464
+ messages_api.send_interactive_reply_buttons(
465
+ sender_id: 12_345, recipient_number: 1234567890
466
+ interactive_json: {...}
467
+ )
468
+ ```
469
+ </details>
470
+
471
+
355
472
  ## Examples
356
473
 
357
474
  Visit [the example file](/example.rb) with examples to call the API in a single file.
data/example.rb CHANGED
@@ -11,7 +11,7 @@ gemfile(true) do
11
11
 
12
12
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
13
13
 
14
- gem "whatsapp_sdk"
14
+ gem "whatsapp_sdk", path: "../"
15
15
  gem "pry"
16
16
  gem "pry-nav"
17
17
  end
@@ -22,11 +22,12 @@ require "pry-nav"
22
22
 
23
23
  ################# UPDATE CONSTANTS #################
24
24
 
25
- ACCESS_TOKEN = "<TODO replace>"
26
- SENDER_ID = "<TODO replace>"
27
- RECIPIENT_NUMBER = "<TODO replace>"
28
- BUSINESS_ID = "<TODO replace>"
29
- IMAGE_LINK = "<TODO replace>"
25
+ ACCESS_TOKEN = "EAAHlmy2rChwBAGELLYnNnJfhKOPuSuaX5cRrfYA65RLY2NlEsMQ4x4tO3fz2imwrhmyx2pvKnC07tm0sWRzFHEko7CtXoZBTSb3lrBrKlx86eDvtdZBm2P2ewEJPbotfMYhTYwLsfMyRdQqgNAmc0wij1hMTHOusZALovPKHsme3RvAo1Ag1wqZA3qrPB2WhZChhKWPOkVQZDZD"
26
+ SENDER_ID = 100219219709628
27
+ RECIPIENT_NUMBER = 15550429560
28
+ BUSINESS_ID = 102_261_539_298_487
29
+ IMAGE_LINK = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
30
+ AUDIO_LINK = "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=2951440611667284&ext=1681491953&hash=ATsLUNWiMmGDndn5YlpWQFHm5CUXca0gdahSJTCp5XjgTQ"
30
31
 
31
32
  if ACCESS_TOKEN == "<TODO replace>"
32
33
  puts "\n\n**** Please update the ACCESS_TOKEN constant in this file. ****\n\n"
@@ -53,6 +54,23 @@ messages_api = WhatsappSdk::Api::Messages.new
53
54
  phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
54
55
  business_profile_api = WhatsappSdk::Api::BusinessProfile.new
55
56
 
57
+ binding.pry
58
+ # upload an audio
59
+ uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/downloaded_audio.ogg", type: "audio/ogg")
60
+ media_id = uploaded_media.data&.id
61
+ puts "Uploaded media id: #{media_id}"
62
+
63
+ # get a media audio
64
+ media = medias_api.media(media_id: media_id).data
65
+ puts "Media info: #{media.raw_data_response}"
66
+
67
+ # get a media audio
68
+ audio_link = media.url
69
+ download_image = medias_api.download(url: audio_link, file_path: 'tmp/downloaded_audio2.ogg', media_type: "audio/ogg")
70
+ puts "Downloaded: #{download_image.data.success?}"
71
+
72
+
73
+
56
74
  ############################## Business API ##############################
57
75
  business_profile = business_profile_api.details(SENDER_ID)
58
76
  business_profile_api.update(phone_number_id: SENDER_ID, params: { about: "A very cool business" } )
@@ -63,23 +81,39 @@ registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
63
81
 
64
82
  ############################## Media API ##############################
65
83
 
66
- # upload a media
84
+ ##### Image #####
85
+ # upload a Image
67
86
  uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
68
87
  media_id = uploaded_media.data&.id
69
88
  puts "Uploaded media id: #{media_id}"
70
89
 
71
- # get a media
90
+ # get a media Image
72
91
  media = medias_api.media(media_id: media_id).data
73
92
  puts "Media info: #{media.raw_data_response}"
74
93
 
75
- # download media
76
- download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
94
+ # download media Image
95
+ download_image = medias_api.download(url: media.url, file_path: 'tmp/downloaded_image.png', media_type: "image/png")
77
96
  puts "Downloaded: #{download_image.data.success?}"
78
97
 
79
98
  # delete a media
80
99
  deleted_media = medias_api.delete(media_id: media&.id)
81
100
  puts "Deleted: #{deleted_media.data.success?}"
82
101
 
102
+ #### Audio ####
103
+ # upload an audio
104
+ uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/downloaded_audio.ogg", type: "audio/ogg")
105
+ media_id = uploaded_media.data&.id
106
+ puts "Uploaded media id: #{media_id}"
107
+
108
+ # get a media audio
109
+ media = medias_api.media(media_id: media_id).data
110
+ puts "Media info: #{media.raw_data_response}"
111
+
112
+ # get a media audio
113
+ audio_link = media.url
114
+ download_image = medias_api.download(url: audio_link, file_path: 'tmp/downloaded_audio2.ogg', media_type: "audio/ogg")
115
+ puts "Downloaded: #{download_image.data.success?}"
116
+
83
117
  ############################## Messages API ##############################
84
118
 
85
119
  ######### SEND A TEXT MESSAGE
@@ -94,7 +128,7 @@ messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUM
94
128
 
95
129
  ######### Reply to a message
96
130
  message_to_reply_id = message_sent.data.messages.first.id
97
- reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "GREAT WORK!!!! I'm a reply", message_id: message_to_reply_id)
131
+ reply = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "I'm a reply", message_id: message_to_reply_id)
98
132
  print_message_sent(reply)
99
133
 
100
134
  ######### Send location
@@ -230,3 +264,47 @@ response_with_json = messages_api.send_template(
230
264
  ]
231
265
  )
232
266
  puts response_with_json
267
+
268
+ ######### SEND INTERACTIVE MESSAGES
269
+ ## with reply buttons
270
+ interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
271
+ type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
272
+ text: "I'm the header!"
273
+ )
274
+
275
+ interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
276
+ text: "I'm the body!"
277
+ )
278
+
279
+ interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
280
+ text: "I'm the footer!"
281
+ )
282
+
283
+ interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
284
+ type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton,
285
+ )
286
+
287
+ interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
288
+ title: "I'm a reply button 1",
289
+ id: "button_1"
290
+ )
291
+ interactive_action.add_reply_button(interactive_reply_button_1)
292
+
293
+ interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
294
+ title: "I'm a reply button 2",
295
+ id: "button_2"
296
+ )
297
+ interactive_action.add_reply_button(interactive_reply_button_2)
298
+
299
+ interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
300
+ type: WhatsappSdk::Resource::Interactive::Type::ReplyButton,
301
+ header: interactive_header,
302
+ body: interactive_body,
303
+ footer: interactive_footer,
304
+ action: interactive_action
305
+ )
306
+
307
+ messages_api.send_interactive_reply_buttons(
308
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
309
+ interactive: interactive_reply_buttons
310
+ )