whatsapp_sdk 0.0.2 → 0.1.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: b4f5058be0fce569741f60508e508ec0b1cba2eccffbc5f510d7312cb023847b
4
+ data.tar.gz: 0af25be3aad6fdb5ffc60b014745294a0fa5cf496ce11ce68ebb886a774dfca4
5
5
  SHA512:
6
- metadata.gz: cc2eef92ce66f87374e1f2fb0f5cfb01f9bbf5814ae80d625403660fd1fc9d834680fb72a06941c8d66f0fb30e9dabe5561231c4547cc2fd1daf651300b93193
7
- data.tar.gz: fe548e9bfee0913a4eca4b1675342c565d35821e5d7b384908eebc0073b03616d7bc762ca8afefd1284dd6bd6ddffe88904b339e6448622b7675f09e68ed62e9
6
+ metadata.gz: 6f1ede78aafc6391e5de55ac4aa338a3286d5a9eb634a6afac22bf28cf39afa2f8e8356db2f266aa1bb7101ca122497b6036b843146fb77890c6f10d7460f7f8
7
+ data.tar.gz: 4b7524e665d2c13e4b8561e125b1cdbccbdfc1f95c49b7b465ce0e368918ce239a17afb58d83625b28a3288d803b25680b78167a967e69f3575d27071f7d9d2c
data/.rubocop.yml CHANGED
@@ -11,7 +11,10 @@ Layout/LineLength:
11
11
  Max: 120
12
12
 
13
13
  Metrics/MethodLength:
14
- Max: 30
14
+ Max: 40
15
+
16
+ Naming/VariableNumber:
17
+ Enabled: false
15
18
 
16
19
  Metrics/ClassLength:
17
20
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Unreleased
2
+ # v 0.1.0
3
+ - Added Message Template API.
4
+ - Added Currency and Datetime resources.
5
+ - Added Media resource.
6
+ - Added Component and ParameterObject resource.
7
+ - Fixed bug in recipient_number in Messages API.
2
8
 
3
9
  # v 0.0.2
4
10
  - Implement read message.
5
11
  - Implement Yard doc.
6
-
data/README.md CHANGED
@@ -117,98 +117,76 @@ messages_api.send_sticker(sender_id: 123123, recipient_number: "56789", sticker_
117
117
  ```
118
118
 
119
119
  **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.
120
+ 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
121
 
123
122
  ```ruby
124
123
  contacts = [create_contact(params)]
125
124
  messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts: contacts)
126
125
  ```
127
126
 
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
-
127
+ Alernative, you could pass a plain json like this:
136
128
  ```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
150
-
151
- require 'whatsapp_sdk'
152
- require "pry"
153
- require "pry-nav"
129
+ messages_api.send_contacts(sender_id: 123123, recipient_number: "56789", contacts_json: {...})
130
+ ```
154
131
 
155
- ACCESS_TOKEN = "12345" # replace this with a valid access_token
156
- SENDER_ID = 107878721936019
157
- RECEIPIENT_NUMBER = "1234"
132
+ **Send a template message**
133
+ 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.
158
134
 
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)
135
+ **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)
162
136
 
163
- phone_numbers_api.registered_number("107878721936019")
164
- phone_numbers_api.registered_numbers("114503234599312")
137
+ <details> <summary>Component's example</summary>
165
138
 
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"
139
+ ```ruby
140
+ currency = WhatsappSdk::Resource::Currency.new(code: "USD", amount: 1000, fallback_value: "1000")
141
+ date_time = WhatsappSdk::Resource::DateTime.new(fallback_value: "2020-01-01T00:00:00Z")
142
+ image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL")
143
+
144
+ parameter_image = WhatsappSdk::Resource::ParameterObject.new(type: "image", image: image)
145
+ parameter_text = WhatsappSdk::Resource::ParameterObject.new(type: "text", text: "TEXT_STRING")
146
+ parameter_currency = WhatsappSdk::Resource::ParameterObject.new(type: "currency", currency: currency)
147
+ parameter_date_time = WhatsappSdk::Resource::ParameterObject.new(type: "date_time", date_time: date_time)
148
+
149
+ header_component = WhatsappSdk::Resource::Component.new(
150
+ type: WhatsappSdk::Resource::Component::Type::HEADER,
151
+ parameters: [parameter_image]
170
152
  )
