whatsapp_sdk 0.0.2 → 0.3.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/.rubocop.yml +15 -2
- data/CHANGELOG.md +16 -1
- data/Gemfile +5 -2
- data/Gemfile.lock +19 -7
- data/README.md +115 -84
- data/example.rb +207 -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 +90 -23
- data/lib/whatsapp_sdk/api/phone_numbers.rb +8 -4
- data/lib/whatsapp_sdk/api/request.rb +9 -3
- data/lib/whatsapp_sdk/api/response.rb +7 -13
- 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 +3 -3
- data/lib/whatsapp_sdk/api/responses/success_response.rb +26 -0
- data/lib/whatsapp_sdk/configuration.rb +34 -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 +15 -1
- data/tmp/whatsapp.png +0 -0
- data/whatsapp_sdk.gemspec +12 -8
- metadata +41 -10
data/example.rb
ADDED
@@ -0,0 +1,207 @@
|
|
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"
|
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
|
+
################# Initialize Client #################
|
32
|
+
WhatsappSdk.configure do |config|
|
33
|
+
config.access_token = ACCESS_TOKEN
|
34
|
+
end
|
35
|
+
|
36
|
+
################# HELPERS ########################
|
37
|
+
def print_message_sent(message_response)
|
38
|
+
if message_response.ok?
|
39
|
+
puts "Message sent to: #{message_response.data.contacts.first.input}"
|
40
|
+
else
|
41
|
+
puts "Error: #{message_response.error&.to_s}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
##################################################
|
45
|
+
|
46
|
+
medias_api = WhatsappSdk::Api::Medias.new
|
47
|
+
messages_api = WhatsappSdk::Api::Messages.new
|
48
|
+
phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new
|
49
|
+
|
50
|
+
############################## Phone Numbers API ##############################
|
51
|
+
phone_numbers_api.registered_number(SENDER_ID)
|
52
|
+
phone_numbers_api.registered_numbers(BUSINESS_ID)
|
53
|
+
############################## Media API ##############################
|
54
|
+
|
55
|
+
# upload a media
|
56
|
+
uploaded_media = medias_api.upload(sender_id: SENDER_ID, file_path: "tmp/whatsapp.png", type: "image/png")
|
57
|
+
puts "Uploaded media id: #{uploaded_media.data&.id}"
|
58
|
+
|
59
|
+
# get a media
|
60
|
+
media = medias_api.media(media_id: uploaded_media.data&.id).data
|
61
|
+
puts "Media info: #{media.raw_data_response}"
|
62
|
+
|
63
|
+
# download media
|
64
|
+
download_image = medias_api.download(url: media&.url, file_path: 'tmp/downloaded_whatsapp.png')
|
65
|
+
puts "Downloaded: #{download_image.data.success?}"
|
66
|
+
|
67
|
+
# delete a media
|
68
|
+
deleted_media = medias_api.delete(media_id: media&.id)
|
69
|
+
puts "Deleted: #{deleted_media.data.success?}"
|
70
|
+
|
71
|
+
############################## Messages API ##############################
|
72
|
+
|
73
|
+
######### SEND A TEXT MESSAGE
|
74
|
+
message_sent = messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
75
|
+
message: "Hey there! it's Whatsapp Ruby SDK")
|
76
|
+
print_message_sent(message_sent)
|
77
|
+
|
78
|
+
location_sent = messages_api.send_location(
|
79
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
80
|
+
longitude: -75.6898604, latitude: 45.4192206, name: "Ignacio", address: "My house"
|
81
|
+
)
|
82
|
+
print_message_sent(location_sent)
|
83
|
+
|
84
|
+
######### READ A MESSAGE
|
85
|
+
# messages_api.read_message(sender_id: SENDER_ID, message_id: msg_id)
|
86
|
+
|
87
|
+
######### SEND AN IMAGE
|
88
|
+
# Send an image with a link
|
89
|
+
image_sent = messages_api.send_image(
|
90
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: media.url, caption: "Ignacio Chiazzo Profile"
|
91
|
+
)
|
92
|
+
print_message_sent(image_sent)
|
93
|
+
|
94
|
+
# Send an image with an id
|
95
|
+
messages_api.send_image(
|
96
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: media.id, caption: "Ignacio Chiazzo Profile"
|
97
|
+
)
|
98
|
+
|
99
|
+
######### SEND AUDIOS
|
100
|
+
## with a link
|
101
|
+
messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
|
102
|
+
|
103
|
+
## with an audio id
|
104
|
+
messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
|
105
|
+
|
106
|
+
######### SEND DOCUMENTS
|
107
|
+
## with a link
|
108
|
+
messages_api.send_document(
|
109
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
|
110
|
+
)
|
111
|
+
|
112
|
+
## with a document id
|
113
|
+
messages_api.send_document(
|
114
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
|
115
|
+
)
|
116
|
+
|
117
|
+
######### SEND STICKERS
|
118
|
+
## with a link
|
119
|
+
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
|
120
|
+
|
121
|
+
## with a sticker_id
|
122
|
+
messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
|
123
|
+
|
124
|
+
######### SEND A TEMPLATE
|
125
|
+
# Note: The template must have been created previously.
|
126
|
+
|
127
|
+
# Send a template with no component
|
128
|
+
response_with_object = messages_api.send_template(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
|
129
|
+
name: "hello_world", language: "en_US", components: [])
|
130
|
+
puts response_with_object
|
131
|
+
|
132
|
+
# Send a template with components.Remember to create the template first.
|
133
|
+
header_component = WhatsappSdk::Resource::Component.new(
|
134
|
+
type: WhatsappSdk::Resource::Component::Type::HEADER
|
135
|
+
)
|
136
|
+
|
137
|
+
image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
|
138
|
+
document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
|
139
|
+
video = WhatsappSdk::Resource::Media.new(type: "video", id: 123)
|
140
|
+
|
141
|
+
parameter_image = WhatsappSdk::Resource::ParameterObject.new(
|
142
|
+
type: "image",
|
143
|
+
image: image
|
144
|
+
)
|
145
|
+
|
146
|
+
parameter_document = WhatsappSdk::Resource::ParameterObject.new(
|
147
|
+
type: "document",
|
148
|
+
document: document
|
149
|
+
)
|
150
|
+
|
151
|
+
parameter_video = WhatsappSdk::Resource::ParameterObject.new(
|
152
|
+
type: "video",
|
153
|
+
video: video
|
154
|
+
)
|
155
|
+
|
156
|
+
parameter_text = WhatsappSdk::Resource::ParameterObject.new(
|
157
|
+
type: "text",
|
158
|
+
text: "I am a text"
|
159
|
+
)
|
160
|
+
|
161
|
+
header_component.add_parameter(parameter_text)
|
162
|
+
header_component.add_parameter(parameter_image)
|
163
|
+
header_component.add_parameter(parameter_video)
|
164
|
+
header_component.add_parameter(parameter_document)
|
165
|
+
header_component.to_json
|
166
|
+
|
167
|
+
body_component = WhatsappSdk::Resource::Component.new(
|
168
|
+
type: WhatsappSdk::Resource::Component::Type::BODY
|
169
|
+
)
|
170
|
+
body_component.add_parameter(parameter_text)
|
171
|
+
body_component.add_parameter(parameter_image)
|
172
|
+
body_component.add_parameter(parameter_video)
|
173
|
+
body_component.add_parameter(parameter_document)
|
174
|
+
body_component.to_json
|
175
|
+
|
176
|
+
# button_component_1 = WhatsappSdk::Resource::Component.new(
|
177
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
178
|
+
# index: 0,
|
179
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
180
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
181
|
+
# )
|
182
|
+
|
183
|
+
# button_component_2 = WhatsappSdk::Resource::Component.new(
|
184
|
+
# type: WhatsappSdk::Resource::Component::Type::BUTTON,
|
185
|
+
# index: 1,
|
186
|
+
# sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
|
187
|
+
# parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
|
188
|
+
# )
|
189
|
+
|
190
|
+
# Send a template with component_json
|
191
|
+
response_with_json = messages_api.send_template(
|
192
|
+
sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
|
193
|
+
components_json: [
|
194
|
+
{
|
195
|
+
"type" => "header",
|
196
|
+
"parameters" => [
|
197
|
+
{
|
198
|
+
"type" => "image",
|
199
|
+
"image" => {
|
200
|
+
"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"
|
201
|
+
}
|
202
|
+
}
|
203
|
+
]
|
204
|
+
}
|
205
|
+
]
|
206
|
+
)
|
207
|
+
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
|
@@ -25,9 +25,9 @@ module WhatsappSdk
|
|
25
25
|
params = {
|
26
26
|
messaging_product: "whatsapp",
|
27
27
|
to: recipient_number,
|
28
|
-
|
28
|
+
recipient_type: "individual",
|
29
29
|
type: "text",
|
30
|
-
|
30
|
+
text: { body: message }
|
31
31
|
}
|
32
32
|
|
33
33
|
response = send_request(
|
@@ -35,7 +35,10 @@ module WhatsappSdk
|
|
35
35
|
params: params
|
36
36
|
)
|
37
37
|
|
38
|
-
WhatsappSdk::Api::Response.new(
|
38
|
+
WhatsappSdk::Api::Response.new(
|
39
|
+
response: response,
|
40
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
41
|
+
)
|
39
42
|
end
|
40
43
|
|
41
44
|
# Send location.
|
@@ -51,13 +54,13 @@ module WhatsappSdk
|
|
51
54
|
params = {
|
52
55
|
messaging_product: "whatsapp",
|
53
56
|
to: recipient_number,
|
54
|
-
|
57
|
+
recipient_type: "individual",
|
55
58
|
type: "location",
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
location: {
|
60
|
+
longitude: longitude,
|
61
|
+
latitude: latitude,
|
62
|
+
name: name,
|
63
|
+
address: address
|
61
64
|
}
|
62
65
|
}
|
63
66
|
|
@@ -66,7 +69,10 @@ module WhatsappSdk
|
|
66
69
|
params: params
|
67
70
|
)
|
68
71
|
|
69
|
-
WhatsappSdk::Api::Response.new(
|
72
|
+
WhatsappSdk::Api::Response.new(
|
73
|
+
response: response,
|
74
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
75
|
+
)
|
70
76
|
end
|
71
77
|
|
72
78
|
# Send an image.
|
@@ -83,7 +89,7 @@ module WhatsappSdk
|
|
83
89
|
params = {
|
84
90
|
messaging_product: "whatsapp",
|
85
91
|
to: recipient_number,
|
86
|
-
|
92
|
+
recipient_type: "individual",
|
87
93
|
type: "image"
|
88
94
|
}
|
89
95
|
params[:image] = if link
|
@@ -97,7 +103,10 @@ module WhatsappSdk
|
|
97
103
|
params: params
|
98
104
|
)
|
99
105
|
|
100
|
-
WhatsappSdk::Api::Response.new(
|
106
|
+
WhatsappSdk::Api::Response.new(
|
107
|
+
response: response,
|
108
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
109
|
+
)
|
101
110
|
end
|
102
111
|
|
103
112
|
# Send an audio.
|
@@ -113,7 +122,7 @@ module WhatsappSdk
|
|
113
122
|
params = {
|
114
123
|
messaging_product: "whatsapp",
|
115
124
|
to: recipient_number,
|
116
|
-
|
125
|
+
recipient_type: "individual",
|
117
126
|
type: "audio"
|
118
127
|
}
|
119
128
|
params[:audio] = link ? { link: link } : { id: audio_id }
|
@@ -123,7 +132,10 @@ module WhatsappSdk
|
|
123
132
|
params: params
|
124
133
|
)
|
125
134
|
|
126
|
-
WhatsappSdk::Api::Response.new(
|
135
|
+
WhatsappSdk::Api::Response.new(
|
136
|
+
response: response,
|
137
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
138
|
+
)
|
127
139
|
end
|
128
140
|
|
129
141
|
# Send a video.
|
@@ -140,7 +152,7 @@ module WhatsappSdk
|
|
140
152
|
params = {
|
141
153
|
messaging_product: "whatsapp",
|
142
154
|
to: recipient_number,
|
143
|
-
|
155
|
+
recipient_type: "individual",
|
144
156
|
type: "video"
|
145
157
|
}
|
146
158
|
params[:video] = if link
|
@@ -154,7 +166,10 @@ module WhatsappSdk
|
|
154
166
|
params: params
|
155
167
|
)
|
156
168
|
|
157
|
-
WhatsappSdk::Api::Response.new(
|
169
|
+
WhatsappSdk::Api::Response.new(
|
170
|
+
response: response,
|
171
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
172
|
+
)
|
158
173
|
end
|
159
174
|
|
160
175
|
# Send a document.
|
@@ -171,7 +186,7 @@ module WhatsappSdk
|
|
171
186
|
params = {
|
172
187
|
messaging_product: "whatsapp",
|
173
188
|
to: recipient_number,
|
174
|
-
|
189
|
+
recipient_type: "individual",
|
175
190
|
type: "document"
|
176
191
|
}
|
177
192
|
params[:document] = if link
|
@@ -185,7 +200,10 @@ module WhatsappSdk
|
|
185
200
|
params: params
|
186
201
|
)
|
187
202
|
|
188
|
-
WhatsappSdk::Api::Response.new(
|
203
|
+
WhatsappSdk::Api::Response.new(
|
204
|
+
response: response,
|
205
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
206
|
+
)
|
189
207
|
end
|
190
208
|
|
191
209
|
# Send a document.
|
@@ -200,7 +218,7 @@ module WhatsappSdk
|
|
200
218
|
params = {
|
201
219
|
messaging_product: "whatsapp",
|
202
220
|
to: recipient_number,
|
203
|
-
|
221
|
+
recipient_type: "individual",
|
204
222
|
type: "sticker"
|
205
223
|
}
|
206
224
|
params[:sticker] = link ? { link: link } : { id: sticker_id }
|
@@ -210,7 +228,10 @@ module WhatsappSdk
|
|
210
228
|
params: params
|
211
229
|
)
|
212
230
|
|
213
|
-
WhatsappSdk::Api::Response.new(
|
231
|
+
WhatsappSdk::Api::Response.new(
|
232
|
+
response: response,
|
233
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
234
|
+
)
|
214
235
|
end
|
215
236
|
|
216
237
|
# Send contacts.
|
@@ -225,7 +246,7 @@ module WhatsappSdk
|
|
225
246
|
params = {
|
226
247
|
messaging_product: "whatsapp",
|
227
248
|
to: recipient_number,
|
228
|
-
|
249
|
+
recipient_type: "individual",
|
229
250
|
type: "contacts"
|
230
251
|
}
|
231
252
|
params[:contacts] = contacts ? contacts.map(&:to_h) : contacts_json
|
@@ -235,7 +256,10 @@ module WhatsappSdk
|
|
235
256
|
params: params
|
236
257
|
)
|
237
258
|
|
238
|
-
WhatsappSdk::Api::Response.new(
|
259
|
+
WhatsappSdk::Api::Response.new(
|
260
|
+
response: response,
|
261
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
262
|
+
)
|
239
263
|
end
|
240
264
|
|
241
265
|
def send_interactive_button
|
@@ -267,7 +291,50 @@ module WhatsappSdk
|
|
267
291
|
params: params
|
268
292
|
)
|
269
293
|
|
270
|
-
WhatsappSdk::Api::Response.new(
|
294
|
+
WhatsappSdk::Api::Response.new(
|
295
|
+
response: response,
|
296
|
+
data_class_type: WhatsappSdk::Api::Responses::ReadMessageDataResponse
|
297
|
+
)
|
298
|
+
end
|
299
|
+
|
300
|
+
# Send template
|
301
|
+
#
|
302
|
+
# @param sender_id [Integer] Sender' phone number.
|
303
|
+
# @param recipient_number [Integer] Recipient' Phone number.
|
304
|
+
# @param name [String] the template's name.
|
305
|
+
# @param language [String] template language.
|
306
|
+
# @param components [Component] Component.
|
307
|
+
# @param components_json [Json] The component as a Json. If you pass components_json, you can't pass components.
|
308
|
+
# @return [WhatsappSdk::Api::Response] Response object.
|
309
|
+
def send_template(sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil)
|
310
|
+
raise MissingArgumentError, "components or components_json is required" if !components && !components_json
|
311
|
+
|
312
|
+
params = {
|
313
|
+
messaging_product: "whatsapp",
|
314
|
+
recipient_type: "individual",
|
315
|
+
to: recipient_number,
|
316
|
+
type: "template",
|
317
|
+
template: {
|
318
|
+
name: name
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
params[:template][:language] = { code: language } if language
|
323
|
+
params[:template][:components] = if components.nil?
|
324
|
+
components_json
|
325
|
+
else
|
326
|
+
components.map(&:to_json)
|
327
|
+
end
|
328
|
+
|
329
|
+
response = send_request(
|
330
|
+
endpoint: endpoint(sender_id),
|
331
|
+
params: params
|
332
|
+
)
|
333
|
+
|
334
|
+
WhatsappSdk::Api::Response.new(
|
335
|
+
response: response,
|
336
|
+
data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
|
337
|
+
)
|
271
338
|
end
|
272
339
|
|
273
340
|
private
|
@@ -16,8 +16,10 @@ module WhatsappSdk
|
|
16
16
|
endpoint: "#{business_id}/phone_numbers"
|
17
17
|
)
|
18
18
|
|
19
|
-
WhatsappSdk::Api::Response.new(
|
20
|
-
|
19
|
+
WhatsappSdk::Api::Response.new(
|
20
|
+
response: response,
|
21
|
+
data_class_type: WhatsappSdk::Api::Responses::PhoneNumbersDataResponse
|
22
|
+
)
|
21
23
|
end
|
22
24
|
|
23
25
|
# Get the registered number id.
|
@@ -30,8 +32,10 @@ module WhatsappSdk
|
|
30
32
|
endpoint: phone_number_id.to_s
|
31
33
|
)
|
32
34
|
|
33
|
-
WhatsappSdk::Api::Response.new(
|
34
|
-
|
35
|
+
WhatsappSdk::Api::Response.new(
|
36
|
+
response: response,
|
37
|
+
data_class_type: WhatsappSdk::Api::Responses::PhoneNumberDataResponse
|
38
|
+
)
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
@@ -6,12 +6,18 @@ module WhatsappSdk
|
|
6
6
|
API_VERSION = "v13.0"
|
7
7
|
API_CLIENT = "https://graph.facebook.com/#{API_VERSION}/"
|
8
8
|
|
9
|
-
def initialize(client)
|
9
|
+
def initialize(client = WhatsappSdk.configuration.client)
|
10
10
|
@client = client
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
@client.
|
13
|
+
def download_file(url, path_to_file_name = nil)
|
14
|
+
@client.download_file(url, path_to_file_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def send_request(endpoint: nil, full_url: nil, http_method: "post", params: {})
|
18
|
+
@client.send_request(
|
19
|
+
http_method: http_method, full_url: full_url, endpoint: endpoint, params: params
|
20
|
+
)
|
15
21
|
end
|
16
22
|
end
|
17
23
|
end
|
@@ -3,31 +3,25 @@
|
|
3
3
|
require_relative "responses/message_data_response"
|
4
4
|
require_relative "responses/phone_number_data_response"
|
5
5
|
require_relative "responses/phone_numbers_data_response"
|
6
|
-
require_relative "responses/
|
6
|
+
require_relative "responses/read_message_data_response"
|
7
|
+
require_relative "responses/message_error_response"
|
7
8
|
|
8
9
|
module WhatsappSdk
|
9
10
|
module Api
|
10
11
|
class Response
|
11
12
|
attr_accessor :error, :data
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
phone_numbers_data_response: Responses::PhoneNumbersDataResponse,
|
17
|
-
read_message_data_response: ReadMessageDataResponse
|
18
|
-
}.freeze
|
19
|
-
|
20
|
-
def initialize(response:, class_type:)
|
21
|
-
@data = class_type.build_from_response(response: response)
|
22
|
-
@error = Responses::ErrorResponse.build_from_response(response: response)
|
14
|
+
def initialize(response:, data_class_type:, error_class_type: Responses::MessageErrorResponse)
|
15
|
+
@data = data_class_type.build_from_response(response: response)
|
16
|
+
@error = error_class_type.build_from_response(response: response)
|
23
17
|
end
|
24
18
|
|
25
|
-
# Whether or not the response is successful.
|
19
|
+
# @return [Boolean] Whether or not the response is successful.
|
26
20
|
def ok?
|
27
21
|
@error.nil?
|
28
22
|
end
|
29
23
|
|
30
|
-
# Whether or not the response has an error.
|
24
|
+
# @return [Boolean] Whether or not the response has an error.
|
31
25
|
def error?
|
32
26
|
!!@error
|
33
27
|
end
|