whatsapp_sdk 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -2
- data/CHANGELOG.md +18 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +6 -1
- data/README.md +80 -70
- data/example.rb +200 -0
- data/lib/version.rb +1 -1
- data/lib/whatsapp_sdk/api/client.rb +28 -8
- data/lib/whatsapp_sdk/api/medias.rb +101 -0
- data/lib/whatsapp_sdk/api/messages.rb +169 -22
- data/lib/whatsapp_sdk/api/phone_numbers.rb +16 -4
- data/lib/whatsapp_sdk/api/request.rb +8 -2
- data/lib/whatsapp_sdk/api/response.rb +7 -10
- data/lib/whatsapp_sdk/api/responses/error_response.rb +10 -19
- data/lib/whatsapp_sdk/api/responses/media_data_response.rb +29 -0
- data/lib/whatsapp_sdk/api/responses/message_error_response.rb +30 -0
- data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +27 -0
- data/lib/whatsapp_sdk/api/responses/success_response.rb +26 -0
- data/lib/whatsapp_sdk/resource/button_parameter.rb +65 -0
- data/lib/whatsapp_sdk/resource/component.rb +85 -0
- data/lib/whatsapp_sdk/resource/currency.rb +36 -0
- data/lib/whatsapp_sdk/resource/date_time.rb +22 -0
- data/lib/whatsapp_sdk/resource/media.rb +86 -0
- data/lib/whatsapp_sdk/resource/parameter_object.rb +134 -0
- data/lib/whatsapp_sdk.rb +12 -1
- data/tmp/whatsapp.png +0 -0
- data/whatsapp_sdk.gemspec +3 -1
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f68cc009fe9fd27e79ac8e7f1a128204c66a565f055bce04bf4338f63ccb4f63
|
4
|
+
data.tar.gz: 49c4b5891624196c2557166cce8587e645a12e16716ccbc216c19f048cf2ab41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91204e0864c369777ff18f63c95f846acbcfa6248ed52fa0db6d05f43784be704e124edffcc73f4d3e0d565269b1784058ede119bdd862d067f75a49550d67df
|
7
|
+
data.tar.gz: 606a0b4ab9601388c2c737e47306cbc2432ef763efd82a869dd92be7d23d8fc2a6877573f1db5119b20ff8135523469a47ed02e2f75f857f33b238b6e7e23be5
|
data/.rubocop.yml
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
|
1
4
|
Style/StringLiterals:
|
2
5
|
Enabled: false
|
3
6
|
|
@@ -11,7 +14,10 @@ Layout/LineLength:
|
|
11
14
|
Max: 120
|
12
15
|
|
13
16
|
Metrics/MethodLength:
|
14
|
-
Max:
|
17
|
+
Max: 40
|
18
|
+
|
19
|
+
Naming/VariableNumber:
|
20
|
+
Enabled: false
|
15
21
|
|
16
22
|
Metrics/ClassLength:
|
17
23
|
Enabled: false
|
@@ -21,4 +27,4 @@ Metrics/AbcSize:
|
|
21
27
|
|
22
28
|
# Wait until https://github.com/rubocop/rubocop/issues/8761 is fixed
|
23
29
|
Gemspec/RequiredRubyVersion:
|
24
|
-
Enabled: false
|
30
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Unreleased
|
2
|
+
|
3
|
+
# v 0.2.0
|
4
|
+
- Added Media API
|
5
|
+
- Update Facebook API to v14
|
6
|
+
- Added error and sucess responses
|
7
|
+
- Added faraday-multiplart as part of the library
|
8
|
+
|
9
|
+
# v 0.1.0
|
10
|
+
- Added Message Template API.
|
11
|
+
- Added Currency and Datetime resources.
|
12
|
+
- Added Media resource.
|
13
|
+
- Added Component and ParameterObject resource.
|
14
|
+
- Fixed bug in recipient_number in Messages API.
|
15
|
+
|
16
|
+
# v 0.0.2
|
17
|
+
- Implement read message.
|
18
|
+
- Implement Yard doc.
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
whatsapp_sdk (0.0
|
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,10 +14,13 @@ 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
21
|
minitest (5.15.0)
|
19
22
|
mocha (1.14.0)
|
23
|
+
multipart-post (2.2.2)
|
20
24
|
oj (3.13.13)
|
21
25
|
parallel (1.22.1)
|
22
26
|
parser (3.1.2.0)
|
@@ -51,6 +55,7 @@ PLATFORMS
|
|
51
55
|
DEPENDENCIES
|
52
56
|
bundler (~> 2.3)
|
53
57
|
faraday
|
58
|
+
faraday-multipart
|
54
59
|
minitest (~> 5.0)
|
55
60
|
mocha
|
56
61
|
oj
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Send stickers, messages, audio, videos, locations or just ask for the phone numb
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
gem '
|
11
|
+
gem 'whatsapp_sdk'
|
12
12
|
```
|
13
13
|
|
14
14
|
And then execute:
|
@@ -17,7 +17,7 @@ And then execute:
|
|
17
17
|
|
18
18
|
Or install it yourself as:
|
19
19
|
|
20
|
-
$ gem install
|
20
|
+
$ gem install whatsapp_sdk
|
21
21
|
|
22
22
|
## Quick Start
|
23
23
|
|
@@ -38,6 +38,7 @@ First, create the client and then create an instance `WhatsappSdk::Api::Messages
|
|
38
38
|
client = WhatsappSdk::Api::Client.new("<ACCESS TOKEN>") # replace this with a valid access_token
|
39
39
|
messages_api = WhatsappSdk::Api::Messages.new(client)
|
40
40
|
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client)
|
41
|
+
medias_api = WhatsappSdk::Api::Medias.new(client)
|
41
42
|
```
|
42
43
|
|
43
44
|
### Phone numbers API
|
@@ -51,6 +52,28 @@ Get the a phone number by id
|
|
51
52
|
phone_numbers_api.registered_numbers("123456") # accepts a phone_number_id
|
52
53
|
```
|
53
54
|
|
55
|
+
### Media API
|
56
|
+
|
57
|
+
Upload a media
|
58
|
+
```ruby
|
59
|
+
medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
60
|
+
```
|
61
|
+
|
62
|
+
Get a media
|
63
|
+
```ruby
|
64
|
+
media = medias_api.media(media_id: MEDIA_ID)
|
65
|
+
```
|
66
|
+
|
67
|
+
Download media
|
68
|
+
```ruby
|
69
|
+
medias_api.download(url: MEDIA_URL, file_path: 'tmp/downloaded_whatsapp.png')
|
70
|
+
```
|
71
|
+
|
72
|
+
Delete a media
|
73
|
+
```ruby
|
74
|
+
medias_api.delete(media_id: MEDIA_ID)
|
75
|
+
```
|
76
|
+
|
54
77
|
### Messages API
|
55
78
|
|
56
79
|
**Send a text message**
|
@@ -117,98 +140,85 @@ messages_api.send_sticker(sender_id: 123123, recipient_number: "56789", sticker_
|
|
117
140
|
```
|
118
141
|
|
119
142
|
**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.
|
143
|
+
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
144
|
|
123
145
|
```ruby
|
124
146
|
contacts = [create_contact(params)]
|
125
147
|
messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts: contacts)
|
126
148
|
```
|
127
149
|
|
128
|
-
|
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
|
-
|
150
|
+
Alernative, you could pass a plain json like this:
|
136
151
|
```ruby
|
137
|
-
|
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" }
|
152
|
+
messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts_json: {...})
|
153
|
+
```
|
145
154
|
|
146
|
-
|
147
|
-
|
148
|
-
gem "pry-nav"
|
149
|
-
end
|
155
|
+
**Send a template message**
|
156
|
+
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.
|
150
157
|
|
151
|
-
|
152
|
-
require "pry"
|
153
|
-
require "pry-nav"
|
158
|
+
**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)
|
154
159
|
|
155
|
-
|
156
|
-
SENDER_ID = 107878721936019
|
157
|
-
RECEIPIENT_NUMBER = "1234"
|
160
|
+
<details> <summary>Component's example</summary>
|
158
161
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
162
|
+
```ruby
|
163
|
+
currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
|
164
|
+
date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
|
165
|
+
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
|
166
|
+
|
167
|
+
parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
|
168
|
+
parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING")
|
169
|
+
parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency)
|
170
|
+
parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time)
|
171
|
+
|
172
|
+
header_component = WhatsappSdk::Resource::Component.new(
|
173
|
+
type: WhatsappSdk::Resource::Component::Type::HEADER,
|
174
|
+
parameters: [parameter_image]
|
170
175
|
)
|
171
176
|
|
172
|
-
|
177
|
+
body_component = WhatsappSdk::Resource::Component.new(
|
178
|
+
type: WhatsappSdk::Resource::Component::Type::BODY,
|
179
|
+
parameters: [parameter_text, parameter_currency, parameter_date_time]
|
180
|
+
)
|
173
181
|
|
174
|
-
|
175
|
-
|
176
|
-
|
182
|
+
button_component1 = WhatsappSdk::Resource::Component.new(
|
183
|
+
type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
184
|
+
index: 0,
|
185
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
186
|
+
parameters: [
|
187
|
+
WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
|
188
|
+
]
|
177
189
|
)
|
178
190
|
|
179
|
-
|
180
|
-
|
181
|
-
|
191
|
+
button_component2 = WhatsappSdk::Resource::Component.new(
|
192
|
+
type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
193
|
+
index: 1,
|
194
|
+
sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
195
|
+
parameters: [
|
196
|
+
WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
|
197
|
+
]
|
182
198
|
)
|
199
|
+
@messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [component_1])
|
200
|
+
```
|
183
201
|
|
184
|
-
|
185
|
-
## with a link
|
186
|
-
messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "audio_link")
|
202
|
+
</details>
|
187
203
|
|
188
|
-
|
189
|
-
|
204
|
+
Alernative, you could pass a plain json like this:
|
205
|
+
```ruby
|
206
|
+
@messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [{...}])
|
207
|
+
```
|
190
208
|
|
191
|
-
|
192
|
-
## with a link
|
193
|
-
messages_api.send_document(
|
194
|
-
sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
|
195
|
-
)
|
209
|
+
## Example
|
196
210
|
|
197
|
-
|
198
|
-
messages_api.send_document(
|
199
|
-
sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
|
200
|
-
)
|
211
|
+
Visit [the example file](/example.rb) with examples to call the API in a single file.
|
201
212
|
|
202
|
-
# send stickers
|
203
|
-
## with a link
|
204
|
-
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "link")
|
205
213
|
|
206
|
-
##
|
207
|
-
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, sticker_id: "1234")
|
208
|
-
binding.pry
|
214
|
+
## Whatsapp Cloud API
|
209
215
|
|
210
|
-
|
211
|
-
|
216
|
+
- See the [official documentation](https://developers.facebook.com/docs/whatsapp/cloud-api) for the Whatsapp Cloud API.
|
217
|
+
- 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.
|
218
|
+
|
219
|
+
## Troubleshooting
|
220
|
+
|
221
|
+
- 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)
|
212
222
|
|
213
223
|
## Development
|
214
224
|
|
data/example.rb
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# 1) Copy this code into a file and save it `example.rb`
|
4
|
+
# 2) Replace the `ACCESS_TOKEN` constant with a valid `access_token`.
|
5
|
+
# 3) Run the file with the command `ruby example.rb`
|
6
|
+
|
7
|
+
require 'bundler/inline'
|
8
|
+
|
9
|
+
gemfile(true) do
|
10
|
+
source 'https://rubygems.org'
|
11
|
+
|
12
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
13
|
+
|
14
|
+
gem "whatsapp_sdk", github: "ignacio-chiazzo/whatsapp_sdk", branch: "main"
|
15
|
+
gem "pry"
|
16
|
+
gem "pry-nav"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'whatsapp_sdk'
|
20
|
+
require "pry"
|
21
|
+
require "pry-nav"
|
22
|
+
|
23
|
+
################# UPDATE CONSTANTS #################
|
24
|
+
|
25
|
+
ACCESS_TOKEN = "<TODO replace>"
|
26
|
+
SENDER_ID = "<TODO replace>"
|
27
|
+
RECIPIENT_NUMBER = "<TODO replace>"
|
28
|
+
BUSINESS_ID = "<TODO replace>"
|
29
|
+
IMAGE_LINK = "<TODO replace>"
|
30
|
+
|
31
|
+
################# HELPERS ########################
|
32
|
+
def print_message_sent(message_response)
|
33
|
+
if message_response.ok?
|
34
|
+
puts "Message sent to: #{message_response.data.contacts.first.input}"
|
35
|
+
else
|
36
|
+
puts "Error: #{message_response.error&.to_s}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
##################################################
|
40
|
+
|
41
|
+
client = WhatsappSdk::Api::Client.new(ACCESS_TOKEN)
|
42
|
+
medias_api = WhatsappSdk::Api::Medias.new(client)
|
43
|
+
messages_api = WhatsappSdk::Api::Messages.new(client)
|
44
|
+
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client)
|
45
|
+
|
46
|
+
############################## Phone Numbers API ##############################
|
47
|
+
phone_numbers_api.registered_number(SENDER_ID)
|
48
|
+
phone_numbers_api.registered_numbers(BUSINESS_ID)
|
49
|
+
############################## Media API ##############################
|
50
|
+
|
51
|
+
# upload a media
|
52
|
+
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
53
|
+
puts "Uploaded media id: #{uploaded_media.data&.id}"
|
54
|
+
|
55
|
+
# get a media
|
56
|
+
media = medias_api.media(media_id: uploaded_media.data&.id).data
|
57
|
+
puts "Media info: #{media.raw_data_response}"
|
58
|
+
|
59
|
+
# download media
|
60
|
+
download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
|
61
|
+
puts "Downloaded: #{download_image.data.success?}"
|
62
|
+
|
63
|
+
# delete a media
|
64
|
+
deleted_media = medias_api.delete(media_id: media&.id)
|
65
|
+
puts "Deleted: #{deleted_media.data.success?}"
|
66
|
+
|
67
|
+
############################## Messages API ##############################
|
68
|
+
|
69
|
+
######### SEND A TEXT MESSAGE
|
70
|
+
message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
71
|
+
message: "Hey there! it's Whatsapp Ruby SDK")
|
72
|
+
print_message_sent(message_sent)
|
73
|
+
|
74
|
+
location_sent = messages_api.send_location(
|
75
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
76
|
+
longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
|
77
|
+
)
|
78
|
+
print_message_sent(location_sent)
|
79
|
+
|
80
|
+
######### SEND AN IMAGE
|
81
|
+
# Send an image with a link
|
82
|
+
image_sent = messages_api.send_image(
|
83
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.url, caption: "Ignacio Chiazzo Profile"
|
84
|
+
)
|
85
|
+
print_message_sent(image_sent)
|
86
|
+
|
87
|
+
# Send an image with an id
|
88
|
+
messages_api.send_image(
|
89
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.id, caption: "Ignacio Chiazzo Profile"
|
90
|
+
)
|
91
|
+
|
92
|
+
######### SEND AUDIOS
|
93
|
+
## with a link
|
94
|
+
# messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
|
95
|
+
|
96
|
+
## with an audio id
|
97
|
+
# messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
|
98
|
+
|
99
|
+
######### SEND DOCUMENTS
|
100
|
+
## with a link
|
101
|
+
# messages_api.send_document(
|
102
|
+
# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
|
103
|
+
# )
|
104
|
+
|
105
|
+
## with a document id
|
106
|
+
# messages_api.send_document(
|
107
|
+
# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
|
108
|
+
# )
|
109
|
+
|
110
|
+
######### SEND STICKERS
|
111
|
+
## with a link
|
112
|
+
# messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
|
113
|
+
|
114
|
+
## with a sticker_id
|
115
|
+
# messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
|
116
|
+
|
117
|
+
######### SEND A TEMPLATE
|
118
|
+
# Note: The template must have been created previously.
|
119
|
+
|
120
|
+
# Send a template with no component
|
121
|
+
response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
122
|
+
name: "hello_world", language: "en_US", components: [])
|
123
|
+
puts response_with_object
|
124
|
+
|
125
|
+
# Send a template with components. Remember to create the template first.
|
126
|
+
# header_component = WhatsappSdk::Resource::Component.new(
|
127
|
+
# type: WhatsappSdk::Resource::Component::Type::HEADER
|
128
|
+
# )
|
129
|
+
|
130
|
+
# image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
|
131
|
+
# document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
|
132
|
+
# video = WhatsappSdk::Resource::Media.new(type: "video", id: 123)
|
133
|
+
|
134
|
+
# parameter_image = WhatsappSdk::Resource::ParameterObject.new(
|
135
|
+
# type: "image",
|
136
|
+
# image: image
|
137
|
+
# )
|
138
|
+
|
139
|
+
# parameter_document = WhatsappSdk::Resource::ParameterObject.new(
|
140
|
+
# type: "document",
|
141
|
+
# document: document
|
142
|
+
# )
|
143
|
+
|
144
|
+
# parameter_video = WhatsappSdk::Resource::ParameterObject.new(
|
145
|
+
# type: "video",
|
146
|
+
# video: video
|
147
|
+
# )
|
148
|
+
|
149
|
+
# parameter_text = WhatsappSdk::Resource::ParameterObject.new(
|
150
|
+
# type: "text",
|
151
|
+
# text: "I am a text"
|
152
|
+
# )
|
153
|
+
|
154
|
+
# header_component.add_parameter(parameter_text)
|
155
|
+
# header_component.add_parameter(parameter_image)
|
156
|
+
# header_component.add_parameter(parameter_video)
|
157
|
+
# header_component.add_parameter(parameter_document)
|
158
|
+
# header_component.to_json
|
159
|
+
|
160
|
+
# body_component = WhatsappSdk::Resource::Component.new(
|
161
|
+
# type: WhatsappSdk::Resource::Component::Type::BODY
|
162
|
+
# )
|
163
|
+
# body_component.add_parameter(parameter_text)
|
164
|
+
# body_component.add_parameter(parameter_image)
|
165
|
+
# body_component.add_parameter(parameter_video)
|
166
|
+
# body_component.add_parameter(parameter_document)
|
167
|
+
# body_component.to_json
|
168
|
+
|
169
|
+
# button_component_1 = WhatsappSdk::Resource::Component.new(
|
170
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
171
|
+
# index: 0,
|
172
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
173
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
174
|
+
# )
|
175
|
+
|
176
|
+
# button_component_2 = WhatsappSdk::Resource::Component.new(
|
177
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
178
|
+
# index: 1,
|
179
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
180
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
181
|
+
# )
|
182
|
+
|
183
|
+
# # Send a template with component_json
|
184
|
+
# response_with_json = messages_api.send_template(
|
185
|
+
# sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
|
186
|
+
# components_json: [
|
187
|
+
# {
|
188
|
+
# "type" => "header",
|
189
|
+
# "parameters" => [
|
190
|
+
# {
|
191
|
+
# "type" => "image",
|
192
|
+
# "image" => {
|
193
|
+
# "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"
|
194
|
+
# }
|
195
|
+
# }
|
196
|
+
# ]
|
197
|
+
# }
|
198
|
+
# ]
|
199
|
+
# )
|
200
|
+
# puts response_with_json
|
data/lib/version.rb
CHANGED
@@ -6,25 +6,45 @@ require "oj"
|
|
6
6
|
module WhatsappSdk
|
7
7
|
module Api
|
8
8
|
class Client
|
9
|
-
API_VERSION = "
|
9
|
+
API_VERSION = "v14.0"
|
10
10
|
API_CLIENT = "https://graph.facebook.com/#{API_VERSION}/"
|
11
11
|
|
12
12
|
def initialize(access_token)
|
13
13
|
@access_token = access_token
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
16
|
+
def send_request(endpoint: "", full_url: nil, http_method: "post", params: {})
|
17
|
+
url = full_url || API_CLIENT
|
18
|
+
|
19
|
+
response = faraday(url).public_send(http_method, endpoint, params)
|
20
|
+
Oj.load(response.body)
|
21
|
+
end
|
22
|
+
|
23
|
+
def download_file(url, path_to_file_name = nil)
|
24
|
+
uri = URI.parse(url)
|
25
|
+
request = Net::HTTP::Get.new(uri)
|
26
|
+
request["Authorization"] = "Bearer #{@access_token}"
|
27
|
+
req_options = { use_ssl: uri.scheme == "https" }
|
28
|
+
|
29
|
+
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
30
|
+
http.request(request)
|
31
|
+
end
|
32
|
+
|
33
|
+
File.write(path_to_file_name, response.body) if response.code == "200" && path_to_file_name
|
34
|
+
|
35
|
+
response
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def faraday(url)
|
41
|
+
::Faraday.new(url) do |client|
|
42
|
+
client.request :multipart
|
18
43
|
client.request :url_encoded
|
19
44
|
client.adapter ::Faraday.default_adapter
|
20
45
|
client.headers['Authorization'] = "Bearer #{@access_token}" unless @access_token.nil?
|
21
46
|
end
|
22
47
|
end
|
23
|
-
|
24
|
-
def send_request(endpoint:, http_method: "post", params: {})
|
25
|
-
response = client.public_send(http_method, endpoint, params)
|
26
|
-
Oj.load(response.body)
|
27
|
-
end
|
28
48
|
end
|
29
49
|
end
|
30
50
|
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "faraday"
|
4
|
+
require "faraday/multipart"
|
5
|
+
|
6
|
+
require_relative "request"
|
7
|
+
require_relative "response"
|
8
|
+
require_relative '../../../lib/whatsapp_sdk/api/responses/media_data_response'
|
9
|
+
require_relative '../../../lib/whatsapp_sdk/api/responses/success_response'
|
10
|
+
|
11
|
+
module WhatsappSdk
|
12
|
+
module Api
|
13
|
+
class Medias < Request
|
14
|
+
class FileNotFoundError < StandardError
|
15
|
+
attr_reader :file_path
|
16
|
+
|
17
|
+
def initialize(file_path)
|
18
|
+
@file_path = file_path
|
19
|
+
super("Couldn't find file_path: #{file_path}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Get Media by ID.
|
24
|
+
#
|
25
|
+
# @param media_id [Integer] Media Id.
|
26
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
27
|
+
def media(media_id:)
|
28
|
+
response = send_request(
|
29
|
+
http_method: "get",
|
30
|
+
endpoint: "/#{media_id}"
|
31
|
+
)
|
32
|
+
|
33
|
+
WhatsappSdk::Api::Response.new(
|
34
|
+
response: response,
|
35
|
+
data_class_type: WhatsappSdk::Api::Responses::MediaDataResponse
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Download Media by URL.
|
40
|
+
#
|
41
|
+
# @param media_id [Integer] Media Id.
|
42
|
+
# @param file_path [String] The file_path to download the media e.g. "tmp/downloaded_image.png".
|
43
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
44
|
+
def download(url:, file_path:)
|
45
|
+
response = download_file(url, file_path)
|
46
|
+
|
47
|
+
response = if response.code.to_i == 200
|
48
|
+
{ "success" => true }
|
49
|
+
else
|
50
|
+
{ "error" => true, "status" => response.code }
|
51
|
+
end
|
52
|
+
|
53
|
+
WhatsappSdk::Api::Response.new(
|
54
|
+
response: response,
|
55
|
+
data_class_type: WhatsappSdk::Api::Responses::SuccessResponse,
|
56
|
+
error_class_type: WhatsappSdk::Api::Responses::ErrorResponse
|
57
|
+
)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Upload a media.
|
61
|
+
# @param sender_id [Integer] Sender' phone number.
|
62
|
+
# @param file_path [String] Path to the file stored in your local directory. For example: "tmp/whatsapp.png".
|
63
|
+
# @param type [String] Media type e.g. text/plain, video/3gp, image/jpeg, image/png. For more information,
|
64
|
+
# see the official documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types.
|
65
|
+
#
|
66
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
67
|
+
def upload(sender_id:, file_path:, type:)
|
68
|
+
raise FileNotFoundError, file_path unless File.file?(file_path)
|
69
|
+
|
70
|
+
params = {
|
71
|
+
messaging_product: "whatsapp",
|
72
|
+
file: Faraday::FilePart.new(file_path, type),
|
73
|
+
type: type
|
74
|
+
}
|
75
|
+
|
76
|
+
response = send_request(http_method: "post", endpoint: "#{sender_id}/media", params: params)
|
77
|
+
|
78
|
+
WhatsappSdk::Api::Response.new(
|
79
|
+
response: response,
|
80
|
+
data_class_type: WhatsappSdk::Api::Responses::MediaDataResponse
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Delete a Media by ID.
|
85
|
+
#
|
86
|
+
# @param media_id [Integer] Media Id.
|
87
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
88
|
+
def delete(media_id:)
|
89
|
+
response = send_request(
|
90
|
+
http_method: "delete",
|
91
|
+
endpoint: "/#{media_id}"
|
92
|
+
)
|
93
|
+
|
94
|
+
WhatsappSdk::Api::Response.new(
|
95
|
+
response: response,
|
96
|
+
data_class_type: WhatsappSdk::Api::Responses::SuccessResponse
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|