171
153
 
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"
154
+ body_component = WhatsappSdk::Resource::Component.new(
155
+ type: WhatsappSdk::Resource::Component::Type::BODY,
156
+ parameters: [parameter_text, parameter_currency, parameter_date_time]
177
157
  )
178
158
 
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"
159
+ button_component1 = WhatsappSdk::Resource::Component.new(
160
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
161
+ index: 0,
162
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
163
+ parameters: [
164
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
165
+ ]
182
166
  )
183
167
 
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"
168
+ button_component2 = WhatsappSdk::Resource::Component.new(
169
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
170
+ index: 1,
171
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
172
+ parameters: [
173
+ WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "PAYLOAD")
174
+ ]
195
175
  )
176
+ @messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [component_1])
177
+ ```
196
178
 
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
- )
179
+ </details>
201
180
 
202
- # send stickers
203
- ## with a link
204
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, link: "link")
181
+ Alernative, you could pass a plain json like this:
182
+ ```ruby
183
+ @messages_api.send_template(sender_id: 12_345, recipient_number: "12345678", name: "hello_world", language: "en_US", components_json: [{...}])
184
+ ```
205
185
 
206
- ## with a sticker_id
207
- messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECEIPIENT_NUMBER, sticker_id: "1234")
208
- binding.pry
186
+ ## Example
187
+
188
+ Visit [the example file](/example.rb) with examples to call the API in a single file.
209
189
 
210
- ```
211
- </details>
212
190
 
213
191
  ## Whatsapp Cloud API
214
192
 
