warb 0.1.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ae3d5993ad731dab7a7aeb08130e76aff0872f4f4420680c8724613241043966
4
- data.tar.gz: c1fa725f65ad1f69dd37abee564a9864b8cb96b224bc2e098d6ffee183fa33a6
3
+ metadata.gz: 58ff4cfa4904a9a820c4b5906b947b6f5dcbd3e5a3afa76a9e88ed3f83488bb8
4
+ data.tar.gz: 1edcbcef65fd5f128cb0cf475f4b5c6ae8e4e285a4b57335b80818e0487f874a
5
5
  SHA512:
6
- metadata.gz: 30e29c7d31ea2d88bb844c1d50c727cbecf7a32d08d4eb5b6b0114a7d756eb16a103c8e4ca3e32c9532d0a834d0524e8d4d90ba536de8560bb2523dc3dd0c34a
7
- data.tar.gz: 765c7adf8cefc2a3eb53dfacfa847f1e29ffddbfd512273a6af8ae09a8f247259b4d6f4465a3c9ea46f83621781c94fa24176c1158589b9e0bda7e9e37c6e355
6
+ metadata.gz: 43cabbb2bcd395dbbc32eacf0f170e00c5fa913f9edbe4543b9b19f81278648ccae955ac64601182fc468ab02ae81cdb81ae1c07d4601f793dd1645cad28a698
7
+ data.tar.gz: f7ef031988f7c39be607047ebab9e56d627b848527fce0baa5043f1108a93e378e4e7b6d7df853240bcdba9ccb46b0c0c8ba135833fc31fbccaf01175dab9ba6
data/README.md CHANGED
@@ -9,16 +9,18 @@ A Ruby Gem focused on wrap all the functionalities and use cases of the WhatsApp
9
9
 
10
10
  ## Installation
11
11
 
12
- Add `warb` to your bundle
12
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
13
13
 
14
- ```ruby
15
- bundle add warb
14
+ Install the gem and add to the application's Gemfile by executing:
15
+
16
+ ```bash
17
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
18
  ```
17
19
 
18
- or add it manually to your Gemfile if you prefer.
20
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
21
 
20
- ```ruby
21
- gem 'warb'
22
+ ```bash
23
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
24
  ```
23
25
 
24
26
  ## Configuration
@@ -88,26 +90,9 @@ Warb.message.dispatch(recipient_number) do |builder|
88
90
  end
