whatsapp_sdk 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 789c30c096735aa557c643a11a810990c8bc2c207360bee7d0b0e9710f2de60c
4
- data.tar.gz: 66bf61944f214767a8feae139e62d544be51c10e32895b33ca5239b3b1582009
3
+ metadata.gz: 3dabd5d87a1a3988caaafc2a090c9b85bcea77cc764132a169227a5ca1c4b409
4
+ data.tar.gz: 1ac41750e5e737fbf9a606e013015d796b68c328e3950241cad051c8eef6d674
5
5
  SHA512:
6
- metadata.gz: cc2eef92ce66f87374e1f2fb0f5cfb01f9bbf5814ae80d625403660fd1fc9d834680fb72a06941c8d66f0fb30e9dabe5561231c4547cc2fd1daf651300b93193
7
- data.tar.gz: fe548e9bfee0913a4eca4b1675342c565d35821e5d7b384908eebc0073b03616d7bc762ca8afefd1284dd6bd6ddffe88904b339e6448622b7675f09e68ed62e9
6
+ metadata.gz: 183f8600f1e74a46b4a38f290c806f479a0e52a20ec7c6d836b2fe3ac8038e4c91620e83d8780810907a2395555169aa8911fd5385299adcd3830a3492bea9a7
7
+ data.tar.gz: ce2cb90d04e06dc62d0294bcd395b3cf6cdffb2e60c24a8671539825de486299b11647eab9cf875007f290b8a35af350aadb7a985cad4bad2bcc415d85955cd3
data/.rubocop.yml CHANGED
@@ -1,3 +1,10 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-performance
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+
1
8
  Style/StringLiterals:
2
9
  Enabled: false
3
10
 
@@ -11,7 +18,10 @@ Layout/LineLength:
11
18
  Max: 120
12
19
 
13
20
  Metrics/MethodLength:
14
- Max: 30
21
+ Max: 40
22
+
23
+ Naming/VariableNumber:
24
+ Enabled: false
15
25
 
16
26
  Metrics/ClassLength:
17
27
  Enabled: false
@@ -21,4 +31,7 @@ Metrics/AbcSize:
21
31
 
22
32
  # Wait until https://github.com/rubocop/rubocop/issues/8761 is fixed
23
33
  Gemspec/RequiredRubyVersion:
24
- Enabled: false
34
+ Enabled: false
35
+
36
+ Minitest/MultipleAssertions:
37
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  # Unreleased
2
2
 
3
+ # v 0.3.0
4
+ - Allow Apps to have a singleton global authentication client.
5
+
6
+ # v 0.2.0
7
+ - Added Media API
8
+ - Update Facebook API to v14
9
+ - Added error and sucess responses
10
+ - Added faraday-multiplart as part of the library
11
+
12
+ # v 0.1.0
13
+ - Added Message Template API.
14
+ - Added Currency and Datetime resources.
15
+ - Added Media resource.
16
+ - Added Component and ParameterObject resource.
17
+ - Fixed bug in recipient_number in Messages API.
18
+
3
19
  # v 0.0.2
4
20
  - Implement read message.
5
21
  - Implement Yard doc.
6
-
data/Gemfile CHANGED
@@ -4,18 +4,21 @@ source "https://rubygems.org"
4
4
 
5
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
- # Specify your gem's dependencies in whatsapp_sdk.gemspec
8
7
  gem("faraday")
8
+ gem("faraday-multipart")
9
9
  gem("oj")
10
+ gem("rake", ">= 12.3.3")
10
11
 
11
12
  group(:test) do
12
13
  gem('mocha')
14
+ gem('rubocop', require: false)
15
+ gem('rubocop-minitest', require: false)
16
+ gem('rubocop-performance', require: false)
13
17
  end
14
18
 
15
19
  group(:development) do
16
20
  gem('pry')
17
21
  gem('pry-nav')
18
- gem('rubocop', require: false)
19
22
  end
20
23
 
21
24
  gemspec
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- whatsapp_sdk (0.0.1)
4
+ whatsapp_sdk (0.2.0)
5
5
  faraday (~> 2.3.0)