data/example.rb ADDED
@@ -0,0 +1,160 @@
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
+ ACCESS_TOKEN = "foo" # replace this with a valid access_token
24
+ SENDER_ID = 123
25
+ RECIPIENT_NUMBER = "456"
26
+
27
+ client = WhatsappSdk::Api::Client.new(ACCESS_TOKEN) # replace this with a valid access_token
28
+ messages_api = WhatsappSdk::Api::Messages.new(client)
29
+ phone_numbers_api = WhatsappSdk::Api::PhoneNumbers.new(client)
30
+
31
+ phone_numbers_api.registered_number("123")
32
+ phone_numbers_api.registered_numbers("457")
33
+
34
+ messages_api.send_text(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, message: "hola")
35
+ messages_api.send_location(
36
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER,
37
+ longitude: 45.4215, latitude: 75.6972, name: "nacho", address: "141 cooper street"
38
+ )
39
+
40
+ # Send images
41
+
42
+ ## with a link
43
+ messages_api.send_image(
44
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "image_link", caption: "Ignacio Chiazzo Profile"
45
+ )
46
+
47
+ ## with an image id
48
+ messages_api.send_image(
49
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, image_id: "1234", caption: "Ignacio Chiazzo Profile"
50
+ )
51
+
52
+ # Send audios
53
+ ## with a link
54
+ messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "audio_link")
55
+
56
+ ## with an audio id
57
+ messages_api.send_audio(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, audio_id: "1234")
58
+
59
+ # Send documents
60
+ ## with a link
61
+ messages_api.send_document(
62
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "document_link", caption: "Ignacio Chiazzo"
63
+ )
64
+
65
+ ## with a document id
66
+ messages_api.send_document(
67
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, document_id: "1234", caption: "Ignacio Chiazzo"
68
+ )
69
+
70
+ # send stickers
71
+ ## with a link
72
+ messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, link: "link")
73
+
74
+ ## with a sticker_id
75
+ messages_api.send_sticker(sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, sticker_id: "1234")
76
+
77
+ # Send a template.
78
+ # Note: The template must have been created previously.
79
+
80
+ header_component = WhatsappSdk::Resource::Component.new(
81
+ type: WhatsappSdk::Resource::Component::Type::HEADER
82
+ )
83
+
84
+ image = WhatsappSdk::Resource::Media.new(type: "image", link: "http(s)://URL", caption: "caption")
85
+ document = WhatsappSdk::Resource::Media.new(type: "document", link: "http(s)://URL", filename: "txt.rb")
86
+ video = WhatsappSdk::Resource::Media.new(type: "video", id: 123)
87
+
88
+ parameter_image = WhatsappSdk::Resource::ParameterObject.new(
89
+ type: "image",
90
+ image: image
91
+ )
92
+
93
+ parameter_document = WhatsappSdk::Resource::ParameterObject.new(
94
+ type: "document",
95
+ document: document
96
+ )
97
+
98
+ parameter_video = WhatsappSdk::Resource::ParameterObject.new(
99
+ type: "video",
100
+ video: video
101
+ )
102
+
103
+ parameter_text = WhatsappSdk::Resource::ParameterObject.new(
104
+ type: "text",
105
+ text: "I am a text"
106
+ )
107
+
108
+ header_component.add_parameter(parameter_text)
109
+ header_component.add_parameter(parameter_image)
110
+ header_component.add_parameter(parameter_video)
111
+ header_component.add_parameter(parameter_document)
112
+ header_component.to_json
113
+
114
+ body_component = WhatsappSdk::Resource::Component.new(
115
+ type: WhatsappSdk::Resource::Component::Type::BODY
116
+ )
117
+ body_component.add_parameter(parameter_text)
118
+ body_component.add_parameter(parameter_image)
119
+ body_component.add_parameter(parameter_video)
120
+ body_component.add_parameter(parameter_document)
121
+ body_component.to_json
122
+
123
+ button_component_1 = WhatsappSdk::Resource::Component.new(
124
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
125
+ index: 0,
126
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
127
+ parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
128
+ )
129
+
130
+ button_component_2 = WhatsappSdk::Resource::Component.new(
131
+ type: WhatsappSdk::Resource::Component::Type::BUTTON,
132
+ index: 1,
133
+ sub_type: WhatsappSdk::Resource::Component::Subtype::QUICK_REPLY,
134
+ parameters: [WhatsappSdk::Resource::ButtonParameter.new(type: "payload", payload: "payload")]
135
+ )
136
+
137
+ # make sure that the template was created
138
+ response_with_json = messages_api.send_template(
139
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
140
+ components_json: [
141
+ {
142
+ "type": "header",
143
+ "parameters": [
144
+ {
145
+ "type": "image",
146
+ "image": {
147
+ "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"
148
+ }
149
+ }
150
+ ]
151
+ }
152
+ ]
153
+ )
154
+ puts response_with_json
155
+
156
+ response_with_object = messages_api.send_template(
157
+ sender_id: SENDER_ID, recipient_number: RECIPIENT_NUMBER, name: "hello_world", language: "en_US",
158
+ components: [header_component, body_component, button_component_1, button_component_2]
159
+ )
160
+ puts response_with_object
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WhatsappSdk
4
- VERSION = "0.0.2"
4
+ VERSION = "0.1.0"
5
5
  end
@@ -25,7 +25,7 @@ module WhatsappSdk
25
25
  params = {
26
26
  messaging_product: "whatsapp",
27
27
  to: recipient_number,
28
- recepient_type: "individual",
28
+ recipient_type: "individual",
29
29
  type: "text",
30
30
  "text": { body: message }
31
31
  }
