whatsapp_sdk 0.3.2 → 0.4.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/.gitignore +1 -0
- data/.rubocop.yml +14 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +44 -0
- data/README.md +102 -28
- data/Rakefile +1 -0
- data/bin/tapioca +30 -0
- data/example.rb +31 -36
- data/lib/version.rb +2 -1
- data/lib/whatsapp_sdk/api/client.rb +14 -3
- data/lib/whatsapp_sdk/api/medias.rb +12 -3
- data/lib/whatsapp_sdk/api/messages.rb +72 -14
- data/lib/whatsapp_sdk/api/phone_numbers.rb +3 -0
- data/lib/whatsapp_sdk/api/request.rb +2 -2
- data/lib/whatsapp_sdk/api/response.rb +17 -1
- data/lib/whatsapp_sdk/api/responses/data_response.rb +10 -3
- data/lib/whatsapp_sdk/api/responses/error_response.rb +8 -1
- data/lib/whatsapp_sdk/api/responses/media_data_response.rb +26 -7
- data/lib/whatsapp_sdk/api/responses/message_data_response.rb +20 -3
- data/lib/whatsapp_sdk/api/responses/message_error_response.rb +26 -7
- data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +18 -5
- data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +9 -1
- data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +10 -3
- data/lib/whatsapp_sdk/api/responses/success_response.rb +5 -1
- data/lib/whatsapp_sdk/configuration.rb +7 -3
- data/lib/whatsapp_sdk/error.rb +1 -0
- data/lib/whatsapp_sdk/resource/address.rb +30 -6
- data/lib/whatsapp_sdk/resource/address_type.rb +15 -0
- data/lib/whatsapp_sdk/resource/button_parameter.rb +19 -23
- data/lib/whatsapp_sdk/resource/component.rb +45 -13
- data/lib/whatsapp_sdk/resource/contact.rb +30 -1
- data/lib/whatsapp_sdk/resource/contact_response.rb +9 -1
- data/lib/whatsapp_sdk/resource/currency.rb +9 -1
- data/lib/whatsapp_sdk/resource/date_time.rb +7 -1
- data/lib/whatsapp_sdk/resource/email.rb +9 -5
- data/lib/whatsapp_sdk/resource/media.rb +44 -15
- data/lib/whatsapp_sdk/resource/message.rb +5 -0
- data/lib/whatsapp_sdk/resource/name.rb +28 -1
- data/lib/whatsapp_sdk/resource/org.rb +13 -1
- data/lib/whatsapp_sdk/resource/parameter_object.rb +82 -39
- data/lib/whatsapp_sdk/resource/phone_number.rb +12 -5
- data/lib/whatsapp_sdk/resource/url.rb +9 -5
- data/lib/whatsapp_sdk.rb +7 -0
- data/sorbet/config +6 -0
- data/sorbet/rbi/annotations/faraday.rbi +17 -0
- data/sorbet/rbi/annotations/mocha.rbi +34 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/gems/faraday-multipart@1.0.4.rbi +270 -0
- data/sorbet/rbi/gems/faraday-net_http@2.0.3.rbi +182 -0
- data/sorbet/rbi/gems/faraday@2.3.0.rbi +2494 -0
- data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
- data/sorbet/rbi/gems/minitest@5.16.1.rbi +1459 -0
- data/sorbet/rbi/gems/mocha@1.14.0.rbi +60 -0
- data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +239 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +150 -0
- data/sorbet/rbi/gems/oj@3.13.14.rbi +589 -0
- data/sorbet/rbi/gems/zeitwerk@2.6.0.rbi +867 -0
- data/sorbet/rbi/todo.rbi +8 -0
- data/sorbet/shims/request.rbi +10 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +5 -0
- data/whatsapp_sdk.gemspec +4 -1
- metadata +50 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c511dc642ddf1e541454117ebb5e08df060b166b47e7905bfd9e8e989e394e1
|
4
|
+
data.tar.gz: 49f4b2fd988d44d6cdac186a47b3553fb78203ceffb1675c80c6bd176f59d68f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbcebfa04a24c570cbd28dae1410de7c5d2f95280f4f06129beb7aecdda13cc4d0b218a6c3815b5ecfefbe97c8ddc19c423ce55fb1ac3e4a6cfe535b42a6a4e1
|
7
|
+
data.tar.gz: 8e84e6f855f6383e1a0b56edeb804f72c8519b521bbf1d174c40ac659e8b244c23268cf87ad7df7ef77cbb753420e64b4910914ea80e9cd80bee1675ec38f4cf
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -4,6 +4,13 @@ require:
|
|
4
4
|
|
5
5
|
AllCops:
|
6
6
|
NewCops: enable
|
7
|
+
Exclude:
|
8
|
+
- 'example.rb'
|
9
|
+
- 'tmp/*'
|
10
|
+
- 'vendor/**/*'
|
11
|
+
|
12
|
+
Lint/ToJSON:
|
13
|
+
Enabled: false
|
7
14
|
|
8
15
|
Style/StringLiterals:
|
9
16
|
Enabled: false
|
@@ -29,9 +36,16 @@ Metrics/ClassLength:
|
|
29
36
|
Metrics/AbcSize:
|
30
37
|
Enabled: false
|
31
38
|
|
39
|
+
Lint/MissingSuper:
|
40
|
+
Enabled: false
|
41
|
+
|
32
42
|
# Wait until https://github.com/rubocop/rubocop/issues/8761 is fixed
|
33
43
|
Gemspec/RequiredRubyVersion:
|
34
44
|
Enabled: false
|
35
45
|
|
36
46
|
Minitest/MultipleAssertions:
|
37
47
|
Enabled: false
|
48
|
+
|
49
|
+
Style/AccessorGrouping: # disabled so that we can use sorbet signatures for each attr
|
50
|
+
Enabled: false
|
51
|
+
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# Unreleased
|
2
|
+
- Make the gem stricly typed using Sorbet [#34](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/34), [#35](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/35), [#37](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/37)
|
3
|
+
- Object IDs are Strings in development [#37](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/pull/37)
|
2
4
|
|
3
5
|
# v 0.3.2
|
4
6
|
- Include Zeitwerk
|
data/Gemfile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: true
|
2
3
|
|
3
4
|
source "https://rubygems.org"
|
4
5
|
|
@@ -8,6 +9,7 @@ gem("faraday")
|
|
8
9
|
gem("faraday-multipart")
|
9
10
|
gem("oj")
|
10
11
|
gem("rake", ">= 12.3.3")
|
12
|
+
gem('sorbet-runtime')
|
11
13
|
gem("zeitwerk", ">= 2.6.0")
|
12
14
|
|
13
15
|
group(:test) do
|
@@ -20,6 +22,9 @@ end
|
|
20
22
|
group(:development) do
|
21
23
|
gem('pry')
|
22
24
|
gem('pry-nav')
|
25
|
+
gem('sorbet')
|
26
|
+
gem('spoom')
|
27
|
+
gem('tapioca', require: false)
|
23
28
|
end
|
24
29
|
|
25
30
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -5,6 +5,8 @@ PATH
|
|
5
5
|
faraday (~> 2.3.0)
|
6
6
|
faraday-multipart (~> 1.0.4)
|
7
7
|
oj (~> 3.13.13)
|
8
|
+
sorbet
|
9
|
+
sorbet-runtime
|
8
10
|
zeitwerk (~> 2.6.0)
|
9
11
|
|
10
12
|
GEM
|
@@ -12,6 +14,7 @@ GEM
|
|
12
14
|
specs:
|
13
15
|
ast (2.4.2)
|
14
16
|
coderay (1.1.3)
|
17
|
+
diff-lcs (1.5.0)
|
15
18
|
faraday (2.3.0)
|
16
19
|
faraday-net_http (~> 2.0)
|
17
20
|
ruby2_keywords (>= 0.0.4)
|
@@ -22,6 +25,7 @@ GEM
|
|
22
25
|
minitest (5.16.1)
|
23
26
|
mocha (1.14.0)
|
24
27
|
multipart-post (2.2.3)
|
28
|
+
netrc (0.11.0)
|
25
29
|
oj (3.13.14)
|
26
30
|
parallel (1.22.1)
|
27
31
|
parser (3.1.2.0)
|
@@ -33,6 +37,11 @@ GEM
|
|
33
37
|
pry (>= 0.9.10, < 0.15)
|
34
38
|
rainbow (3.1.1)
|
35
39
|
rake (13.0.6)
|
40
|
+
rbi (0.0.15)
|
41
|
+
ast
|
42
|
+
parser (>= 2.6.4.0)
|
43
|
+
sorbet-runtime (>= 0.5.9204)
|
44
|
+
unparser
|
36
45
|
regexp_parser (2.5.0)
|
37
46
|
rexml (3.2.5)
|
38
47
|
rubocop (1.30.1)
|
@@ -53,7 +62,38 @@ GEM
|
|
53
62
|
rubocop-ast (>= 0.4.0)
|
54
63
|
ruby-progressbar (1.11.0)
|
55
64
|
ruby2_keywords (0.0.5)
|
65
|
+
sorbet (0.5.10346)
|
66
|
+
sorbet-static (= 0.5.10346)
|
67
|
+
sorbet-runtime (0.5.10346)
|
68
|
+
sorbet-static (0.5.10346-universal-darwin-21)
|
69
|
+
sorbet-static-and-runtime (0.5.10346)
|
70
|
+
sorbet (= 0.5.10346)
|
71
|
+
sorbet-runtime (= 0.5.10346)
|
72
|
+
spoom (1.1.12)
|
73
|
+
sorbet (>= 0.5.9204)
|
74
|
+
sorbet-runtime (>= 0.5.9204)
|
75
|
+
thor (>= 0.19.2)
|
76
|
+
tapioca (0.9.4)
|
77
|
+
bundler (>= 1.17.3)
|
78
|
+
netrc (>= 0.11.0)
|
79
|
+
parallel (>= 1.21.0)
|
80
|
+
pry (>= 0.12.2)
|
81
|
+
rbi (~> 0.0.0, >= 0.0.14)
|
82
|
+
sorbet-static-and-runtime (>= 0.5.9204)
|
83
|
+
spoom (~> 1.1.0, >= 1.1.11)
|
84
|
+
thor (>= 1.2.0)
|
85
|
+
yard-sorbet
|
86
|
+
thor (1.2.1)
|
56
87
|
unicode-display_width (2.2.0)
|
88
|
+
unparser (0.6.5)
|
89
|
+
diff-lcs (~> 1.3)
|
90
|
+
parser (>= 3.1.0)
|
91
|
+
webrick (1.7.0)
|
92
|
+
yard (0.9.28)
|
93
|
+
webrick (~> 1.7.0)
|
94
|
+
yard-sorbet (0.7.0)
|
95
|
+
sorbet-runtime (>= 0.5)
|
96
|
+
yard (>= 0.9)
|
57
97
|
zeitwerk (2.6.0)
|
58
98
|
|
59
99
|
PLATFORMS
|
@@ -72,6 +112,10 @@ DEPENDENCIES
|
|
72
112
|
rubocop
|
73
113
|
rubocop-minitest
|
74
114
|
rubocop-performance
|
115
|
+
sorbet
|
116
|
+
sorbet-runtime
|
117
|
+
spoom
|
118
|
+
tapioca
|
75
119
|
whatsapp_sdk!
|
76
120
|
zeitwerk (>= 2.6.0)
|
77
121
|
|
data/README.md
CHANGED
@@ -59,15 +59,82 @@ medias_api = WhatsappSdk::Api::Medias.new
|
|
59
59
|
|
60
60
|
Note: Remember to initialize the client first!
|
61
61
|
|
62
|
+
## Set up a Meta app
|
63
|
+
|
64
|
+
<details><summary>1) Create a Meta Business app </summary>
|
65
|
+
<img width="1063" alt="Screen Shot 2022-09-05 at 11 03 47 AM" src="https://user-images.githubusercontent.com/11672878/188477795-4745a71a-a4b5-41e2-bef1-e41d3060e02b.png">
|
66
|
+
</details>
|
67
|
+
|
68
|
+
<details><summary>2) Add Whatsapp to your Application</summary>
|
69
|
+
<img width="1087" alt="Screen Shot 2022-09-05 at 11 05 43 AM" src="https://user-images.githubusercontent.com/11672878/188478100-98b3bf0a-fec7-4ea1-a492-aeb90a6b06bd.png">
|
70
|
+
</details>
|
71
|
+
|
72
|
+
<details><summary>3) Add a phone number to your account</summary>
|
73
|
+
<img width="972" alt="Screen Shot 2022-09-05 at 11 09 22 AM" src="https://user-images.githubusercontent.com/11672878/188478741-8a6105e8-2776-4493-bba9-05a62082a5aa.png">
|
74
|
+
</details>
|
75
|
+
|
76
|
+
Try sending a message to your phone in the UI.
|
77
|
+
|
78
|
+
<details><summary>4) Copy the ACCESS_TOKEN, the SENDER_ID, the BUSINESS_ID and the RECEIPIENT_NUMBER</summary>
|
79
|
+
<img width="1010" alt="Screen Shot 2022-09-05 at 11 13 24 AM" src="https://user-images.githubusercontent.com/11672878/188480634-369f8de1-b851-4735-86de-f49e96f78d8c.png">
|
80
|
+
</details>
|
81
|
+
|
82
|
+
</details>
|
83
|
+
|
84
|
+
<details><summary>5) Use the GEM to interact with Whatsapp</summary>
|
85
|
+
|
86
|
+
Example:
|
87
|
+
1) Install the gem by running `gem install whatsapp_sdk` in the gem.
|
88
|
+
2) Open the irb terminal by running `irb`
|
89
|
+
3) `require "whatsapp_sdk"`
|
90
|
+
4) Set up the `ACCESS_TOKEN`, the `SENDER_ID`, the `BUSINESS_ID` and the `RECEIPIENT_NUMBER` in variables.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
ACCESS_TOKEN = "EAAZAvvr0DZBs0BABRLF8zohP5Epc6pyNu"
|
94
|
+
BUSINESS_ID = 1213141516171819
|
95
|
+
SENDER_ID = 1234567891011
|
96
|
+
RECIPIENT_NUMBER = 12398765432
|
97
|
+
```
|
98
|
+
|
99
|
+
5) Configure the Client by running
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
WhatsappSdk.configure do |config|
|
103
|
+
config.access_token = ACCESS_TOKEN
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
6) Try the Medias or Messages API
|
108
|
+
|
109
|
+
Medias API
|
110
|
+
```ruby
|
111
|
+
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
|
112
|
+
registered_number = phone_numbers_api.registered_number(SENDER_ID)
|
113
|
+
```
|
114
|
+
|
115
|
+
Messages API
|
116
|
+
```ruby
|
117
|
+
messages_api = WhatsappSdk::Api::Messages.new
|
118
|
+
message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
119
|
+
message: "Hey there! it's Whatsapp Ruby SDK")
|
120
|
+
```
|
121
|
+
|
122
|
+
Check the [example.rb file](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/blob/main/example.rb) for more examples.
|
123
|
+
|
124
|
+
</details>
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
62
129
|
### Phone numbers API
|
63
130
|
Get the list of phone numbers registered
|
64
131
|
```ruby
|
65
|
-
phone_numbers_api.registered_numbers(
|
132
|
+
phone_numbers_api.registered_numbers(123456) # accepts a business_id
|
66
133
|
```
|
67
134
|
|
68
135
|
Get the a phone number by id
|
69
136
|
```ruby
|
70
|
-
phone_numbers_api.registered_numbers(
|
137
|
+
phone_numbers_api.registered_numbers(123456) # accepts a phone_number_id
|
71
138
|
```
|
72
139
|
|
73
140
|
### Media API
|
@@ -97,7 +164,7 @@ medias_api.delete(media_id: MEDIA_ID)
|
|
97
164
|
**Send a text message**
|
98
165
|
|
99
166
|
```ruby
|
100
|
-
messages_api.send_text(sender_id: 1234, recipient_number:
|
167
|
+
messages_api.send_text(sender_id: 1234, recipient_number: 112345678, message: "hola")
|
101
168
|
```
|
102
169
|
|
103
170
|
**Read a message**
|
@@ -112,7 +179,7 @@ Note: To get the `message_id` you can set up [Webhooks](https://developers.faceb
|
|
112
179
|
|
113
180
|
```ruby
|
114
181
|
messages_api.send_location(
|
115
|
-
sender_id: 123123, recipient_number:
|
182
|
+
sender_id: 123123, recipient_number: 56789,
|
116
183
|
longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
|
117
184
|
)
|
118
185
|
```
|
@@ -122,12 +189,12 @@ It could use a link or an image_id.
|
|
122
189
|
```ruby
|
123
190
|
# with a link
|
124
191
|
messages_api.send_image(
|
125
|
-
sender_id: 123123, recipient_number:
|
192
|
+
sender_id: 123123, recipient_number: 56789, link: "image_link", caption: "Ignacio Chiazzo Profile"
|
126
193
|
)
|
127
194
|
|
128
195
|
# with an image id
|
129
196
|
messages_api.send_image(
|
130
|
-
sender_id: 123123, recipient_number:
|
197
|
+
sender_id: 123123, recipient_number: 56789, image_id: "1234", caption: "Ignacio Chiazzo Profile"
|
131
198
|
)
|
132
199
|
```
|
133
200
|
|
@@ -135,10 +202,10 @@ messages_api.send_image(
|
|
135
202
|
It could use a link or an audio_id.
|
136
203
|
```ruby
|
137
204
|
# with a link
|
138
|
-
messages_api.send_audio(sender_id: 123123, recipient_number:
|
205
|
+
messages_api.send_audio(sender_id: 123123, recipient_number: 56789, link: "audio_link")
|
139
206
|
|
140
207
|
# with an audio id
|
141
|
-
messages_api.send_audio(sender_id: 123123, recipient_number:
|
208
|
+
messages_api.send_audio(sender_id: 123123, recipient_number: 56789, audio_id: "1234")
|
142
209
|
```
|
143
210
|
|
144
211
|
**Send a document message**
|
@@ -146,12 +213,12 @@ It could use a link or a document_id.
|
|
146
213
|
```ruby
|
147
214
|
# with a link
|
148
215
|
messages_api.send_document(
|
149
|
-
sender_id: 123123, recipient_number:
|
216
|
+
sender_id: 123123, recipient_number: 56789, link: "document_link", caption: "Ignacio Chiazzo"
|
150
217
|
)
|
151
218
|
|
152
219
|
# with a document id
|
153
220
|
messages_api.send_document(
|
154
|
-
sender_id: 123123, recipient_number:
|
221
|
+
sender_id: 123123, recipient_number: 56789, document_id: "1234", caption: "Ignacio Chiazzo"
|
155
222
|
)
|
156
223
|
```
|
157
224
|
|
@@ -159,10 +226,10 @@ messages_api.send_document(
|
|
159
226
|
It could use a link or a sticker_id.
|
160
227
|
```ruby
|
161
228
|
# with a link
|
162
|
-
messages_api.send_sticker(sender_id: 123123, recipient_number:
|
229
|
+
messages_api.send_sticker(sender_id: 123123, recipient_number: 56789, link: "link")
|
163
230
|
|
164
231
|
# with a sticker_id
|
165
|
-
messages_api.send_sticker(sender_id: 123123, recipient_number:
|
232
|
+
messages_api.send_sticker(sender_id: 123123, recipient_number: 56789, sticker_id: "1234")
|
166
233
|
```
|
167
234
|
|
168
235
|
**Send contacts message**
|
@@ -170,12 +237,12 @@ To send a contact, you need to create a Contact instance object that contain obj
|
|
170
237
|
|
171
238
|
```ruby
|
172
239
|
contacts = [create_contact(params)]
|
173
|
-
messages_api.send_contacts(sender_id: 123123, recipient_number:
|
240
|
+
messages_api.send_contacts(sender_id: 123123, recipient_number: 56789, contacts: contacts)
|
174
241
|
```
|
175
242
|
|
176
243
|
Alernative, you could pass a plain json like this:
|
177
244
|
```ruby
|
178
|
-
messages_api.send_contacts(sender_id: 123123, recipient_number:
|
245
|
+
messages_api.send_contacts(sender_id: 123123, recipient_number: 56789, contacts_json: {...})
|
179
246
|
```
|
180
247
|
|
181
248
|
**Send a template message**
|
@@ -190,46 +257,48 @@ currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallba
|
|
190
257
|
date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
|
191
258
|
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
|
192
259
|
|
193
|
-
parameter_image = WhatsappSdk::Resource::ParameterObject.new(type:
|
194
|
-
|
195
|
-
|
196
|
-
|
260
|
+
parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Image, image: image)
|
261
|
+
# You can also use a plain string as type e.g.
|
262
|
+
# parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
|
263
|
+
parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Text, text: "TEXT_STRING")
|
264
|
+
parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::Currency, currency: currency)
|
265
|
+
parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: WhatsappSdk::Resource::ParameterObject::Type::DateTime, date_time: date_time)
|
197
266
|
|
198
267
|
header_component = WhatsappSdk::Resource::Component.new(
|
199
|
-
type: WhatsappSdk::Resource::Component::Type::
|
268
|
+
type: WhatsappSdk::Resource::Component::Type::Header,
|
200
269
|
parameters: [parameter_image]
|
201
270
|
)
|
202
271
|
|
203
272
|
body_component = WhatsappSdk::Resource::Component.new(
|
204
|
-
type: WhatsappSdk::Resource::Component::Type::
|
273
|
+
type: WhatsappSdk::Resource::Component::Type::Body,
|
205
274
|
parameters: [parameter_text, parameter_currency, parameter_date_time]
|
206
275
|
)
|
207
276
|
|
208
277
|
button_component1 = WhatsappSdk::Resource::Component.new(
|
209
|
-
type: WhatsappSdk::Resource::Component::Type::
|
278
|
+
type: WhatsappSdk::Resource::Component::Type::Button,
|
210
279
|
index: 0,
|
211
|
-
sub_type: WhatsappSdk::Resource::Component::Subtype::
|
280
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
|
212
281
|
parameters: [
|
213
|
-
WhatsappSdk::Resource::ButtonParameter.new(type:
|
282
|
+
WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload, payload: "PAYLOAD")
|
214
283
|
]
|
215
284
|
)
|
216
285
|
|
217
286
|
button_component2 = WhatsappSdk::Resource::Component.new(
|
218
|
-
type: WhatsappSdk::Resource::Component::Type::
|
287
|
+
type: WhatsappSdk::Resource::Component::Type::Button,
|
219
288
|
index: 1,
|
220
|
-
sub_type: WhatsappSdk::Resource::Component::Subtype::
|
289
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
|
221
290
|
parameters: [
|
222
|
-
WhatsappSdk::Resource::ButtonParameter.new(type:
|
291
|
+
WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload, payload: "PAYLOAD")
|
223
292
|
]
|
224
293
|
)
|
225
|
-
@messages_api.send_template(sender_id: 12_345, recipient_number:
|
294
|
+
@messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [component_1])
|
226
295
|
```
|
227
296
|
|
228
297
|
</details>
|
229
298
|
|
230
299
|
Alernative, you could pass a plain json like this:
|
231
300
|
```ruby
|
232
|
-
@messages_api.send_template(sender_id: 12_345, recipient_number:
|
301
|
+
@messages_api.send_template(sender_id: 12_345, recipient_number: 12345678, name: "hello_world", language: "en_US", components_json: [{...}])
|
233
302
|
```
|
234
303
|
|
235
304
|
## Example
|
@@ -256,6 +325,11 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
256
325
|
|
257
326
|
Bug reports and pull requests are welcome on GitHub at [https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk](https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk) This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
258
327
|
|
328
|
+
### Run all the tests
|
329
|
+
- **Unit tests:** Run `rake test`
|
330
|
+
- **Sorbet Typecheck:** run `srb tc`
|
331
|
+
- **Linters:** `bundle exec rubocop`
|
332
|
+
|
259
333
|
## License
|
260
334
|
|
261
335
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
data/bin/tapioca
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
# typed: true
|
4
|
+
|
5
|
+
#
|
6
|
+
# This file was generated by Bundler.
|
7
|
+
#
|
8
|
+
# The application 'tapioca' is installed as part of a gem, and
|
9
|
+
# this file is here to facilitate running it.
|
10
|
+
#
|
11
|
+
|
12
|
+
require "pathname"
|
13
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
14
|
+
Pathname.new(__FILE__).realpath)
|
15
|
+
|
16
|
+
bundle_binstub = File.expand_path('bundle', __dir__)
|
17
|
+
|
18
|
+
if File.file?(bundle_binstub)
|
19
|
+
if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 300))
|
20
|
+
load(bundle_binstub)
|
21
|
+
else
|
22
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
23
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require "rubygems"
|
28
|
+
require "bundler/setup"
|
29
|
+
|
30
|
+
load Gem.bin_path("tapioca", "tapioca")
|
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"
|
15
15
|
gem "pry"
|
16
16
|
gem "pry-nav"
|
17
17
|
end
|
@@ -22,13 +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 = "
|
30
|
-
binding.pry
|
31
|
-
# message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "holis")
|
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>"
|
32
30
|
|
33
31
|
################# Initialize Client #################
|
34
32
|
WhatsappSdk.configure do |config|
|
@@ -50,26 +48,26 @@ messages_api = WhatsappSdk::Api::Messages.new
|
|
50
48
|
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
|
51
49
|
|
52
50
|
############################## Phone Numbers API ##############################
|
53
|
-
phone_numbers_api.registered_number(SENDER_ID)
|
54
|
-
phone_numbers_api.registered_numbers(BUSINESS_ID)
|
51
|
+
registered_number = phone_numbers_api.registered_number(SENDER_ID)
|
52
|
+
registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
|
55
53
|
############################## Media API ##############################
|
56
54
|
|
57
55
|
# upload a media
|
58
56
|
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
59
|
-
|
57
|
+
media_id = uploaded_media.data&.id
|
58
|
+
puts "Uploaded media id: #{media_id}"
|
60
59
|
|
61
60
|
# get a media
|
62
|
-
|
63
|
-
media = medias_api.media(media_id: uploaded_media.data&.id).data
|
61
|
+
media = medias_api.media(media_id: media_id).data
|
64
62
|
puts "Media info: #{media.raw_data_response}"
|
65
63
|
|
66
64
|
# download media
|
67
65
|
download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
|
68
66
|
puts "Downloaded: #{download_image.data.success?}"
|
69
67
|
|
70
|
-
#
|
71
|
-
|
72
|
-
|
68
|
+
# delete a media
|
69
|
+
deleted_media = medias_api.delete(media_id: media&.id)
|
70
|
+
puts "Deleted: #{deleted_media.data.success?}"
|
73
71
|
|
74
72
|
############################## Messages API ##############################
|
75
73
|
|
@@ -134,12 +132,12 @@ puts response_with_object
|
|
134
132
|
|
135
133
|
# Send a template with components.Remember to create the template first.
|
136
134
|
header_component = WhatsappSdk::Resource::Component.new(
|
137
|
-
type: WhatsappSdk::Resource::Component::Type::
|
135
|
+
type: WhatsappSdk::Resource::Component::Type::Header
|
138
136
|
)
|
139
137
|
|
140
138
|
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
|
141
139
|
document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
|
142
|
-
video = WhatsappSdk::Resource::Media.new(type: "video", id: 123)
|
140
|
+
video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
|
143
141
|
|
144
142
|
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
|
145
143
|
type: "image",
|
@@ -168,7 +166,7 @@ header_component.add_parameter(parameter_document)
|
|
168
166
|
header_component.to_json
|
169
167
|
|
170
168
|
body_component = WhatsappSdk::Resource::Component.new(
|
171
|
-
type: WhatsappSdk::Resource::Component::Type::
|
169
|
+
type: WhatsappSdk::Resource::Component::Type::Body
|
172
170
|
)
|
173
171
|
body_component.add_parameter(parameter_text)
|
174
172
|
body_component.add_parameter(parameter_image)
|
@@ -176,19 +174,21 @@ body_component.add_parameter(parameter_video)
|
|
176
174
|
body_component.add_parameter(parameter_document)
|
177
175
|
body_component.to_json
|
178
176
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
177
|
+
button_component_1 = WhatsappSdk::Resource::Component.new(
|
178
|
+
type: WhatsappSdk::Resource::Component::Type::Button,
|
179
|
+
index: 0,
|
180
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
|
181
|
+
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
|
182
|
+
payload: "payload")]
|
183
|
+
)
|
185
184
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
185
|
+
button_component_2 = WhatsappSdk::Resource::Component.new(
|
186
|
+
type: WhatsappSdk::Resource::Component::Type::Button,
|
187
|
+
index: 1,
|
188
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
|
189
|
+
parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
|
190
|
+
payload: "payload")]
|
191
|
+
)
|
192
192
|
|
193
193
|
# Send a template with component_json
|
194
194
|
response_with_json = messages_api.send_template(
|
@@ -208,8 +208,3 @@ response_with_json = messages_api.send_template(
|
|
208
208
|
]
|
209
209
|
)
|
210
210
|
puts response_with_json
|
211
|
-
|
212
|
-
|
213
|
-
# # delete a media
|
214
|
-
deleted_media = medias_api.delete(media_id: media&.id)
|
215
|
-
puts "Deleted: #{deleted_media.data.success?}"
|
data/lib/version.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
require "faraday"
|
4
5
|
require "oj"
|
@@ -6,20 +7,29 @@ require "oj"
|
|
6
7
|
module WhatsappSdk
|
7
8
|
module Api
|
8
9
|
class Client
|
9
|
-
|
10
|
-
API_CLIENT = "https://graph.facebook.com/#{API_VERSION}/"
|
10
|
+
extend T::Sig
|
11
11
|
|
12
|
+
API_VERSION = T.let("v14.0", String)
|
13
|
+
API_CLIENT = T.let("https://graph.facebook.com/#{API_VERSION}/", String)
|
14
|
+
|
15
|
+
sig { params(access_token: String).void }
|
12
16
|
def initialize(access_token)
|
13
17
|
@access_token = access_token
|
14
18
|
end
|
15
19
|
|
20
|
+
sig do
|
21
|
+
params(
|
22
|
+
endpoint: String, full_url: T.nilable(String), http_method: String, params: T::Hash[T.untyped, T.untyped]
|
23
|
+
).returns(T::Hash[T.untyped, T.untyped])
|
24
|
+
end
|
16
25
|
def send_request(endpoint: "", full_url: nil, http_method: "post", params: {})
|
17
26
|
url = full_url || API_CLIENT
|
18
27
|
|
19
|
-
response = faraday(url).public_send(http_method, endpoint, params)
|
28
|
+
response = T.unsafe(faraday(url)).public_send(http_method, endpoint, params)
|
20
29
|
Oj.load(response.body)
|
21
30
|
end
|
22
31
|
|
32
|
+
sig { params(url: String, path_to_file_name: T.nilable(String)).returns(Net::HTTPResponse) }
|
23
33
|
def download_file(url, path_to_file_name = nil)
|
24
34
|
uri = URI.parse(url)
|
25
35
|
request = Net::HTTP::Get.new(uri)
|
@@ -37,6 +47,7 @@ module WhatsappSdk
|
|
37
47
|
|
38
48
|
private
|
39
49
|
|
50
|
+
sig { params(url: String).returns(Faraday::Connection) }
|
40
51
|
def faraday(url)
|
41
52
|
::Faraday.new(url) do |client|
|
42
53
|
client.request :multipart
|