6
+ faraday-multipart (~> 1.0.4)
6
7
  oj (~> 3.13.13)
7
8
 
8
9
  GEM
@@ -13,11 +14,14 @@ GEM
13
14
  faraday (2.3.0)
14
15
  faraday-net_http (~> 2.0)
15
16
  ruby2_keywords (>= 0.0.4)
17
+ faraday-multipart (1.0.4)
18
+ multipart-post (~> 2)
16
19
  faraday-net_http (2.0.3)
17
20
  method_source (1.0.0)
18
- minitest (5.15.0)
21
+ minitest (5.16.1)
19
22
  mocha (1.14.0)
20
- oj (3.13.13)
23
+ multipart-post (2.2.3)
24
+ oj (3.13.14)
21
25
  parallel (1.22.1)
22
26
  parser (3.1.2.0)
23
27
  ast (~> 2.4.1)
@@ -27,10 +31,10 @@ GEM
27
31
  pry-nav (1.0.0)
28
32
  pry (>= 0.9.10, < 0.15)
29
33
  rainbow (3.1.1)
30
- rake (10.5.0)
34
+ rake (13.0.6)
31
35
  regexp_parser (2.5.0)
32
36
  rexml (3.2.5)
33
- rubocop (1.30.0)
37
+ rubocop (1.30.1)
34
38
  parallel (~> 1.10)
35
39
  parser (>= 3.1.0.0)
36
40
  rainbow (>= 2.2.2, < 4.0)
@@ -41,9 +45,14 @@ GEM
41
45
  unicode-display_width (>= 1.4.0, < 3.0)
42
46
  rubocop-ast (1.18.0)
43
47
  parser (>= 3.1.1.0)
48
+ rubocop-minitest (0.20.1)
49
+ rubocop (>= 0.90, < 2.0)
50
+ rubocop-performance (1.14.2)
51
+ rubocop (>= 1.7.0, < 2.0)
52
+ rubocop-ast (>= 0.4.0)
44
53
  ruby-progressbar (1.11.0)
45
54
  ruby2_keywords (0.0.5)
46
- unicode-display_width (2.1.0)
55
+ unicode-display_width (2.2.0)
47
56
 
48
57
  PLATFORMS
49
58
  x86_64-darwin-21
@@ -51,13 +60,16 @@ PLATFORMS
51
60
  DEPENDENCIES
52
61
  bundler (~> 2.3)
53
62
  faraday
63
+ faraday-multipart
54
64
  minitest (~> 5.0)
55
65
  mocha
56
66
  oj
57
67
  pry
58
68
  pry-nav
59
- rake (~> 10.0)
69
+ rake (>= 12.3.3)
60
70
  rubocop
71
+ rubocop-minitest
72
+ rubocop-performance
61
73
  whatsapp_sdk!
62
74
 