@@ -51,7 +51,7 @@ module WhatsappSdk
51
51
  params = {
52
52
  messaging_product: "whatsapp",
53
53
  to: recipient_number,
54
- recepient_type: "individual",
54
+ recipient_type: "individual",
55
55
  type: "location",
56
56
  "location": {
57
57
  "longitude": longitude,
@@ -83,7 +83,7 @@ module WhatsappSdk
83
83
  params = {
84
84
  messaging_product: "whatsapp",
85
85
  to: recipient_number,
86
- recepient_type: "individual",
86
+ recipient_type: "individual",
87
87
  type: "image"
88
88
  }
89
89
  params[:image] = if link
@@ -113,7 +113,7 @@ module WhatsappSdk
113
113
  params = {
114
114
  messaging_product: "whatsapp",
115
115
  to: recipient_number,
116
- recepient_type: "individual",
116
+ recipient_type: "individual",
117
117
  type: "audio"
118
118
  }
119
119
  params[:audio] = link ? { link: link } : { id: audio_id }
@@ -140,7 +140,7 @@ module WhatsappSdk
140
140
  params = {
141
141
  messaging_product: "whatsapp",
142
142
  to: recipient_number,
143
- recepient_type: "individual",
143
+ recipient_type: "individual",
144
144
  type: "video"
145
145
  }
146
146
  params[:video] = if link
@@ -171,7 +171,7 @@ module WhatsappSdk
171
171
  params = {
172
172
  messaging_product: "whatsapp",
173
173
  to: recipient_number,
174
- recepient_type: "individual",
174
+ recipient_type: "individual",
175
175
  type: "document"
176
176
  }
177
177
  params[:document] = if link
@@ -200,7 +200,7 @@ module WhatsappSdk
200
200
  params = {
201
201
  messaging_product: "whatsapp",
202
202
  to: recipient_number,
203
- recepient_type: "individual",
203
+ recipient_type: "individual",
204
204
  type: "sticker"
205
205
  }
206
206
  params[:sticker] = link ? { link: link } : { id: sticker_id }
@@ -225,7 +225,7 @@ module WhatsappSdk
225
225
  params = {
226
226
  messaging_product: "whatsapp",
227
227
  to: recipient_number,
228
- recepient_type: "individual",
228
+ recipient_type: "individual",
229
229
  type: "contacts"
230
230
  }
231
231
  params[:contacts] = contacts ? contacts.map(&:to_h) : contacts_json
@@ -267,7 +267,50 @@ module WhatsappSdk
267
267
  params: params
268
268
  )
269
269
 
270
- WhatsappSdk::Api::Response.new(response: response, class_type: WhatsappSdk::Api::Responses::MessageDataResponse)
270
+ WhatsappSdk::Api::Response.new(
271
+ response: response,
272
+ class_type: WhatsappSdk::Api::Responses::ReadMessageDataResponse
273
+ )
274
+ end
275
+
276
+ # Send template
277
+ #
278
+ # @param sender_id [Integer] Sender' phone number.
279
+ # @param recipient_number [Integer] Recipient' Phone number.
280
+ # @param name [String] the template's name.
281
+ # @param language [String] template language.
282
+ # @param components [Component] Component.
283
+ # @param components_json [Json] The component as a Json. If you pass components_json, you can't pass components.
284
+ # @return [WhatsappSdk::Api::Response] Response object.
285
+ def send_template(sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil)
286
+ raise MissingArgumentError, "components or components_json is required" if !components && !components_json
287
+
288
+ params = {
289
+ messaging_product: "whatsapp",
290
+ recipient_type: "individual",
291
+ to: recipient_number,
292
+ type: "template",
293
+ template: {
294
+ name: name
295
+ }
296
+ }
297
+
298
+ params[:template][:language] = { code: language } if language
299
+ params[:template][:components] = if components.nil?
300
+ components_json
301
+ else
302
+ components.map(&:to_json)
303
+ end
304
+
305
+ response = send_request(
306
+ endpoint: endpoint(sender_id),
307
+ params: params
308
+ )
309
+
310
+ WhatsappSdk::Api::Response.new(
311
+ response: response,
312
+ class_type: WhatsappSdk::Api::Responses::MessageDataResponse
313
+ )
271
314
  end
272
315
 
273
316
  private
@@ -3,6 +3,7 @@
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/read_message_data_response"
6
7
  require_relative "responses/error_response"
7
8
 
8
9
  module WhatsappSdk
@@ -14,7 +15,7 @@ module WhatsappSdk
14
15
  message_data_response: Responses::MessageDataResponse,
15
16
  phone_number_data_response: Responses::PhoneNumberDataResponse,
16
17
  phone_numbers_data_response: Responses::PhoneNumbersDataResponse,
17
- read_message_data_response: ReadMessageDataResponse
18
+ read_message_data_response: Responses::ReadMessageDataResponse
18
19
  }.freeze
