whatsapp_sdk 0.3.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +14 -0
  4. data/CHANGELOG.md +3 -1
  5. data/Gemfile +5 -0
  6. data/Gemfile.lock +45 -1
  7. data/README.md +102 -28
  8. data/Rakefile +1 -0
  9. data/bin/tapioca +30 -0
  10. data/example.rb +30 -34
  11. data/lib/version.rb +2 -1
  12. data/lib/whatsapp_sdk/api/client.rb +14 -3
  13. data/lib/whatsapp_sdk/api/medias.rb +12 -3
  14. data/lib/whatsapp_sdk/api/messages.rb +72 -14
  15. data/lib/whatsapp_sdk/api/phone_numbers.rb +3 -0
  16. data/lib/whatsapp_sdk/api/request.rb +2 -2
  17. data/lib/whatsapp_sdk/api/response.rb +17 -1
  18. data/lib/whatsapp_sdk/api/responses/data_response.rb +10 -3
  19. data/lib/whatsapp_sdk/api/responses/error_response.rb +8 -1
  20. data/lib/whatsapp_sdk/api/responses/media_data_response.rb +26 -7
  21. data/lib/whatsapp_sdk/api/responses/message_data_response.rb +20 -3
  22. data/lib/whatsapp_sdk/api/responses/message_error_response.rb +26 -7
  23. data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +18 -5
  24. data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +9 -1
  25. data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +10 -3
  26. data/lib/whatsapp_sdk/api/responses/success_response.rb +5 -1
  27. data/lib/whatsapp_sdk/configuration.rb +7 -3
  28. data/lib/whatsapp_sdk/error.rb +1 -0
  29. data/lib/whatsapp_sdk/resource/address.rb +30 -6
  30. data/lib/whatsapp_sdk/resource/address_type.rb +15 -0
  31. data/lib/whatsapp_sdk/resource/button_parameter.rb +19 -23
  32. data/lib/whatsapp_sdk/resource/component.rb +45 -13
  33. data/lib/whatsapp_sdk/resource/contact.rb +30 -1
  34. data/lib/whatsapp_sdk/resource/contact_response.rb +9 -1
  35. data/lib/whatsapp_sdk/resource/currency.rb +9 -1
  36. data/lib/whatsapp_sdk/resource/date_time.rb +7 -1
  37. data/lib/whatsapp_sdk/resource/email.rb +9 -5
  38. data/lib/whatsapp_sdk/resource/media.rb +44 -15
  39. data/lib/whatsapp_sdk/resource/message.rb +5 -0
  40. data/lib/whatsapp_sdk/resource/name.rb +28 -1
  41. data/lib/whatsapp_sdk/resource/org.rb +13 -1
  42. data/lib/whatsapp_sdk/resource/parameter_object.rb +82 -39
  43. data/lib/whatsapp_sdk/resource/phone_number.rb +12 -5
  44. data/lib/whatsapp_sdk/resource/url.rb +9 -5
  45. data/lib/whatsapp_sdk.rb +8 -0
  46. data/sorbet/config +6 -0
  47. data/sorbet/rbi/annotations/faraday.rbi +17 -0
  48. data/sorbet/rbi/annotations/mocha.rbi +34 -0
  49. data/sorbet/rbi/annotations/rainbow.rbi +269 -0
  50. data/sorbet/rbi/gems/faraday-multipart@1.0.4.rbi +270 -0
  51. data/sorbet/rbi/gems/faraday-net_http@2.0.3.rbi +182 -0
  52. data/sorbet/rbi/gems/faraday@2.3.0.rbi +2494 -0
  53. data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
  54. data/sorbet/rbi/gems/minitest@5.16.1.rbi +1459 -0
  55. data/sorbet/rbi/gems/mocha@1.14.0.rbi +60 -0
  56. data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +239 -0
  57. data/sorbet/rbi/gems/netrc@0.11.0.rbi +150 -0
  58. data/sorbet/rbi/gems/oj@3.13.14.rbi +589 -0
  59. data/sorbet/rbi/gems/zeitwerk@2.6.0.rbi +867 -0
  60. data/sorbet/rbi/todo.rbi +8 -0
  61. data/sorbet/shims/request.rbi +10 -0
  62. data/sorbet/tapioca/config.yml +13 -0
  63. data/sorbet/tapioca/require.rb +5 -0
  64. data/whatsapp_sdk.gemspec +4 -1
  65. metadata +50 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21f67768ad0e420df08a3ba0046968b6576e4d60bf0f601d902c968d3cfa9a29
4
- data.tar.gz: 1db186a1de042f4da848587a9c4fd366b52ca28111de7fff63ae93ef776e98d8
3
+ metadata.gz: 70e66d8860068d5cab7de45cf1972de69e5ddbb24e8195fbfa6643eef55a6da8
4
+ data.tar.gz: 5894ded02a7d2ed5440801f5431146451d83753a630eb4c795e25c141f207885
5
5
  SHA512:
6
- metadata.gz: 933a7f3caa079cb0e551bbb772516c3ab03f0f57bb97ef05934acad663ef600cacfcf1ff0b65c65b87596df2424aaa3e6f293134f9ddce9a55d0bfd0dcb833cd
7
- data.tar.gz: 429575992b1728097f53daba070ad74e4622f4ae8e21c73e40ecf06947b8c9697e5fd0272ab105f10f7cb9e023ff2dc2574f6268d5a34b318d7b422fe95c51a5
6
+ metadata.gz: d3c9b6522b7d8ef331a635bddaa0cb5aa2f4821bb1a755e0395a6af844fdd5da3602f8b8fde04601f0c4389f80661d4ec4d42b46693b1f3baa55e4cc9e56ee1e
7
+ data.tar.gz: 4951df21c4943bba0992c698fed0f8870604889cf2b3145ac069a6daf61bf7910b3761fe736f2a2bbf0fbf867e5c6c9ba22f04456041e20ee3d7b38b8d8966ed
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ REMOVE.rb
21
21
  .ruby-gemset
22
22
 
23
23
  .rvmrc
24
+ .DS_Store
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
- # Unreleased
1
+ # v 0.4.0
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
@@ -1,10 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- whatsapp_sdk (0.3.2)
4
+ whatsapp_sdk (0.5.0)
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("123456") # accepts a business_id
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("123456") # accepts a phone_number_id
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: "112345678", message: "hola")
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: "56789",
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: "56789", link: "image_link", caption: "Ignacio Chiazzo Profile"
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: "56789", image_id: "1234", caption: "Ignacio Chiazzo Profile"
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: "56789", link: "audio_link")
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: "56789", audio_id: "1234")
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: "56789", link: "document_link", caption: "Ignacio Chiazzo"
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: "56789", document_id: "1234", caption: "Ignacio Chiazzo"
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: "56789", link: "link")
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: "56789", sticker_id: "1234")
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: "56789", contacts: contacts)
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: "56789", contacts_json: {...})
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: "image", image: image)
194
- parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING")
195
- parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency)
196
- parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time)
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::HEADER,
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::BODY,
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::BUTTON,
278
+ type: WhatsappSdk::Resource::Component::Type::Button,
210
279
  index: 0,
211
- sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
280
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
212
281
  parameters: [
213
- WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
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::BUTTON,
287
+ type: WhatsappSdk::Resource::Component::Type::Button,
219
288
  index: 1,
220
- sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
289
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
221
290
  parameters: [
222
- WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
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: "12345678", name: "hello_world", language: "en_US", components_json: [component_1])
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: "12345678", name: "hello_world", language: "en_US", components_json: [{...}])
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
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+ # typed: true
2
3
 
3
4
  require "bundler/gem_tasks"
4
5
  require "rake/testtask"
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
@@ -21,14 +21,12 @@ require "pry"
21
21
  require "pry-nav"
22
22
 
23
23
  ################# UPDATE CONSTANTS #################
24
+ ACCESS_TOKEN = "EAAVRvYO3LQMBANyzA38RmXZA83VGGwYblwZASqrEU50dZBki0vqEsGSlglL2nuyD9XVbX0qxbSQURnzW5jkwvfrz8J0P3XW3CSM1R03n81ZBjd1dLHsYIIPdhF8NjMy8hnwYDzTFLSabpkWlD7LQ7VWQAx5k64XgNv02DFLt2EhgVhib09SmEj6ZBSKZBknasHjaTUcgX0iQZDZD" # replace this with a valid access_token # TODO replace
25
+ BUSINESS_ID = 101292429380714
26
+ SENDER_ID = 108129082031154
27
+ RECIPIENT_NUMBER = 5511970395230
24
28
 
25
- ACCESS_TOKEN = "EAAHwuHDNkqoBAOtjFZAw1MUOXsFOv4EMP8Hi2mtzvcHSPbYWD4HLLLRapjPIDQWagke3cF2IIZCbtlhMZCluO1sH7C594q0q9UqZAXwufTqBtebRlssZCLwKeC69l6Emdmt2gRYGDnhTJH0mrM7L5ivZAWezhjOvtzdnKJ7xBZALiQRKB6JrEAgAZBcnBMfBZC75xR0ZACXQUPoplhjN1a5FTA" # replace this with a valid access_token # TODO replace
26
- SENDER_ID = 107_878_721_936_019
27
- RECIPIENT_NUMBER = 13_437_772_910
28
- BUSINESS_ID = 114_503_234_599_312
29
29
  IMAGE_LINK = "https://ignaciochiazzo.com/static/4c403819b9750c8ad8b20a75308f2a8a/876d5/profile-pic.avif"