63
75
  BUNDLED WITH
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # Ruby Whatsapp SDK
2
+ [![Gem Version](https://badge.fury.io/rb/whatsapp_sdk.svg)](https://badge.fury.io/rb/whatsapp_sdk)
3
+ [![CircleCI](https://circleci.com/gh/circleci/circleci-docs.svg?style=svg)](https://circleci.com/gh/ignacio-chiazzo/ruby_whatsapp_sdk)
4
+ <a href="https://codeclimate.com/github/ignacio-chiazzo/ruby_whatsapp_sdk/maintainability"><img src="https://api.codeclimate.com/v1/badges/169cce95450272e4ad7d/maintainability" /></a>
2
5
 
3
6
  The SDK provides a set of operations and classes to use the Whatsapp API.
4
7
  Send stickers, messages, audio, videos, locations or just ask for the phone numbers through this library in a few steps!
5
8
 
9
+
10
+ https://user-images.githubusercontent.com/11672878/173238826-6fc0a6f8-d0ee-4eae-8947-7dfd3b8b3446.mov
11
+
12
+
6
13
  ## Installation
7
14
 
8
15
  Add this line to your application's Gemfile:
@@ -21,12 +28,22 @@ Or install it yourself as:
21
28
 
22
29
  ## Quick Start
23
30
 
24
- There are two primary resources, `Messages` and `PhoneNumbers`. The first one allows clients to send any kind of message (text, audio, location, video, image, etc.), and the latter will enable clients to query the phone numbers associated.
31
+ There are three primary resources, `Messages`, `Media` and `PhoneNumbers`. The first one allows clients to send any kind of message (text, audio, location, video, image, etc.), and the latter will enable clients to query the phone numbers associated.
32
+
33
+ To use `Messages`, `Media` or `PhoneNumbers` you need to initialize the `Client` that contain auth information. There are two ways to do it
25
34
 
26
- To use `Messages` or `PhoneNumbers` you need to create a `Client` instance by passing the `access_token` like this:
35
+ 1) Using an initializer
36
+
37
+ ```ruby
38
+ WhatsappSdk.configure do |config|
39
+ config.access_token = ACCESS_TOKEN
40
+ end
41
+ ```
42
+ OR creating an instance and pass it to the Messages or PhoneNumbers instance like this:
27
43
 
28
44
  ```ruby
29
45
  client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access token
46
+ messages_api = WhatsappSdk::Api::Messages.new(client)
30
47
  ```
31
48
 
32
49
  Each API operation returns a `WhatsappSdk::Api::Response` that contains `data` and `error` and a couple of helpful functions such as `ok?` and `error?`. There are three types of response `WhatsappSdk::Api::MessageDataResponse`, `WhatsappSdk::Api::PhoneNumberDataResponse` and `WhatsappSdk::Api::PhoneNumbersDataResponse`. Each of them contains different attributes.
@@ -35,11 +52,13 @@ Each API operation returns a `WhatsappSdk::Api::Response` that contains `data` a
35
52
  First, create the client and then create an instance `WhatsappSdk::Api::Messages` that requires a client as a param like this:
36
53
 
37
54
  ```ruby
38
- client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access_token
39
- messages_api = WhatsappSdk::Api::Messages.new(client)
40
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client)
55
+ messages_api = WhatsappSdk::Api::Messages.new
56
+ phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
57
+ medias_api = WhatsappSdk::Api::Medias.new
41
58
  ```
42
59
 
60
+ Note: Remember to initialize the client first!
61
+
43
62
  ### Phone numbers API
44
63
  Get the list of phone numbers registered
45
64
  ```ruby
@@ -51,6 +70,28 @@ Get the a phone number by id
51
70
  phone_numbers_api.registered_numbers("123456") # accepts a phone_number_id
52
71
  ```
53
72
 
73
+ ### Media API
74
+
75
+ Upload a media
76
+ ```ruby
77
+ medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
78
+ ```
79
+
80
+ Get a media
81
+ ```ruby
82
+ media = medias_api.media(media_id: MEDIA_ID)
83
+ ```
84
+
85
+ Download media
86
+ ```ruby
87
+ medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png')
88
+ ```
89
+
90
+ Delete a media
91
+ ```ruby
92
+ medias_api.delete(media_id: MEDIA_ID)
93
+ ```
94
+
54
95
  ### Messages API
55
96
 
56
97
  **Send a text message**
@@ -59,11 +100,19 @@ phone_numbers_api.registered_numbers("123456") # accepts a phone_number_id
59
100
  messages_api.send_text(sender_id: 1234, recipient_number: "112345678", message: "hola")
60
101
  ```
61
102
 
103
+ **Read a message**
104
+ ```ruby
105
+ messages_api.read_message(sender_id: 1234, message_id: "wamid.HBgLMTM0M12345678910=")
106
+ ```
107
+
108
+ Note: To get the `message_id` you can set up [Webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) that will listen and fire an event when a message is received.
109
+
110
+
62
111
  **Send a location message**
63
112
 
64
113
  ```ruby
65
114
  messages_api.send_location(
66
- sender_id: 123123, recipient_number: "56789",
115
+ sender_id: 123123, recipient_number: "56789",
67
116
  longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
68
117
  )
69
118
  ```
@@ -71,12 +120,12 @@ messages_api.send_location(
71
120
  **Send an image message**
72
121
  It could use a link or an image_id.
73
122
  ```ruby
74
- # with a link
123
+ # with a link
75
124
  messages_api.send_image(
76
125
  sender_id: 123123, recipient_number: "56789", link: "image_link", caption: "Ignacio Chiazzo Profile"
77
126
  )
78
127
 
79
- # with an image id
128
+ # with an image id
80
129
  messages_api.send_image(
81
130
  sender_id: 123123, recipient_number: "56789", image_id: "1234", caption: "Ignacio Chiazzo Profile"
82
131
  )
@@ -85,22 +134,22 @@ messages_api.send_image(
85
134
  **Send an audio message**
86
135
  It could use a link or an audio_id.
87
136
  ```ruby
88
- # with a link
137
+ # with a link
89
138
  messages_api.send_audio(sender_id: 123123, recipient_number: "56789", link: "audio_link")
90
139
 
91
- # with an audio id
140
+ # with an audio id
92
141
  messages_api.send_audio(sender_id: 123123, recipient_number: "56789", audio_id: "1234")
93
142
  ```
94
143
 
95
144
  **Send a document message**
96
145
  It could use a link or a document_id.
97
146
  ```ruby
98
- # with a link
147
+ # with a link
99
148
  messages_api.send_document(
100
149
  sender_id: 123123, recipient_number: "56789", link: "document_link", caption: "Ignacio Chiazzo"
101
150
  )
102
151
 
103
- # with a document id
152
+ # with a document id
104
153
  messages_api.send_document(
105
154
  sender_id: 123123, recipient_number: "56789", document_id: "1234", caption: "Ignacio Chiazzo"
106
155
  )
@@ -109,7 +158,7 @@ messages_api.send_document(
109
158
  **Send a sticker message**
110
159
  It could use a link or a sticker_id.
111
160
  ```ruby
112
- # with a link
161
+ # with a link
113
162
  messages_api.send_sticker(sender_id: 123123, recipient_number: "56789", link: "link")
114
163
 
115
164
  # with a sticker_id
@@ -117,104 +166,86 @@ messages_api.send_sticker(sender_id: 123123, recipient_number: "56789", sticker_
117
166
  ```
118
167
 
119
168
  **Send contacts message**
120
- To send a contact, you need to create a Contact instance object that contain objects embedded like
121
- `addresses`, `birthday`, `emails`, `name`, `org`. See this [guide](/test/contact_helper.rb) to learn how to create contacts objects.
169
+ To send a contact, you need to create a Contact instance object that contain objects embedded like `addresses`, `birthday`, `emails`, `name`, `org`. See this [guide](/test/contact_helper.rb) to learn how to create contacts objects.
122
170
 
123
171
  ```ruby
124
172
  contacts = [create_contact(params)]
125
173
  messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts: contacts)
126
174
  ```
127
175
 
128
- ## Example
129
-
130
- <details><summary>Example in a single file. </summary>
131
-
132
- 1) Copy this code into a file and save it `example.rb`
133
- 2) Replace the `ACCESS_TOKEN` constant with a valid `access_token`.
134
- 3) Run the file with the command `ruby example.rb`
135
-
176
+ Alernative, you could pass a plain json like this:
136
177
  ```ruby
137
- # frozen_string_literal: true
138
-
139
- require 'bundler/inline'
140
-
141
- gemfile(true) do
142
- source 'https://rubygems.org'
143
-
144
- git_source(:github) { |repo| "https://github.com/#{repo}.git" }
145
-
146
- gem "whatsapp_sdk"
147
- gem "pry"
148
- gem "pry-nav"
149
- end
178
+ messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts_json: {...})
179
+ ```
150
180
 
151
- require 'whatsapp_sdk'
152
- require "pry"
153
- require "pry-nav"
181
+ **Send a template message**
182
+ WhatsApp message templates are specific message formats that businesses use to send out notifications or customer care messages to people that have opted in to notifications. Messages can include appointment reminders, shipping information, issue resolution or payment updates.
154
183
 
155
- ACCESS_TOKEN = "12345" # replace this with a valid access_token
156
- SENDER_ID = 107878721936019
157
- RECEIPIENT_NUMBER = "1234"
184
+ **Before sending a message template, you need to create one.** visit the [Official API Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates)
158
185
 
159
- client = WhatsappSdk::Api::Client.new(ACCESS_TOKEN) # replace this with a valid access_token
160
- messages_api = WhatsappSdk::Api::Messages.new(client)
161
- phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client)
186
+ <details> <summary>Component's example</summary>
162
187
 
163
- phone_numbers_api.registered_number("107878721936019")
164
- phone_numbers_api.registered_numbers("114503234599312")
165
-
166
- messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, message: "hola")
167
- messages_api.send_location(
168
- sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER,
169
- longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
188
+ ```ruby
189
+ currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
190
+ date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
191
+ image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
192
+
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)
197
+
198
+ header_component = WhatsappSdk::Resource::Component.new(
199
+ type: WhatsappSdk::Resource::Component::Type::HEADER,
200
+ parameters: [parameter_image]
170
201
  )
171
202
 
172
- # Send images
173
-
174
- ## with a link
175
- messages_api.send_image(
176
- sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "image_link", caption: "Ignacio Chiazzo Profile"
203
+ body_component = WhatsappSdk::Resource::Component.new(
204
+ type: WhatsappSdk::Resource::Component::Type::BODY,
205
+ parameters: [parameter_text, parameter_currency, parameter_date_time]
177
206
  )
178
207
 
179
- ## with an image id
180
- messages_api.send_image(
181
- sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, image_id: "1234", caption: "Ignacio Chiazzo Profile"
208
+ button_component1 = WhatsappSdk::Resource::Component.new(
209
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
210
+ index: 0,
211
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
212
+ parameters: [
213
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
214
+ ]
182
215
  )
183
216
 
184
- # Send audios
185
- ## with a link
186
- messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "audio_link")
187
-
188
- ## with an audio id
189
- messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, audio_id: "1234")
190
-
191
- # Send documents
192
- ## with a link
193
- messages_api.send_document(
194
- sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
217
+ button_component2 = WhatsappSdk::Resource::Component.new(
218
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
219
+ index: 1,
220
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
221
+ parameters: [
222
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
223
+ ]
195
224
  )
225
+ @messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [component_1])
226
+ ```
196
227
 
197
- ## with a document id
198
- messages_api.send_document(
199
- sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
200
- )
228
+ </details>
201
229
 
202
- # send stickers
203
- ## with a link
204
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "link")
230
+ Alernative, you could pass a plain json like this:
231
+ ```ruby
232
+ @messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [{...}])
233
+ ```
205
234
 
206
- ## with a sticker_id
207
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, sticker_id: "1234")
208
- binding.pry
235
+ ## Example
236
+
237
+ Visit [the example file](/example.rb) with examples to call the API in a single file.
209
238
 
210
- ```
211
- </details>
212
239
 
213
240
  ## Whatsapp Cloud API
214
241
 
215
242
  - See the [official documentation](https://developers.facebook.com/docs/whatsapp/cloud-api) for the Whatsapp Cloud API.
216
243
  - For pricing, refer to the [official documentation](https://developers.facebook.com/docs/whatsapp/pricing/). As of today, Whatsapp offers have 1000 conversations free per month.
217
244
 
245
+ ## Troubleshooting
246
+
247
+ - If the API response is success but the message is not delivered, make sure the device you're sending the message to is using a supported Whatsapp version. [Check documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/support/troubleshooting#message-not-delivered)
248
+
218
249
  ## Development
219
250
 
220
251
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
@@ -223,7 +254,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
223
254
 
224
255
  ## Contributing
225
256
 
226
- Bug reports and pull requests are welcome on GitHub at [https://github.com/ignacio-chiazzo/whatsapp_sdk](https://github.com/ignacio-chiazzo/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.
257
+ 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.
227
258
 
228
259
  ## License
229
260