19
20
 
20
21
  def initialize(response:, class_type:)
@@ -8,7 +8,7 @@ require_relative "../../resource/contact_response"
8
8
  module WhatsappSdk
9
9
  module Api
10
10
  module Responses
11
- class MessageDataResponse < DataResponse
11
+ class ReadMessageDataResponse < DataResponse
12
12
  attr_reader :sucess
13
13
 
14
14
  def initialize(response:)
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class ButtonParameter
6
+ class InvalidType < StandardError
7
+ attr_accessor :message
8
+
9
+ def initialize(type)
10
+ @message = "invalid type #{type}. type should be text or payload"
11
+ super
12
+ end
13
+ end
14
+
15
+ # Returns the button parameter type.
16
+ #
17
+ # @returns type [String] Valid options are payload and text.
18
+ attr_accessor :type
19
+
20
+ module Type
21
+ TEXT = "text"
22
+ PAYLOAD = "payload"
23
+
24
+ VALID_TYPES = [PAYLOAD, TEXT].freeze
25
+ end
26
+
27
+ # Required for quick_reply buttons.
28
+ # Returns the button payload. Developer-defined payload that is returned when the button is clicked
29
+ # in addition to the display text on the button.
30
+ #
31
+ # @returns payload [String]
32
+ attr_accessor :payload
33
+
34
+ # Required for URL buttons.
35
+ # Developer-provided suffix that is appended to the predefined prefix URL in the template.
36
+ #
37
+ # @returns text [String]
38
+ attr_accessor :text
39
+
40
+ def initialize(type:, payload: nil, text: nil)
41
+ @type = type
42
+ @payload = payload
43
+ @text = text
44
+ validate
45
+ end
46
+
47
+ def to_json(*_args)
48
+ json = {
49
+ type: type
50
+ }
51
+ json[:payload] = payload if payload
52
+ json[:text] = text if text
53
+ json
54
+ end
55
+
56
+ private
57
+
58
+ def validate
59
+ return if Type::VALID_TYPES.include?(type)
60
+
61
+ raise InvalidType, type
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Component
6
+ class InvalidField < StandardError
7
+ attr_reader :field, :message
8
+
9
+ def initialize(field, message)
10
+ @field = field
11
+ @message = message
12
+ super(message)
13
+ end
14
+ end
15
+
16
+ module Type
17
+ HEADER = 'header'
18
+ BODY = 'body'
19
+ BUTTON = 'button'
20
+ end
21
+
22
+ module Subtype
23
+ QUICK_REPLY = "quick_reply"
24
+ URL = "url"
25
+ end
26
+
27
+ # Returns the Component type.
28
+ #
29
+ # @returns type [String]. Supported Options are header, body and button.
30
+ attr_accessor :type
31
+
32
+ # Returns the parameters of the component. For button type, it's required.
33
+ #
34
+ # @returns parameter [Array<ButtonParameter, ParameterObject>] .
35
+ attr_accessor :parameters
36
+
37
+ # Returns the Type of button to create. Required when type=button. Not used for the other types.
38
+ # Supported Options
39
+ # quick_reply: Refers to a previously created quick reply button
40
+ # that allows for the customer to return a predefined message.
41
+ # url: Refers to a previously created button that allows the customer to visit the URL generated by
42
+ # appending the text parameter to the predefined prefix URL in the template.
43
+ #
44
+ # @returns subtype [String]. Valid options are quick_reply and url.
45
+ attr_accessor :sub_type
46
+
47
+ # Required when type=button. Not used for the other types.
48
+ # Position index of the button. You can have up to 3 buttons using index values of 0 to 2.
49
+ #
50
+ # @returns index [Integer].
51
+ attr_accessor :index
52
+
53
+ def add_parameter(parameter)
54
+ @parameters << parameter
55
+ end
56
+
57
+ def initialize(type:, parameters: [], sub_type: nil, index: nil)
58
+ @parameters = parameters
59
+ @type = type
60
+ @sub_type = sub_type
61
+ @index = index.nil? && type == Type::BUTTON ? 0 : index
62
+ validate_fields
63
+ end
64
+
65
+ def to_json(*_args)
66
+ json = {
67
+ type: type,
68
+ parameters: parameters.map(&:to_json)
69
+ }
70
+ json[:sub_type] = sub_type if sub_type
71
+ json[:index] = index if index
72
+ json
73
+ end
74
+
75
+ private
76
+
77
+ def validate_fields
78
+ return if type == Type::BUTTON
79
+ raise InvalidField.new(:sub_type, 'sub_type is not required when type is not button') if sub_type
80
+
81
+ raise InvalidField.new(:index, 'index is not required when type is not button') if index
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Currency
6
+ # Returns default text if localization fails.
7
+ #
8
+ # @returns fallback_value [String].
9
+ attr_accessor :fallback_value
10
+
11
+ # Currency code as defined in ISO 4217.
12
+ #
13
+ # @returns code [String].
14
+ attr_accessor :code
15
+
16
+ # Amount multiplied by 1000.
17
+ #
18
+ # @returns code [Float].
19
+ attr_accessor :amount
20
+
21
+ def initialize(fallback_value:, code:, amount:)
22
+ @fallback_value = fallback_value
23
+ @code = code
24
+ @amount = amount
25
+ end
26
+
27
+ def to_json(*_args)
28
+ {
29
+ fallback_value: fallback_value,
30
+ code: code,
31
+ amount_1000: amount
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class DateTime
6
+ # Returns default text if localization fails.
7
+ #
8
+ # @returns fallback_value [String].
9
+ attr_accessor :fallback_value
10
+
11
+ def initialize(fallback_value:)
12
+ @fallback_value = fallback_value
13
+ end
14
+
15
+ def to_json(*_args)
16
+ {
17
+ fallback_value: fallback_value
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Media
6
+ class InvalidMedia < StandardError
7
+ attr_reader :field, :message
8
+
9
+ def initialize(field, message)
10
+ @field = field
11
+ @message = message
12
+ super(message)
13
+ end
14
+ end
15
+
16
+ # Returns media id.
17
+ #
18
+ # @returns id [String].
19
+ attr_accessor :id
20
+
21
+ module Type
22
+ AUDIO = 'audio'
23
+ DOCUMENT = 'document'
24
+ IMAGE = 'image'
25
+ VIDEO = 'video'
26
+ STICKER = 'sticker'
27
+
28
+ VALID_TYPES = [AUDIO, DOCUMENT, IMAGE, VIDEO, STICKER].freeze
29
+ end
30
+
31
+ # @returns type [String]. Valid options ar audio, document, image, video and sticker.
32
+ attr_accessor :type
33
+
34
+ # The protocol and URL of the media to be sent. Use only with HTTP/HTTPS URLs.
35
+ # Do not use this field when the message type is set to text.
36
+ #
37
+ # @returns link [String].
38
+ attr_accessor :link
39
+
40
+ # Describes the specified document or image media.
41
+ #
42
+ # @returns caption [String].
43
+ attr_accessor :caption
44
+
45
+ # Describes the filename for the specific document. Use only with document media.
46
+ #
47
+ # @returns filename [String].
48
+ attr_accessor :filename
49
+
50
+ def initialize(type:, id: nil, link: nil, caption: nil, filename: nil)
51
+ @type = type
52
+ @id = id
53
+ @link = link
54
+ @caption = caption
55
+ @filename = filename
56
+ validate_media
57
+ end
58
+
59
+ def to_json(*_args)
60
+ json = {}
61
+ json[:id] = id unless id.nil?
62
+ json[:link] = link unless link.nil?
63
+ json[:caption] = caption unless caption.nil?
64
+ json[:filename] = filename unless filename.nil?
65
+ json
66
+ end
67
+
68
+ private
69
+
70
+ def validate_media
71
+ unless Type::VALID_TYPES.include?(type)
72
+ raise InvalidMedia.new(:type, "invalid type. type should be audio, document, image, video or sticker")
73
+ end
74
+ if filename && (type != Type::DOCUMENT)
75
+ raise InvalidMedia.new(:filename, "filename can only be used with document")
76
+ end
77
+
78
+ if caption && !(type == Type::DOCUMENT || type == Type::IMAGE)
79
+ raise InvalidMedia.new(:caption, "caption can only be used with document or image")
80
+ end
81
+
82
+ true
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class ParameterObject
6
+ class InvalidType < StandardError
7
+ attr_accessor :message
8
+
9
+ def initialize(type)
10
+ @message = "invalid type #{type}. type should be text, currency, date_time, image, document or video"
11
+ super
12
+ end
13
+ end
14
+
15
+ class MissingValue < StandardError
16
+ attr_reader :field, :message
17
+
18
+ def initialize(field, message)
19
+ @field = field
20
+ @message = message
21
+ super(message)
22
+ end
23
+ end
24
+
25
+ # Returns the parameter type.
26
+ #
27
+ # @returns type [String] Valid options are text, currency, date_time, image, document, video.
28
+ attr_accessor :type
29
+
30
+ module Type
31
+ TEXT = "text"
32
+ CURRENCY = "currency"
33
+ DATE_TIME = "date_time"
34
+ IMAGE = "image"
35
+ DOCUMENT = "document"
36
+ VIDEO = "video"
37
+
38
+ VALID_TYPES = [TEXT, CURRENCY, DATE_TIME, IMAGE, DOCUMENT, VIDEO].freeze
39
+ end
40
+
41
+ # Returns Text string if the parameter object type is text.
42
+ # For the header component, the character limit is 60 characters.
43
+ # For the body component, the character limit is 1024 characters.
44
+ #
45
+ # @returns text [String]
46
+ attr_accessor :text
47
+
48
+ # Returns Currency if the parameter object type is currency.
49
+ #
50
+ # @returns currency [Currency]
51
+ attr_accessor :currency
52
+
53
+ # Returns date_time if the parameter object type is date_time.
54
+ #
55
+ # @returns date_time [DateTime]
56
+ attr_accessor :date_time
57
+
58
+ # Returns image if the parameter object type is image.
59
+ #
60
+ # @returns image [Media]
61
+ attr_accessor :image
62
+
63
+ # Returns document if the parameter object type is document.
64
+ #
65
+ # @returns document [Media]
66
+ attr_accessor :document
67
+
68
+ # Returns video if the parameter object type is video.
69
+ #
70
+ # @returns video [Media]
71
+ attr_accessor :video
72
+
73
+ def initialize(type:, text: nil, currency: nil, date_time: nil, image: nil, document: nil, video: nil)
74
+ @type = type
75
+ @text = text
76
+ @currency = currency
77
+ @date_time = date_time
78
+ @image = image
79
+ @document = document
80
+ @video = video
81
+ validate
82
+ end
83
+
84
+ def to_json(*_args)
85
+ json = { type: type }
86
+ json[type.to_sym] = case type
87
+ when "text"
88
+ text
89
+ when "currency"
90
+ currency.to_json
91
+ when "date_time"
92
+ date_time.to_json
93
+ when "image"
94
+ image.to_json
95
+ when "document"
96
+ document.to_json
97
+ when "video"
98
+ video.to_json
99
+ else
100
+ raise "Invalid type: #{type}"
101
+ end
102
+
103
+ json
104
+ end
105
+
106
+ private
107
+
108
+ def validate
109
+ validate_attributes
110
+ validate_type
111
+ end
112
+
113
+ def validate_type
114
+ return if Type::VALID_TYPES.include?(type)
115
+
116
+ raise InvalidType, type
117
+ end
118
+
119
+ def validate_attributes
120
+ [
121
+ [:text, text],
122
+ [:currency, currency],
123
+ [:date_time, date_time],
124
+ [:image, image],
125
+ [:document, document],
126
+ [:video, video]
127
+ ].each do |type_sym, value|
128
+ next unless type == type_sym
129
+ raise MissingValue.new(type, "#{type} is required when the type is #{type}") if value.nil?
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
data/lib/whatsapp_sdk.rb CHANGED
@@ -11,14 +11,21 @@ require_relative "whatsapp_sdk/api/responses/phone_number_data_response"
11
11
  require_relative "whatsapp_sdk/api/responses/phone_numbers_data_response"
12
12
  require_relative "whatsapp_sdk/api/responses/error_response"
13
13
  require_relative "whatsapp_sdk/api/responses/data_response"
14
+ require_relative "whatsapp_sdk/api/responses/read_message_data_response"
14
15
 
15
16
  # Resources
16
17
  require_relative "whatsapp_sdk/resource/address"
18
+ require_relative "whatsapp_sdk/resource/button_parameter"
19
+ require_relative "whatsapp_sdk/resource/component"
17
20
  require_relative "whatsapp_sdk/resource/contact_response"
18
21
  require_relative "whatsapp_sdk/resource/contact"
22
+ require_relative "whatsapp_sdk/resource/currency"
23
+ require_relative "whatsapp_sdk/resource/date_time"
19
24
  require_relative "whatsapp_sdk/resource/email"
25
+ require_relative "whatsapp_sdk/resource/media"
20
26
  require_relative "whatsapp_sdk/resource/message"
21
27
  require_relative "whatsapp_sdk/resource/name"
22
28
  require_relative "whatsapp_sdk/resource/org"
29
+ require_relative "whatsapp_sdk/resource/parameter_object"
23
30
  require_relative "whatsapp_sdk/resource/phone_number"
24
31
  require_relative "whatsapp_sdk/resource/url"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatsapp_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ignacio-chiazzo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-03 00:00:00.000000000 Z
11
+ date: 2022-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -102,6 +102,7 @@ files:
102
102
  - Rakefile
103
103
  - bin/console
104
104
  - bin/setup
105
+ - example.rb
105
106
  - lib/version.rb
106
107
  - lib/whatsapp_sdk.rb
107
108
  - lib/whatsapp_sdk/api/client.rb
@@ -117,12 +118,18 @@ files:
117
118
  - lib/whatsapp_sdk/api/responses/read_message_data_response.rb
118
119
  - lib/whatsapp_sdk/error.rb
119
120
  - lib/whatsapp_sdk/resource/address.rb
121
+ - lib/whatsapp_sdk/resource/button_parameter.rb
122
+ - lib/whatsapp_sdk/resource/component.rb
120
123
  - lib/whatsapp_sdk/resource/contact.rb
121
124
  - lib/whatsapp_sdk/resource/contact_response.rb
125
+ - lib/whatsapp_sdk/resource/currency.rb
126
+ - lib/whatsapp_sdk/resource/date_time.rb
122
127
  - lib/whatsapp_sdk/resource/email.rb
128
+ - lib/whatsapp_sdk/resource/media.rb
123
129
  - lib/whatsapp_sdk/resource/message.rb
124
130
  - lib/whatsapp_sdk/resource/name.rb
125
131
  - lib/whatsapp_sdk/resource/org.rb
132
+ - lib/whatsapp_sdk/resource/parameter_object.rb
126
133
  - lib/whatsapp_sdk/resource/phone_number.rb
127
134
  - lib/whatsapp_sdk/resource/url.rb
128
135
  - whatsapp_sdk.gemspec