89
91
  ```
90
92
 
91
- ### What types of messages can I send?
92
-
93
- Warb implements the main types of WhatsApp messages, you can follow the message types [`here`](docs/messages/README.md#messages-types)
94
-
95
- examples:
96
-
97
- ```ruby
98
-
99
- warb = Warb.new(...)
100
-
101
- warb.message.dispatch(...)
102
- warb.audio.dispatch(...)
103
- warb.video.dispatch(...)
104
- warb.image.dispatch(...)
105
- ...
106
- ```
107
-
108
93
  ### Find all usage examples
109
94
 
110
- We suggest heading to the [`examples`](examples) directory, where you'll find documentation files with plenty of usage examples, organized by **Resources** (**Resources** are the types of messages you can send via WhatsApp using Warb).
95
+ We suggest heading to the `examples/` directory, where you'll find documentation files with plenty of usage examples, organized by **Resources** (**Resources** are the types of messages you can send via WhatsApp using Warb).
111
96
 
112
97
  You can also check the [`docs`](docs/README.md) for a more structured overview of the available resources and their usage.
113
98
 
data/docs/README.md CHANGED
@@ -10,7 +10,4 @@ Refer to [webhook](webhook.md) for more details on how to setup a webhook to get
10
10
  Refer to [messages](./messages/README.md) for more details on how to send messages using Warb.
11
11
 
12
12
  ## Components
13
- Refer to [components](./components/README.md) for more details on how Warb components work.
14
-
15
- ## Resources
16
- Refer to [resources](./resources/README.md) for more details on resources.
13
+ Refer to [components](./components/README.md) for more details on how Warb components work.
@@ -18,7 +18,4 @@ Here is a table of available components, and where they can be used:
18
18
  | `Warb::Components::ReplyButtonAction` | [Interactive Reply Button](../messages/interactive_reply_button.md) | [Reply Button Action](./reply_button_action) |
19
19
  | `Warb::Components::Row` | [Interactive List](../messages/interactive_list.md) | [Row](./row.md) |
20
20
  | `Warb::Components::ListAction` | [Interactive List](../messages/interactive_list.md) | [List Action](./list_action.md) |
21
- | `Warb::Components::Section` | [Interactive List](../messages/interactive_list.md) | [Section](./section.md) |
22
- | `Warb::Components::Button` | [Template](../messages/template.md) | [Button](./button.md) |
23
- | `Warb::Components::UrlButton` | [Template](../messages/template.md) | [UrlButton](./url_button.md) |
24
- | `Warb::Components::CopyCodeButton` | [Template](../messages/template.md) | [CopyCodeButton](./copy_code_button.md) |
21
+ | `Warb::Components::Section` | [Interactive List](../messages/interactive_list.md) | [Section](./section.md) |
@@ -71,10 +71,9 @@ Messages are sent using a helper dispatcher and its resource class. Here is a li
71
71
  | Video | `Warb::Resources::Video` | `video` | [Video Message](./video.md) |
72
72
  | Special Dispatchers |
73
73
  | Indicator | `Warb::Resources::Indicator` | `indicator` | [Indicator Message](./indicator.md) |
74
- | Template | `Warb::Resources::Template` | `template` | [Template Message](./template.md) |
75
74
 
76
75
  > Simple Dispatcher doesn't offer any additional functionality, they just send messages.
77
76
 
78
77
  > Media Dispatchers, aside from sending messages, also provide methods to upload and download media files.
79
78
 
80
- > Special Dispatchers are used for specific purposes, such as sending indicators, reactions or templates.
79
+ > Special Dispatchers are used for specific purposes, such as sending indicators or reactions.
@@ -6,7 +6,7 @@ module Warb
6
6
  end
7
7
 
8
8
  def dispatch(recipient_number, reply_to: nil, **args, &block)
9
- resource = block_given? ? @klass.new(**args).tap(&block) : @klass.new(**args)
9
+ resource = block_given? ? @klass.new.tap(&block) : @klass.new(**args)
10
10
 
11
11
  data = resource.call(recipient_number, reply_to:)
12
12
 
@@ -56,10 +56,6 @@ module Warb
56
56
  @contact ||= Dispatcher.new Resources::Contact, dispatcher
57
57
  end
58
58
 
59
- def template
60
- @template ||= TemplateDispatcher.new Resources::Template, dispatcher
61
- end
62
-
63
59
  def flow
64
60
  @flow ||= Dispatcher.new Resources::Flow, dispatcher
65
61
  end
@@ -5,17 +5,7 @@ module Warb
5
5
  class Location < Resource
6
6
  attr_accessor :latitude, :longitude, :name, :address
7
7
 
8
- def build_header
9
- common_location_params
10
- end
11
-
12
8
  def build_payload
13
- common_location_params
14
- end
15
-
16
- private
17
-
18
- def common_location_params
19
9
  {
20
10
  type: "location",
21
11
  location: {
@@ -19,14 +19,6 @@ module Warb
19
19
  raise NotImplementedError
20
20
  end
21
21
 
22
- def build_template_positional_parameter
23
- raise NotImplementedError
24
- end
25
-
26
- def build_template_named_parameter(paramater_name)
27
- raise NotImplementedError
28
- end
29
-
30
22
  def set_text_header
31
23
  raise NotImplementedError
32
24
  end
@@ -3,13 +3,10 @@
3
3
  module Warb
4
4
  module Resources
5
5
  class Text < Resource
6
- attr_accessor :content, :text, :message, :preview_url, :parameter_name
6
+ attr_accessor :content, :text, :message, :preview_url
7
7
 
8
8
  def build_header
9
- { type: "text", text: message_per_priority }.tap do |header|
10
- parameter_name ||= @params[:parameter_name]
11
- header[:parameter_name] = parameter_name unless parameter_name.nil?
12
- end
9
+ { type: "text", text: message_per_priority }
13
10
  end
14
11
 
15
12
  def build_payload
@@ -22,21 +19,6 @@ module Warb
22
19
  }
23
20
  end
24
21
 
25
- def build_template_named_parameter(parameter_name)
26
- {
27
- type: "text",
28
- text: message_per_priority,
29
- parameter_name: parameter_name
30
- }
31
- end
32
-
33
- def build_template_positional_parameter
34
- {
35
- type: "text",
36
- text: message_per_priority
37
- }
38
- end
39
-
40
22
  private
41
23
 
42
24
  def message_per_priority
data/lib/warb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Warb
4
- VERSION = "0.1.0"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/warb.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require "faraday"
4
4
  require "faraday/multipart"
5
5
  require_relative "warb/version"
6
- require_relative "warb/language"
7
6
  require_relative "warb/configuration"
8
7
  require_relative "warb/dispatcher_concern"
9
8
  require_relative "warb/client"
@@ -21,21 +20,11 @@ require_relative "warb/resources/interactive_reply_button"
21
20
  require_relative "warb/resources/interactive_list"
22
21
  require_relative "warb/resources/interactive_call_to_action_url"
23
22
  require_relative "warb/resources/contact"
24
- require_relative "warb/resources/template"
25
- require_relative "warb/resources/currency"
26
- require_relative "warb/resources/date_time"
27
23
  require_relative "warb/resources/flow"
28
24
  require_relative "warb/dispatcher"
29
25
  require_relative "warb/media_dispatcher"
30
- require_relative "warb/template_dispatcher"
31
26
  require_relative "warb/indicator_dispatcher"
32
27
  require_relative "warb/utils"
33
- require_relative "warb/components/component"
34
- require_relative "warb/components/button"
35
- require_relative "warb/components/quick_reply_button"
36
- require_relative "warb/components/url_button"
37
- require_relative "warb/components/copy_code_button"
38
- require_relative "warb/components/voice_call_button"
39
28
  require_relative "warb/components/action"
40
29
 
41
30
  module Warb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebase
@@ -52,8 +52,6 @@ files:
52
52
  - docs/README.md
53
53
  - docs/components/README.md
54
54
  - docs/components/address.md
55
- - docs/components/button.md
56
- - docs/components/copy_code_button.md
57
55
  - docs/components/cta_action.md
58
56
  - docs/components/email.md
59
57
  - docs/components/list_action.md
@@ -64,7 +62,6 @@ files:
64
62
  - docs/components/row.md
65
63
  - docs/components/section.md
66
64
  - docs/components/url.md
67
- - docs/components/url_button.md
68
65
  - docs/images/contact-with-wa_id.png
69
66
  - docs/images/contact-without-wa_id.png
70
67
  - docs/messages/README.md
@@ -81,13 +78,8 @@ files:
81
78
  - docs/messages/location_request.md
82
79
  - docs/messages/reaction.md
83
80
  - docs/messages/sticker.md
84
- - docs/messages/template.md
85
81
  - docs/messages/text.md
86
82
  - docs/messages/video.md
87
- - docs/resources/currency.md
88
- - docs/resources/date_time.md
89
- - docs/resources/index.md
90
- - docs/resources/text.md
91
83
  - docs/setup.md
92
84
  - docs/webhook.md
93
85
  - examples/audio.rb
@@ -106,28 +98,19 @@ files:
106
98
  - lib/warb/client.rb
107
99
  - lib/warb/components/action.rb
108
100
  - lib/warb/components/address.rb
109
- - lib/warb/components/button.rb
110
- - lib/warb/components/component.rb
111
- - lib/warb/components/copy_code_button.rb
112
101
  - lib/warb/components/email.rb
113
102
  - lib/warb/components/name.rb
114
103
  - lib/warb/components/org.rb
115
104
  - lib/warb/components/phone.rb
116
- - lib/warb/components/quick_reply_button.rb
117
105
  - lib/warb/components/url.rb
118
- - lib/warb/components/url_button.rb
119
- - lib/warb/components/voice_call_button.rb
120
106
  - lib/warb/configuration.rb
121
107
  - lib/warb/connection.rb
122
108
  - lib/warb/dispatcher.rb
123
109
  - lib/warb/dispatcher_concern.rb
124
110
  - lib/warb/indicator_dispatcher.rb
125
- - lib/warb/language.rb
126
111
  - lib/warb/media_dispatcher.rb
127
112
  - lib/warb/resources/audio.rb
128
113
  - lib/warb/resources/contact.rb
129
- - lib/warb/resources/currency.rb
130
- - lib/warb/resources/date_time.rb
131
114
  - lib/warb/resources/document.rb
132
115
  - lib/warb/resources/flow.rb
133
116
  - lib/warb/resources/image.rb
@@ -139,10 +122,8 @@ files:
139
122
  - lib/warb/resources/reaction.rb
140
123
  - lib/warb/resources/resource.rb
141
124
  - lib/warb/resources/sticker.rb
142
- - lib/warb/resources/template.rb
143
125
  - lib/warb/resources/text.rb
144
126
  - lib/warb/resources/video.rb
145
- - lib/warb/template_dispatcher.rb
146
127
  - lib/warb/utils.rb
147
128
  - lib/warb/version.rb
148
129
  - sig/warb.rbs
@@ -1,61 +0,0 @@
1
- # Button
2
-
3
- `Warb::Components::Button` is a component that represents a generic button used in template messages. It's part of the Components module, which contains reusable parts of complex structures.
4
-
5
- It represents a generic button, which can be set with the following attributes:
6
-
7
- ## Attributes
8
- | Attribute | Type | Required | Description |
9
- |--------------|-----------|----------|-----------------------------------------------|
10
- | `index` | `Integer` | Yes | An identifier or position for the button. |
11
- | `sub_type` | `String` | Yes | A more specific classification for the button |
12
-
13
- ## Common Button Types
14
- | Button Type | Template Instance Method | Params |
15
- |---------------|------------------------------|-------------------------|
16
- | `quick_reply` | `add_quick_reply_button` | `index` |
17
- | `voice_call` | `add_voice_call_button` | `index` |
18
- | `url` | `add_dynamic_url_button` | `index`, `text` |
19
- | `copy_code` | `add_copy_code_button` | `index`, `coupon_code` |
20
-
21
- Please, refer to our [templates messaging documentation](../messages/template.md) for more info. You can check the methods to insert a button in the "Adding Buttons" section.
22
-
23
- ## Examples
24
-
25
- ### Quick Reply button
26
- ```ruby
27
- quick_reply = Warb::Components::Button.new(index: 0, sub_type: "quick_reply")
28
- quick_reply.to_h
29
- => {
30
- type: "button",
31
- sub_type: "quick_reply",
32
- index: 0
33
- }
34
- ```
35
-
36
- ### Voice Call button
37
- ```ruby
38
- voice_call = Warb::Components::Button.new(index: 1, sub_type: "voice_call")
39
- voice_call.to_h
40
- => {
41
- type: "button",
42
- sub_type: "voice_call",
43
- index: 1
44
- }
45
- ```
46
-
47
- ## Usage in Templates
48
-
49
- Buttons are typically used within template messages. Here's how to add them:
50
-
51
- ```ruby
52
- template = Warb::Resources::Template.new(name: "my_template", language: "en_US")
53
-
54
- # Add a quick reply button
55
- template.add_quick_reply_button(index: 0)
56
-
57
- # Add a voice call button
58
- template.add_voice_call_button(index: 1)
59
-
60
- # The buttons will be included in the template's build_payload (as components)
61
- ```
@@ -1,57 +0,0 @@
1
- # CopyCodeButton
2
-
3
- `Warb::Components::CopyCodeButton` is a component used in template messages for copy code buttons.
4
-
5
- ## Attributes
6
- | Attribute | Type | Required | Description |
7
- |----------------|-----------|----------|-----------------------------------------------|
8
- | `index` | `Integer` | Yes | An identifier or position for the button. |
9
- | `sub_type` | `String` | Yes | Always "copy_code" for this button type |
10
- | `coupon_code` | `String` | No | The coupon code to be copied when button is pressed |
11
-
12
- ## Examples
13
-
14
- ### Basic copy code button
15
- ```ruby
16
- copy_button = Warb::Components::CopyCodeButton.new(index: 0, sub_type: "copy_code", coupon_code: "SAVE20")
17
- copy_button.to_h
18
- => {
19
- type: "button",
20
- sub_type: "copy_code",
21
- index: 0,
22
- parameters: [
23
- {
24
- type: "coupon_code",
25
- coupon_code: "SAVE20"
26
- }
27
- ]
28
- }
29
- ```
30
-
31
- ### Copy code button without coupon code
32
- ```ruby
33
- copy_button = Warb::Components::CopyCodeButton.new(index: 1, sub_type: "copy_code")
34
- copy_button.to_h
35
- => {
36
- type: "button",
37
- sub_type: "copy_code",
38
- index: 1
39
- }
40
- ```
41
-
42
- ## Usage in Templates
43
-
44
- Copy code buttons are typically added to templates using the `add_copy_code_button` method:
45
-
46
- ```ruby
47
- template = Warb::Resources::Template.new(name: "my_template", language: "en_US")
48
-
49
- # Add a copy code button
50
- template.add_copy_code_button(index: 0, coupon_code: "SAVE20")
51
-
52
- # Or using a block for more complex configuration
53
- template.add_copy_code_button do |button|
54
- button.index = 0
55
- button.coupon_code = "SAVE20"
56
- end
57
- ```
@@ -1,57 +0,0 @@
1
- # UrlButton
2
-
3
- `Warb::Components::UrlButton` is a component used in template messages, for dynamic url or auth code buttons.
4
-
5
- ## Attributes
6
- | Attribute | Type | Required | Description |
7
- |--------------|-----------|----------|-----------------------------------------------|
8
- | `index` | `Integer` | Yes | An identifier or position for the button. |
9
- | `sub_type` | `String` | Yes | Always "url" for this button type |
10
- | `text` | `String` | No | The URL suffix or text parameter for the button |
11
-
12
- ## Examples
13
-
14
- ### Basic URL button
15
- ```ruby
16
- url_button = Warb::Components::UrlButton.new(index: 0, sub_type: "url", text: "example.com")
17
- url_button.to_h
18
- => {
19
- type: "button",
20
- sub_type: "url",
21
- index: 0,
22
- parameters: [
23
- {
24
- type: "text",
25
- text: "example.com"
26
- }
27
- ]
28
- }
29
- ```
30
-
31
- ### URL button without text parameter
32
- ```ruby
33
- url_button = Warb::Components::UrlButton.new(index: 1, sub_type: "url")
34
- url_button.to_h
35
- => {
36
- type: "button",
37
- sub_type: "url",
38
- index: 1
39
- }
40
- ```
41
-
42
- ## Usage in Templates
43
-
44
- URL buttons are typically added to templates using the `add_dynamic_url_button` method:
45
-
46
- ```ruby
47
- template = Warb::Resources::Template.new(name: "my_template", language: "en_US")
48
-
49
- # Add a dynamic URL button
50
- template.add_dynamic_url_button(index: 0, text: "example.com")
51
-
52
- # Or using a block for more complex configuration
53
- template.add_dynamic_url_button do |button|
54
- button.index = 0
55
- button.text = "example.com"
56
- end
57
- ```
@@ -1,327 +0,0 @@
1
- # Template
2
-
3
- Template messages are used to send a message with a specific template.
4
-
5
- This is useful for sending messages that have a predefined structure, such as notifications or alerts.
6
-
7
- #### Quick Examples
8
-
9
- **Note**: For the examples below, take into account `Warb.setup` was called to configure the global client instance.
10
-
11
- List available templates:
12
- ```ruby
13
- Warb.template.list
14
- ```
15
-
16
- Sending template messages:
17
- ```ruby
18
- Warb.template.dispatch(recipient_number, **params)
19
- ```
20
-
21
- #### Listing Templates
22
-
23
- **Prerequisites**: In order to view templates, you need to have them available in your business account. For more details, refer to the [Meta documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates/template-library). Template creation via API will be covered later in this documentation (coming soon).
24
-
25
- You can retrieve all available message templates:
26
-
27
- ```ruby
28
- Warb.template.list
29
- ```
30
-
31
- ##### Optional Parameters
32
-
33
- You can optionally filter templates using various parameters:
34
-
35
- Limit the number of results:
36
- ```ruby
37
- Warb.template.list(limit: 10)
38
- ```
39
-
40
- Specify which template fields to return:
41
- ```ruby
42
- Warb.template.list(fields: ["name", "status", "category"])
43
- ```
44
-
45
- For a complete list of available fields, refer to the [Meta documentation](https://developers.facebook.com/docs/graph-api/reference/whats-app-business-hsm/#fields).
46
-
47
- ##### Example Response
48
-
49
- ```ruby
50
- {
51
- "data" => [
52
- {
53
- "name" => "hello_world",
54
- "status" => "APPROVED",
55
- "category" => "UTILITY",
56
- "language" => "en_US",
57
- "components" => [ ... ],
58
- "id" => "1282952826730729"
59
- },
60
- {
61
- "name" => "other_template_001",
62
- "status" => "APPROVED",
63
- "category" => "MARKETING",
64
- "language" => "pt_BR",
65
- "components" => [ ... ],
66
- "id" => "1948352829250167"
67
- }
68
- ],
69
- "paging" => {
70
- "cursors" => {
71
- "before" => "...",
72
- "after" => "..."
73
- }
74
- }
75
- }
76
- ```
77
-
78
- #### Sending Template Messages
79
-
80
- To send a template message, you need to provide the template name and the parameters for the template, if any.
81
-
82
- ```ruby
83
- Warb.template.dispatch(recipient_number, **params)
84
- ```
85
-
86
- Here, `**params` is a named hash parameters that will be passed to the template, as follow:
87
- | Attribute | Type | Required | Description |
88
- |-------------|-------------------|----------|---------------------------------------|
89
- | `name` | `String` | Yes | The name of the template to use |
90
- | `language` | `String` | Yes | The language to use for the template |
91
- | `resources` | `Array` or `Hash` | No | The resources to use for the template |
92
-
93
- `name` must be the name of a template that has been created in the WhatsApp Business Platform.
94
-
95
- `language` must be a valid language code, such as `en_US` for English (United States) or `fr_FR` for French (France). Also, the specified template must support the specified language.
96
-
97
- > You can check `Warb::Language` for a list of supported languages.
98
- > If yours isn't there, you can refer to this [list with all supported languages](https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/supported-languages) by WhatsApp.
99
- > Just use the value in the `code` column, and it will work.
100
-
101
- `resouces` can be an array or a hash of resources that will be used as parameters for the template. At this point, only `text`, `date_time` and `currency` are supported.
102
-
103
- If the template uses named parameters, then `resources` must be a hash, as follow:
104
- ```ruby
105
- customer_name = Warb::Resources::Text.new(content: "Name")
106
- purchase_date = Warb::Resources::Text.new("January 1, 2023")
107
-
108
- Warb.template.dispatch(recipient_number, resources: {
109
- customer_name: customer_name,
110
- purchase_date: purchase_date
111
- }, name: "testing", language: "pt_BR")
112
- ```
113
-
114
- If the template uses positional parameters, then `resources` must be an array, as follow:
115
- ```ruby
116
- customer_name = Warb::Resources::Text.new(content: "Name")
117
- purchase_date = Warb::Resources::DateTime.new("January 1, 2023")
118
-
119
- Warb.template.dispatch(recipient_number, resources: [
120
- customer_name,
121
- purchase_date
122
- ], name: "testing", language: "en_US")
123
- ```
124
-
125
- In any case, under the hood, the resources instances' method `build_template_named_parameter` or `build_template_positional_parameter` will be called to build the template parameters accordingly.
126
-
127
- Instead of instatiating the resources manually, you can use helper methods as follow:
128
- ```ruby
129
- Warb.template.dispatch(recipient_number) do |template|
130
- template.name = "testing"
131
- template.language = Warb::Language::PORTUGUESE_BR
132
- template.add_text_parameter(content: "Name")
133
- template.add_currency_parameter(code: "USD", amount: 11.42)
134
- end
135
- ```
136
-
137
- Here, `add_text_parameter` and `add_currency_parameter` will build the resources for you with the given named parameters and add them to the template.
138
-
139
- Here's a table with all the helper methods you can use to add resources/paramters to the template message:
140
- | Method Name | Resource Type | Description |
141
- |---------------------------------|-----------------------------|--------------------------------------------------|
142
- | `add_text_parameter` | `Warb::Resources::Text` | Adds a text parameter to the template |
143
- | `add_date_time_parameter` | `Warb::Resources::DateTime` | Adds a date time parameter to the template |
144
- | `add_currency_parameter` | `Warb::Resources::Currency` | Adds a currency parameter to the template |
145
-
146
- In the example above, notice we only used named parameters (no positional parameters) as arguments to the add parameter methods.
147
-
148
- So, the parameters will be built as positional parameters, and the `resources` will be an array.
149
-
150
- Also, the resources will be used to substitute the corresponding parameters in your template, in the same order they were added to the parameters/resources list.
151
-
152
- If you don't want to worry about the order of the parameters, you may use named parameters.
153
-
154
- For that, you have to pass the parameter name (as defined in your template) as the first argument to the helper method, as follow:
155
- ```ruby
156
- Warb.template.dispatch(recipient_number, name: "testing", language: "pt_BR") do |template|
157
- template.add_text_parameter("customer_name", content: "Name")
158
- template.add_date_time_parameter("purchase_date", date_time: "January 1, 2023")
159
- end
160
- ```
161
-
162
- This way, the order of the parameters names in the template and the order they were added with the `add_parameter` methods may be entirely different.
163
-
164
- One **IMPORTANT** thing to note is that, once defined, the type of the parameters won't be changed.
165
-
166
- So, in the following code:
167
- ```ruby
168
- Warb.template.dispatch(recipient_number, name: "testing", language: "pt_BR") do |template|
169
- template.add_date_time_parameter(date_time: "January 1, 2023")
170
- template.add_text_parameter("customer_name", content: "Name")
171
- end
172
- ```
173
- Your `customer_name` parameter won't behave the way you may expect.
174
-
175
- Since the first call to an `add_parameter` method was using the positional parameters syntax, then the internal resources attribute was set to an array, so that means any posterior calls to any `add_parameter` method will simply ignore the named parameter syntax and append the built resource to the list of positional parameters
176
-
177
- If the order of the calls was different:
178
- ```ruby
179
- Warb.template.dispatch(recipient_number, name: "testing", language: "pt_BR") do |template|
180
- template.add_text_parameter("customer_name", content: "Name")
181
- template.add_date_time_parameter(date_time: "January 1, 2023")
182
- template.add_currency_parameter(code: "USD", amount: 10)
183
- end
184
- ```
185
-
186
- 1. The first call to `add_parameter` method would set the internal resources/parameter to a hash, with a initial key named `customer_name`, pointing to a text resource
187
-
188
- 2. Then the `DateTime` resource (built in the second call to `add_parameter` method) would be set as value to the key `""` (empty string) in the named parameter hash.
189
-
190
- 3. Eventually, the `Currency` resource (in the third call do `add_parameter` method) would be set as value to the same key `""` (empty string) as the previous call, which would overwrite the previous resource.
191
-
192
- In this case, the call to the api would result in an error, either due to the mismatch of the count of parameters, or due to not having all named parameters defined.
193
-
194
- So, make sure to always use the same parameter syntax accordingly to your template.
195
-
196
- The add parameter methods also support block building as well:
197
- ```ruby
198
- Warb.template.dispatch(recipient_number) do |template|
199
- template.name = "testing"
200
- template.language = Warb::Language::ENGLISH_US
201
-
202
- template.add_text_parameter(content: "Name")
203
-
204
- template.add_currency_parameter do |currency|
205
- currency.code = "USD"
206
- currency.amount = 11.42
207
- currency.fallback = "$ 11.42"
208
- end
209
-
210
- template.add_date_time_parameter do |purchase_date|
211
- purchase_date.date_time = "Jan 1st, 2024"
212
- end
213
- end
214
- ```
215
-
216
- If your template has any media header, you can set it as follow:
217
- | Header Type | Template Instance Method | Params |
218
- |----------------|-----------------------------|------------------------------------------------|
219
- | `image` | `set_image_header` | `media_id` or `link` |
220
- | `video` | `set_video_header` | `media_id` or `link` |
221
- | `document` | `set_document_header` | `media_id` or `link`, `filename` |
222
- | `location` | `set_location_header` | `latitude`, `longitude`, `name`, and `address` |
223
- | `text` | `set_text_header` | `content`, `parameter_name` |
224
-
225
- Every time a call is made to any `set_header` method, a new header will be set, overwriting the previous one.
226
-
227
- If you just want to change one attribute or another, `set_header` methods return the related resource, so it is possible to set the values if you keep a hold of such instance
228
- ```ruby
229
- Warb.template.dispatch(recipient_number) do |template|
230
- header = template.set_image_header(media_id: "wrong_media_id")
231
-
232
- header.media_id = "correct_media_id"
233
- end
234
- ```
235
-
236
- Also, either only one of `media_id` or `link` must be provided.
237
-
238
- The `media_id` must be the id of media uploaded previously to WhatsApp Business Platform, while the `link` must be a public acessible URL.
239
-
240
- Check the `upload` section for each media related message, like [`image`](./image.md) or [`document`](./document.md), for more info on how to upload and the supported formats.
241
-
242
- For the `document` header, `filename` is important because its extension will determine the preview capability on the recipient's device.
243
-
244
- For the `location` header, at least `latitude` and `longitude` must be provided.
245
-
246
- `set_header` methods will simply instatiate the corresponding resource class with the given parameters, and then, set it as the header attribute.
247
-
248
- For `text` header, note that, due to how the WhatsApp Business Platform works, you can't set the entire content for it (the same that happens with the body of the message).
249
-
250
- In this case, `set_text_header`, will simply use whatever was given to it as parameter to build the final header in the WhatsApp Business Platform.
251
-
252
- So, for example, if your tamplate header was created with `Hello, {{1}}!`, then the text passed to `set_text_header` will simply be substituted in that `{{1}}`.
253
-
254
- If your template was defined using named parameters instead (something like `Hello, {{customer_name}}!`), then you must pass the name of the paramter to `set_text_header` as follow:
255
- ```ruby
256
- Warb.template.dispatch(recipient_number) do |template|
257
- template.set_text_header(content: "John", parameter_name: "customer_name")
258
- end
259
- ```
260
-
261
- When the template instance's `build_payload` method is called (which happens when the message is about to be dispatched), the header param will be created using the `header`'s `build_header` method.
262
-
263
- #### Adding Buttons
264
-
265
- If your template supports buttons, you can add them using the following methods:
266
-
267
- | Button Type | Template Instance Method | Params |
268
- |--------------------|-------------------------------|-------------------------------------------|
269
- | `quick_reply` | `add_quick_reply_button` | `index` |
270
- | `url` | `add_dynamic_url_button` | `index`, `text` |
271
- | `url` | `add_auth_code_button` | `index`, `text` |
272
- | `copy_code` | `add_copy_code_button` | `index`, `coupon_code` |
273
- | `voice_call` | `add_voice_call_button` | `index` |
274
- | `doesn't apply` | `add_button` | `instance`, `&block` |
275
-
276
- You can either use the keyword parameters or set the attributes using a block:
277
-
278
- ```ruby
279
- Warb.template.dispatch(recipient_number) do |template|
280
- template.name = "order_confirmation"
281
- template.language = Warb::Language::ENGLISH_US
282
-
283
- # Add a quick reply button
284
- template.add_quick_reply_button
285
-
286
- # Add a dynamic URL button
287
- template.add_dynamic_url_button do |button|
288
- button.text = "dynamic-url-suffix"
289
- end
290
-
291
- # Add a copy auth code button
292
- template.add_auth_code_button do |button|
293
- button.text = "4UTHC0D3"
294
- end
295
-
296
- # Add a copy code button
297
- template.add_copy_code_button(index: 2) do |button|
298
- button.coupon_code = "SAVE20"
299
- end
300
-
301
- # Add a voice call button
302
- template.add_voice_call_button
303
- end
304
- ```
305
-
306
- or if you'd rather to, you can add buttons using it's component and the `add_button` method:
307
-
308
- ```ruby
309
- quick_reply_btn = Warb::Components::QuickReplyButton.new
310
-
311
- Warb.template.dispatch(recipient_number) do |template|
312
- template.name = "order_confirmation"
313
- template.language = Warb::Language::ENGLISH_US
314
-
315
- # Add a quick reply button
316
- template.add_button(quick_reply_btn)
317
-
318
- # Add a quick reply button, passing a block.
319
- template.add_button(quick_reply_btn) { |button| button.index = 1 }
320
- end
321
- ```
322
-
323
- **Note**: The `index` parameter determines the order of the buttons in the template. Make sure the
324
- indices match the button positions defined in your template. The `index` is automaticaly set if you
325
- don't do it manually, but it is done based on the number of buttons added with the methods above,
326
- so if your template has a button that doesn't need configuration like the static url button you'll
327
- have provide the position of the other buttons.
@@ -1,22 +0,0 @@
1
- # Currency
2
-
3
- `Warb::Resources::Currency` is a resource, but different from most other resources, it can't be sent. Instead, it's used in a template message as one of its parameters.
4
-
5
- Please, refer to our [templates messaging documentation](../messages/template.md) for more info.
6
-
7
- It represents an amount in a specific currency, which can be set with the following attributes:
8
- | Attribute | Type | Required | Description |
9
- |------------|--------------------|----------|-------------------------------------------------------------|
10
- | `amount` | `Integer`, `Float` | Yes | The amount to be represented in the given currency |
11
- | `code` | `String` | Yes | The code of the currency to be used |
12
- | `fallback` | `String` | No | A fallback value to be used in case the code can't be found |
13
-
14
- Since it is used in template messages, it implements both `build_template_named_parameter` and `build_template_positional_parameter`, which prepares the resource for using as parameter.
15
-
16
- If no `fallback` value is given, then a default one, based on the given `amount` and `currency` is used.
17
-
18
- In the WhatsApp Business Platform, the `code` will be used to represent the given `amount` according to the specific currency.
19
-
20
- But if the given code doesn't exist, or is not supported, then the given `fallback` value will be used instead.
21
-
22
- For the `code`, you must use one currency code like `USD` or `BRL`. `Warb::Resources::Currency` offers some of the most used currencies. You can also [check here](https://www.iso.org/iso-4217-currency-codes.html) for more detailed info and other available options.
@@ -1,11 +0,0 @@
1
- # Date Time
2
-
3
- `Warb::Resources::DateTime` is a resource, but different from most other resources, it can't be sent. Instead, it's used in a template message as one of its parameters.
4
-
5
- Please, refer to our [templates messaging documentation](../messages/template.md) for more info.
6
-
7
- It simply represents a date time object, with the unique param/attribute being `date_time`.
8
-
9
- Although its name is `date_time`, its content can be any string, like `"January 1st"`, `"2020-01-01"` or `"Wednesday, Jan, 1st, 2020"`.
10
-
11
- Since it is used in template messages, it implements both `build_template_named_parameter` and `build_template_positional_parameter`, which prepares the resource for using as parameter.
@@ -1,14 +0,0 @@
1
- # Resources
2
-
3
- Most of the resources can be sent as message. Check the [messages](../messages/index.md) for detailed info.
4
-
5
- But there are some resources which act more like a component, not being used alone by themself.
6
-
7
- There are also, resources which can be used alone, and as components for other messages types, like the `Text` resource.
8
-
9
- Such resources, and where they are used, are listed bellow:
10
- | Resource | Documentation | Used at |
11
- |-----------------------------|------------------------------|--------------------------------------------------------------------------------------|
12
- | `Warb::Resources::Currency` | [Currency](./currency.md) | [Template Messaging](../messages/template.md) |
13
- | `Warb::Resources::DateTime` | [Date Time](./date_time.md) | [Template Messaging](../messages/template.md) |
14
- | `Warb::Resources::Text` | [Text](./text.md) | [Text Messaging](../messages/text.md), [Template Messaging](../messages/template.md) |
@@ -1,9 +0,0 @@
1
- # Text
2
-
3
- `Warb::Resources::Text` is a resource, which represents a text.
4
-
5
- It can be used as a [simple message](../messages/text.md), as a text header for some interactive messages, like the [reply button message](../messages/interactive_reply_button.md) and as [parameters for templates](../messages/template.md) variables.
6
-
7
- In the [text messaging documentation](../messages/text.md) there is a table with the corresponding attributes.
8
-
9
- Aside from such attributes, `Text` resource also provides the `build_template_named_parameter` and `build_template_positional_parameter` methods, which are using under the hood for the template messaging feature.
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class Button < Component
6
- attr_accessor :index, :sub_type
7
-
8
- def initialize(**params)
9
- params[:sub_type] = button_type unless params[:sub_type]
10
-
11
- super(**params)
12
- end
13
-
14
- def to_h
15
- {
16
- type: "button",
17
- sub_type: sub_type || @params[:sub_type],
18
- index: index || @params[:index]
19
- }
20
- end
21
-
22
- private
23
-
24
- def button_type
25
- raise NotImplementedError
26
- end
27
- end
28
- end
29
- end
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class Component
6
- def initialize(**params)
7
- @params = params
8
- end
9
-
10
- def to_h
11
- raise NotImplementedError
12
- end
13
-
14
- protected
15
-
16
- attr_reader :params
17
- end
18
- end
19
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class CopyCodeButton < Button
6
- BUTTON_TYPE = "copy_code"
7
-
8
- attr_accessor :coupon_code
9
-
10
- def to_h
11
- button_payload = super
12
-
13
- if coupon_code || @params[:coupon_code]
14
- button_payload[:parameters] = Array.new(1, {
15
- type: "coupon_code",
16
- coupon_code: coupon_code || @params[:coupon_code]
17
- })
18
- end
19
-
20
- button_payload
21
- end
22
-
23
- private
24
-
25
- def button_type
26
- BUTTON_TYPE
27
- end
28
- end
29
- end
30
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class QuickReplyButton < Button
6
- BUTTON_TYPE = "quick_reply"
7
-
8
- private
9
-
10
- def button_type
11
- BUTTON_TYPE
12
- end
13
- end
14
- end
15
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class UrlButton < Button
6
- BUTTON_TYPE = "url"
7
-
8
- attr_accessor :text
9
-
10
- def to_h
11
- button_payload = super
12
-
13
- if text || @params[:text]
14
- button_payload[:parameters] = Array.new(1, {
15
- type: "text",
16
- text: text || @params[:text]
17
- })
18
- end
19
-
20
- button_payload
21
- end
22
-
23
- private
24
-
25
- def button_type
26
- BUTTON_TYPE
27
- end
28
- end
29
- end
30
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Components
5
- class VoiceCallButton < Button
6
- BUTTON_TYPE = "voice_call"
7
-
8
- private
9
-
10
- def button_type
11
- BUTTON_TYPE
12
- end
13
- end
14
- end
15
- end
data/lib/warb/language.rb DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Language
5
- ENGLISH_US = "en_US"
6
- PORTUGUESE_BR = "pt_BR"
7
- end
8
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Resources
5
- class Currency < Resource
6
- BRL = "BRL"
7
- USD = "USD"
8
-
9
- attr_accessor :amount, :code, :fallback
10
-
11
- def initialize(**params)
12
- super(**params)
13
-
14
- @code = params[:code]
15
- @amount = params[:amount]
16
- @fallback = params[:fallback]
17
- end
18
-
19
- def build_template_named_parameter(parameter_name)
20
- common_currency_params.merge(parameter_name: parameter_name)
21
- end
22
-
23
- def build_template_positional_parameter
24
- common_currency_params
25
- end
26
-
27
- private
28
-
29
- def common_currency_params
30
- {
31
- type: "currency",
32
- currency: {
33
- amount_1000: amount * 1000,
34
- code: code,
35
- fallback_value: fallback || default_fallback_value
36
- }
37
- }
38
- end
39
-
40
- def default_fallback_value
41
- "#{amount} (#{code})"
42
- end
43
- end
44
- end
45
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Resources
5
- class DateTime < Resource
6
- attr_accessor :date_time
7
-
8
- def initialize(date_time = nil, **params)
9
- super(**params)
10
-
11
- @date_time = date_time || params[:date_time]
12
- end
13
-
14
- def build_template_named_parameter(parameter_name)
15
- common_date_time_params.merge(parameter_name: parameter_name)
16
- end
17
-
18
- def build_template_positional_parameter
19
- common_date_time_params
20
- end
21
-
22
- private
23
-
24
- def common_date_time_params
25
- {
26
- type: "date_time",
27
- date_time: {
28
- fallback_value: date_time
29
- }
30
- }
31
- end
32
- end
33
- end
34
- end
@@ -1,166 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Warb
4
- module Resources
5
- class Template < Resource
6
- attr_accessor :name, :language, :resources, :header, :buttons
7
-
8
- def initialize(**params)
9
- super(**params)
10
-
11
- @name = params[:name]
12
- @language = params[:language]
13
- @resources = params[:resources]
14
- @buttons = []
15
- end
16
-
17
- def build_payload
18
- {
19
- type: "template",
20
- template: {
21
- name: name,
22
- language: {
23
- code: language
24
- },
25
- components: [
26
- component_header,
27
- component_body,
28
- *buttons
29
- ].compact
30
- }
31
- }
32
- end
33
-
34
- def add_currency_parameter(parameter_name = nil, **params, &block)
35
- add_parameter(parameter_name, Currency.new(**params), &block)
36
- end
37
-
38
- def add_date_time_parameter(parameter_name = nil, **params, &block)
39
- add_parameter(parameter_name, DateTime.new(**params), &block)
40
- end
41
-
42
- def add_text_parameter(parameter_name = nil, **params, &block)
43
- add_parameter(parameter_name, Text.new(**params), &block)
44
- end
45
-
46
- def set_text_header(content: nil, message: nil, text: nil, parameter_name: nil, &block)
47
- set_header(Text.new(content:, message:, text:, parameter_name:), &block)
48
- end
49
-
50
- def set_image_header(media_id: nil, link: nil, &block)
51
- set_header(Image.new(media_id:, link:), &block)
52
- end
53
-
54
- def set_document_header(media_id: nil, link: nil, filename: nil, &block)
55
- set_header(Document.new(media_id:, link:, filename:), &block)
56
- end
57
-
58
- def set_video_header(media_id: nil, link: nil, &block)
59
- set_header(Video.new(media_id:, link:), &block)
60
- end
61
-
62
- def set_location_header(latitude: nil, longitude: nil, address: nil, name: nil, &block)
63
- set_header(Location.new(latitude:, longitude:, address:, name:), &block)
64
- end
65
-
66
- def add_quick_reply_button(index: position, &block)
67
- add_button(Warb::Components::QuickReplyButton.new(index:), &block)
68
- end
69
-
70
- def add_dynamic_url_button(index: position, text: nil, &block)
71
- add_button(Warb::Components::UrlButton.new(index:, text:), &block)
72
- end
73
-
74
- alias_method :add_auth_code_button, :add_dynamic_url_button
75
-
76
- def add_copy_code_button(index: position, coupon_code: nil, &block)
77
- add_button(Warb::Components::CopyCodeButton.new(index:, coupon_code:), &block)
78
- end
79
-
80
- def add_voice_call_button(index: position, &block)
81
- add_button(Warb::Components::VoiceCallButton.new(index:), &block)
82
- end
83
-
84
- def add_button(instance, &block)
85
- return @buttons << instance.to_h unless block_given?
86
-
87
- @buttons << instance.tap(&block).to_h
88
- end
89
-
90
- private
91
-
92
- def set_header(instance, &block)
93
- @header = instance
94
-
95
- block_given? ? @header.tap(&block) : @header
96
- end
97
-
98
-
99
- def component_header
100
- return unless header.is_a? Resource
101
-
102
- {
103
- type: "header",
104
- parameters: [
105
- header.build_header
106
- ]
107
- }
108
- end
109
-
110
- def component_body
111
- return if resources.nil? || resources.empty?
112
-
113
- {
114
- type: "body",
115
- parameters: build_parameters
116
- }
117
- end
118
-
119
- def build_parameters
120
- case resources
121
- when Hash
122
- named_parameters
123
- when Array
124
- positional_parameters
125
- end
126
- end
127
-
128
- def named_parameters
129
- resources.map do |parameter_name, resource|
130
- resource.build_template_named_parameter(parameter_name.to_s)
131
- end
132
- end
133
-
134
- def positional_parameters
135
- resources.map(&:build_template_positional_parameter)
136
- end
137
-
138
- def add_parameter(parameter_name, instance, &block)
139
- case resources
140
- when Hash
141
- resources[parameter_name.to_s] = instance
142
- when Array
143
- resources << instance
144
- else
145
- initialize_resources(parameter_name, instance, &block)
146
- end
147
-
148
- block_given? ? instance.tap(&block) : instance
149
- end
150
-
151
- def initialize_resources(parameter_name, instance)
152
- if parameter_name.nil?
153
- @resources = []
154
- @resources << instance
155
- else
156
- @resources = {}
157
- @resources[parameter_name] = instance
158
- end
159
- end
160
-
161
- def position
162
- buttons.count
163
- end
164
- end
165
- end
166
- end
@@ -1,10 +0,0 @@
1
- module Warb
2
- class TemplateDispatcher < Dispatcher
3
- def list(**args)
4
- filter = args.slice(:limit, :fields, :after, :before)
5
- filter[:fields] = filter[:fields].join(",") if filter[:fields].is_a?(Array)
6
-
7
- @client.get("message_templates", endpoint_prefix: :business_id, data: filter).body
8
- end
9
- end
10
- end