30
- binding.pry
31
- # message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "holis")
32
30
 
33
31
  ################# Initialize Client #################
34
32
  WhatsappSdk.configure do |config|
@@ -48,28 +46,29 @@ end
48
46
  medias_api = WhatsappSdk::Api::Medias.new
49
47
  messages_api = WhatsappSdk::Api::Messages.new
50
48
  phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
49
+ binding.pry
51
50
 
52
51
  ############################## Phone Numbers API ##############################
53
- phone_numbers_api.registered_number(SENDER_ID)
54
- phone_numbers_api.registered_numbers(BUSINESS_ID)
52
+ registered_number = phone_numbers_api.registered_number(SENDER_ID)
53
+ registered_numbers = phone_numbers_api.registered_numbers(BUSINESS_ID)
55
54
  ############################## Media API ##############################
56
55
 
57
56
  # upload a media
58
57
  uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
59
- puts "Uploaded media id: #{uploaded_media.data&.id}"
58
+ media_id = uploaded_media.data&.id
59
+ puts "Uploaded media id: #{media_id}"
60
60
 
61
61
  # get a media
62
- uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
63
- media = medias_api.media(media_id: uploaded_media.data&.id).data
62
+ media = medias_api.media(media_id: media_id).data
64
63
  puts "Media info: #{media.raw_data_response}"
65
64
 
66
65
  # download media
67
66
  download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
68
67
  puts "Downloaded: #{download_image.data.success?}"
69
68
 
70
- # # delete a media
71
- # deleted_media = medias_api.delete(media_id: media&.id)
72
- # puts "Deleted: #{deleted_media.data.success?}"
69
+ # delete a media
70
+ deleted_media = medias_api.delete(media_id: media&.id)
71
+ puts "Deleted: #{deleted_media.data.success?}"
73
72
 
74
73
  ############################## Messages API ##############################
75
74
 
@@ -134,12 +133,12 @@ puts response_with_object
134
133
 
135
134
  # Send a template with components.Remember to create the template first.
136
135
  header_component = WhatsappSdk::Resource::Component.new(
137
- type: WhatsappSdk::Resource::Component::Type::HEADER
136
+ type: WhatsappSdk::Resource::Component::Type::Header
138
137
  )
139
138
 
140
139
  image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
141
140
  document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
142
- video = WhatsappSdk::Resource::Media.new(type: "video", id: 123)
141
+ video = WhatsappSdk::Resource::Media.new(type: "video", id: "123")
143
142
 
144
143
  parameter_image = WhatsappSdk::Resource::ParameterObject.new(
145
144
  type: "image",
@@ -168,7 +167,7 @@ header_component.add_parameter(parameter_document)
168
167
  header_component.to_json
169
168
 
170
169
  body_component = WhatsappSdk::Resource::Component.new(
171
- type: WhatsappSdk::Resource::Component::Type::BODY
170
+ type: WhatsappSdk::Resource::Component::Type::Body
172
171
  )
173
172
  body_component.add_parameter(parameter_text)
174
173
  body_component.add_parameter(parameter_image)
@@ -176,19 +175,21 @@ body_component.add_parameter(parameter_video)
176
175
  body_component.add_parameter(parameter_document)
177
176
  body_component.to_json
178
177
 
179
- # button_component_1 = WhatsappSdk::Resource::Component.new(
180
- # type: WhatsappSdk::Resource::Component::Type::BUTTON,
181
- # index: 0,
182
- # sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
183
- # parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
184
- # )
178
+ button_component_1 = WhatsappSdk::Resource::Component.new(
179
+ type: WhatsappSdk::Resource::Component::Type::Button,
180
+ index: 0,
181
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
182
+ parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
183
+ payload: "payload")]
184
+ )
185
185
 
186
- # button_component_2 = WhatsappSdk::Resource::Component.new(
187
- # type: WhatsappSdk::Resource::Component::Type::BUTTON,
188
- # index: 1,
189
- # sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
190
- # parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
191
- # )
186
+ button_component_2 = WhatsappSdk::Resource::Component.new(
187
+ type: WhatsappSdk::Resource::Component::Type::Button,
188
+ index: 1,
189
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QuickReply,
190
+ parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: WhatsappSdk::Resource::ButtonParameter::Type::Payload,
191
+ payload: "payload")]
192
+ )
192
193
 
193
194
  # Send a template with component_json
194
195
  response_with_json = messages_api.send_template(
@@ -208,8 +209,3 @@ response_with_json = messages_api.send_template(
208
209
  ]
209
210
  )
210
211
  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,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ # typed: strict
2
3
 
3
4
  module WhatsappSdk
4
- VERSION = "0.3.2"
5
+ VERSION = "0.5.0"
5
6
  end
@@ -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
- API_VERSION = "v14.0"
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