whatsapp_sdk 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +38 -233
- data/.rubocop.yml +4 -1
- data/CHANGELOG.md +5 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +1 -4
- data/README.md +117 -0
- data/example.rb +51 -7
- data/lib/whatsapp_sdk/api/client.rb +3 -2
- data/lib/whatsapp_sdk/api/messages.rb +50 -6
- data/lib/whatsapp_sdk/resource/error.rb +39 -0
- data/lib/whatsapp_sdk/resource/interactive.rb +89 -0
- data/lib/whatsapp_sdk/resource/interactive_action.rb +133 -0
- data/lib/whatsapp_sdk/resource/interactive_action_reply_button.rb +88 -0
- data/lib/whatsapp_sdk/resource/interactive_action_section.rb +75 -0
- data/lib/whatsapp_sdk/resource/interactive_action_section_row.rb +94 -0
- data/lib/whatsapp_sdk/resource/interactive_body.rb +48 -0
- data/lib/whatsapp_sdk/resource/interactive_footer.rb +48 -0
- data/lib/whatsapp_sdk/resource/interactive_header.rb +120 -0
- data/lib/whatsapp_sdk/resource/parameter_object.rb +5 -18
- data/lib/whatsapp_sdk/version.rb +1 -1
- data/whatsapp_sdk.gemspec +0 -1
- metadata +11 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf1d072bf1bc9cd834a9917a863177cc0b5b2ddfac0c29b0d4e899ee23cb9c19
|
4
|
+
data.tar.gz: 25dca7a61a5d80d19fcf32fa54e07edcb911901e936c5b315e46488d87c8b900
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a1204308855d7fb8945a5fda9a3939928b0ec5d6cc6e65fe95abfb303eec2f3dfa4f667f6b9cfc3c264144cdf8290eb80e7aa2ed5e60b213da398d1beded5ef
|
7
|
+
data.tar.gz: 82039c3038209445ad552cca6c23755d8acf2662af4ff6c3fb63464979a42533c92d5768cd376c72252e5b4058932b71a8b9d393471aeb7ba4903a1b62358c12
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,233 +1,38 @@
|
|
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
|
-
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/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Unreleased
|
2
2
|
|
3
|
+
# v 0.8.0
|
4
|
+
- Added Send interactive message @alienware [#82](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/82)
|
5
|
+
- Support JRuby @ignacio-chiazzo [#83](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/83)
|
6
|
+
- Send interactive Reply Buttons Message @alienware [#79](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/79)
|
7
|
+
|
3
8
|
# v 0.7.3
|
4
9
|
- Added the ability to reply messages. @alienware [#77](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/77)
|
5
10
|
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
whatsapp_sdk (0.
|
4
|
+
whatsapp_sdk (0.8.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
@@ -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,11 @@ require "pry-nav"
|
|
22
22
|
|
23
23
|
################# UPDATE CONSTANTS #################
|
24
24
|
|
25
|
-
ACCESS_TOKEN = "
|
26
|
-
SENDER_ID =
|
27
|
-
RECIPIENT_NUMBER =
|
28
|
-
BUSINESS_ID =
|
29
|
-
IMAGE_LINK = "
|
25
|
+
ACCESS_TOKEN = "EAAZAvvr0DZBs0BAIOsVV2FogD1qQhIyIrK9vfM7mmfdTlhbRqFZAZBnS2ciXPD35wb8d69siULaU5cqX9HonDNnZA9YLAbrCLUys1qt1E3n4d69v9RTZCoA8bkz9TZCV2PWzRR9DvBmwUOltCOrZBN0vGQxULlhsJE1mTeUrXlpVfFZCY5sWXCKxe04uyd7dzyK0hMgvkCR97OXi9AI3qJFYo"
|
26
|
+
SENDER_ID = 111591145018464
|
27
|
+
RECIPIENT_NUMBER = 13437772910
|
28
|
+
BUSINESS_ID = 102261539298487
|
29
|
+
IMAGE_LINK = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
|
30
30
|
|
31
31
|
if ACCESS_TOKEN == "<TODO replace>"
|
32
32
|
puts "\n\n**** Please update the ACCESS_TOKEN constant in this file. ****\n\n"
|
@@ -94,7 +94,7 @@ messages_api.send_reaction(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUM
|
|
94
94
|
|
95
95
|
######### Reply to a message
|
96
96
|
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: "
|
97
|
+
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
98
|
print_message_sent(reply)
|
99
99
|
|
100
100
|
######### Send location
|
@@ -230,3 +230,47 @@ response_with_json = messages_api.send_template(
|
|
230
230
|
]
|
231
231
|
)
|
232
232
|
puts response_with_json
|
233
|
+
|
234
|
+
######### SEND INTERACTIVE MESSAGES
|
235
|
+
## with reply buttons
|
236
|
+
interactive_header = WhatsappSdk::Resource::InteractiveHeader.new(
|
237
|
+
type: WhatsappSdk::Resource::InteractiveHeader::Type::Text,
|
238
|
+
text: "I'm the header!"
|
239
|
+
)
|
240
|
+
|
241
|
+
interactive_body = WhatsappSdk::Resource::InteractiveBody.new(
|
242
|
+
text: "I'm the body!"
|
243
|
+
)
|
244
|
+
|
245
|
+
interactive_footer = WhatsappSdk::Resource::InteractiveFooter.new(
|
246
|
+
text: "I'm the footer!"
|
247
|
+
)
|
248
|
+
|
249
|
+
interactive_action = WhatsappSdk::Resource::InteractiveAction.new(
|
250
|
+
type: WhatsappSdk::Resource::InteractiveAction::Type::ReplyButton,
|
251
|
+
)
|
252
|
+
|
253
|
+
interactive_reply_button_1 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
|
254
|
+
title: "I'm a reply button 1",
|
255
|
+
id: "button_1"
|
256
|
+
)
|
257
|
+
interactive_action.add_reply_button(interactive_reply_button_1)
|
258
|
+
|
259
|
+
interactive_reply_button_2 = WhatsappSdk::Resource::InteractiveActionReplyButton.new(
|
260
|
+
title: "I'm a reply button 2",
|
261
|
+
id: "button_2"
|
262
|
+
)
|
263
|
+
interactive_action.add_reply_button(interactive_reply_button_2)
|
264
|
+
|
265
|
+
interactive_reply_buttons = WhatsappSdk::Resource::Interactive.new(
|
266
|
+
type: WhatsappSdk::Resource::Interactive::Type::ReplyButton,
|
267
|
+
header: interactive_header,
|
268
|
+
body: interactive_body,
|
269
|
+
footer: interactive_footer,
|
270
|
+
action: interactive_action
|
271
|
+
)
|
272
|
+
|
273
|
+
messages_api.send_interactive_reply_buttons(
|
274
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
275
|
+
interactive: interactive_reply_buttons
|
276
|
+
)
|
@@ -3,7 +3,6 @@
|
|
3
3
|
|
4
4
|
require "faraday"
|
5
5
|
require "faraday/multipart"
|
6
|
-
require "oj"
|
7
6
|
|
8
7
|
module WhatsappSdk
|
9
8
|
module Api
|
@@ -31,7 +30,9 @@ module WhatsappSdk
|
|
31
30
|
|
32
31
|
response = faraday_request.public_send(http_method, endpoint, request_params(params, headers), headers)
|
33
32
|
|
34
|
-
|
33
|
+
return nil if response.body == ""
|
34
|
+
|
35
|
+
JSON.parse(response.body)
|
35
36
|
end
|
36
37
|
|
37
38
|
sig { params(url: String, path_to_file_name: T.nilable(String)).returns(Net::HTTPResponse) }
|
@@ -363,13 +363,57 @@ module WhatsappSdk
|
|
363
363
|
# # TODO: https://developers.facebook.com/docs/whatsapp_sdk/cloud-api/reference/messages#contacts-object
|
364
364
|
# end
|
365
365
|
|
366
|
-
#
|
367
|
-
#
|
368
|
-
#
|
366
|
+
# Send interactive reply buttons.
|
367
|
+
# https://developers.facebook.com/docs/whatsapp/guides/interactive-messages#reply-buttons
|
368
|
+
# You can either send interactive object or as JSON.
|
369
|
+
#
|
370
|
+
# @param sender_id [Integer] Sender' phone number.
|
371
|
+
# @param recipient_number [Integer] Recipient' Phone number.
|
372
|
+
# @param interactive [Interactive] Interactive.
|
373
|
+
# @param interactive_json [Json] The interactive object as a Json.
|
374
|
+
# If you pass interactive_json, you can't pass interactive.
|
375
|
+
# @param message_id [String] The id of the message to reply to.
|
376
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
377
|
+
sig do
|
378
|
+
params(
|
379
|
+
sender_id: Integer, recipient_number: Integer,
|
380
|
+
interactive: T.nilable(WhatsappSdk::Resource::Interactive),
|
381
|
+
interactive_json: T.nilable(T::Hash[T.untyped, T.untyped]), message_id: T.nilable(String)
|
382
|
+
).returns(WhatsappSdk::Api::Response)
|
383
|
+
end
|
384
|
+
def send_interactive_message(
|
385
|
+
sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
|
386
|
+
)
|
387
|
+
raise MissingArgumentError, "interactive or interactive_json is required" if !interactive && !interactive_json
|
369
388
|
|
370
|
-
|
371
|
-
|
372
|
-
|
389
|
+
params = {
|
390
|
+
messaging_product: "whatsapp",
|
391
|
+
to: recipient_number,
|
392
|
+
recipient_type: "individual",
|
393
|
+
type: "interactive"
|
394
|
+
}
|
395
|
+
|
396
|
+
params[:interactive] = if interactive.nil?
|
397
|
+
interactive_json
|
398
|
+
else
|
399
|
+
interactive.to_json
|
400
|
+
end
|
401
|
+
params[:context] = { message_id: message_id } if message_id
|
402
|
+
|
403
|
+
response = send_request(
|
404
|
+
endpoint: endpoint(sender_id),
|
405
|
+
params: params,
|
406
|
+
headers: DEFAULT_HEADERS
|
407
|
+
)
|
408
|
+
|
409
|
+
WhatsappSdk::Api::Response.new(
|
410
|
+
response: response,
|
411
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
412
|
+
)
|
413
|
+
end
|
414
|
+
|
415
|
+
alias :send_interactive_reply_buttons :send_interactive_message
|
416
|
+
alias :send_interactive_list_messages :send_interactive_message
|
373
417
|
|
374
418
|
# Mark a message as read.
|
375
419
|
#
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module WhatsappSdk
|
5
|
+
module Resource
|
6
|
+
module Error
|
7
|
+
class MissingValue < WhatsappSdk::Error
|
8
|
+
extend T::Sig
|
9
|
+
|
10
|
+
sig { returns(String) }
|
11
|
+
attr_reader :field
|
12
|
+
|
13
|
+
sig { returns(String) }
|
14
|
+
attr_reader :message
|
15
|
+
|
16
|
+
sig { params(field: String, message: String).void }
|
17
|
+
def initialize(field, message)
|
18
|
+
@field = field
|
19
|
+
@message = message
|
20
|
+
super(message)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class InvalidField < MissingValue; end
|
25
|
+
|
26
|
+
class InvalidInteractiveBody < WhatsappSdk::Error; end
|
27
|
+
|
28
|
+
class InvalidInteractiveActionReplyButton < WhatsappSdk::Error; end
|
29
|
+
|
30
|
+
class InvalidInteractiveActionButton < WhatsappSdk::Error; end
|
31
|
+
|
32
|
+
class InvalidInteractiveActionSection < WhatsappSdk::Error; end
|
33
|
+
|
34
|
+
class InvalidInteractiveActionSectionRow < WhatsappSdk::Error; end
|
35
|
+
|
36
|
+
class InvalidInteractiveFooter < WhatsappSdk::Error; end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|