warb 1.0.0 → 1.0.1
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 +12 -5
- data/README.md +32 -9
- data/Rakefile +3 -3
- data/docs/README.md +4 -1
- data/docs/components/README.md +4 -1
- data/docs/components/button.md +62 -0
- data/docs/components/copy_code_button.md +57 -0
- data/docs/components/flow_button.md +102 -0
- data/docs/components/url_button.md +57 -0
- data/docs/messages/README.md +2 -1
- data/docs/messages/flow.md +241 -5
- data/docs/messages/interactive_call_to_action_url.md +9 -9
- data/docs/messages/interactive_list.md +2 -2
- data/docs/messages/interactive_reply_button.md +9 -9
- data/docs/messages/template.md +373 -0
- data/docs/resources/README.md +14 -0
- data/docs/resources/currency.md +22 -0
- data/docs/resources/date_time.md +11 -0
- data/docs/resources/text.md +9 -0
- data/docs/setup.md +45 -1
- data/examples/audio.rb +10 -10
- data/examples/document.rb +34 -34
- data/examples/image.rb +22 -22
- data/examples/interactive_call_to_action_url.rb +46 -46
- data/examples/interactive_list.rb +61 -61
- data/examples/interactive_reply_button.rb +43 -43
- data/examples/location.rb +32 -32
- data/examples/location_request.rb +11 -11
- data/examples/message.rb +8 -8
- data/examples/sticker.rb +10 -10
- data/examples/video.rb +22 -22
- data/examples/webhook.rb +77 -43
- data/lib/warb/category.rb +8 -0
- data/lib/warb/client.rb +7 -5
- data/lib/warb/components/action.rb +12 -8
- data/lib/warb/components/button.rb +29 -0
- data/lib/warb/components/component.rb +19 -0
- data/lib/warb/components/copy_code_button.rb +30 -0
- data/lib/warb/components/flow_button.rb +32 -0
- data/lib/warb/components/quick_reply_button.rb +15 -0
- data/lib/warb/components/url_button.rb +30 -0
- data/lib/warb/components/voice_call_button.rb +15 -0
- data/lib/warb/configuration.rb +4 -1
- data/lib/warb/connection.rb +15 -9
- data/lib/warb/dispatcher.rb +4 -3
- data/lib/warb/dispatcher_concern.rb +6 -0
- data/lib/warb/errors.rb +27 -0
- data/lib/warb/indicator_dispatcher.rb +4 -4
- data/lib/warb/language.rb +8 -0
- data/lib/warb/media_dispatcher.rb +10 -10
- data/lib/warb/resources/audio.rb +1 -1
- data/lib/warb/resources/contact.rb +22 -20
- data/lib/warb/resources/currency.rb +47 -0
- data/lib/warb/resources/date_time.rb +34 -0
- data/lib/warb/resources/document.rb +1 -1
- data/lib/warb/resources/flow.rb +82 -20
- data/lib/warb/resources/helpers/header.rb +35 -0
- data/lib/warb/resources/image.rb +1 -1
- data/lib/warb/resources/interactive_call_to_action_url.rb +10 -8
- data/lib/warb/resources/interactive_list.rb +7 -5
- data/lib/warb/resources/interactive_reply_button.rb +10 -8
- data/lib/warb/resources/location.rb +11 -1
- data/lib/warb/resources/location_request.rb +5 -3
- data/lib/warb/resources/reaction.rb +1 -1
- data/lib/warb/resources/resource.rb +14 -4
- data/lib/warb/resources/sticker.rb +1 -1
- data/lib/warb/resources/template.rb +163 -0
- data/lib/warb/resources/text.rb +31 -3
- data/lib/warb/resources/validation.rb +30 -0
- data/lib/warb/resources/video.rb +1 -1
- data/lib/warb/response.rb +33 -0
- data/lib/warb/response_error_handler.rb +42 -0
- data/lib/warb/template_dispatcher.rb +21 -0
- data/lib/warb/utils.rb +3 -1
- data/lib/warb/version.rb +1 -1
- data/lib/warb.rb +67 -31
- metadata +34 -3
|
@@ -28,7 +28,7 @@ Warb.interactive_call_to_action_url.dispatch(recipient_number) do |message|
|
|
|
28
28
|
message.footer = "footer"
|
|
29
29
|
|
|
30
30
|
# for header, there's a helper method to create a text header
|
|
31
|
-
message.
|
|
31
|
+
message.add_text_header("Check this out!")
|
|
32
32
|
|
|
33
33
|
# and for action, you can use the build_action method to create a CTA action`
|
|
34
34
|
message.build_action(button_text: "Click Here") do |action|
|
|
@@ -37,7 +37,7 @@ Warb.interactive_call_to_action_url.dispatch(recipient_number) do |message|
|
|
|
37
37
|
end
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
Aside from the text header, you can also set an image, video, or document as header using the respective methods, `
|
|
40
|
+
Aside from the text header, you can also set an image, video, or document as header using the respective methods, `add_image_header`, `add_video_header`, and `add_document_header`.
|
|
41
41
|
|
|
42
42
|
Here is an example of using an image as header:
|
|
43
43
|
|
|
@@ -47,7 +47,7 @@ Warb.interactive_call_to_action_url.dispatch(recipient_number) do |message|
|
|
|
47
47
|
message.footer = "footer"
|
|
48
48
|
|
|
49
49
|
# set an image as header
|
|
50
|
-
message.
|
|
50
|
+
message.add_image_header(link: "https://example.com/image.jpg")
|
|
51
51
|
|
|
52
52
|
# the build_action method also returns the built action
|
|
53
53
|
action = message.build_action do |action|
|
|
@@ -68,7 +68,7 @@ Warb.interactive_call_to_action_url.dispatch(recipient_number) do |message|
|
|
|
68
68
|
|
|
69
69
|
# set a document as header
|
|
70
70
|
# for documents, you can also pass a filename. its extension will be used to determine the MIME type
|
|
71
|
-
message.
|
|
71
|
+
message.add_document_header(link: "https://example.com/document.pdf", filename: "document.pdf")
|
|
72
72
|
|
|
73
73
|
message.build_action(url: "https://example.com", button_text: "Click here")
|
|
74
74
|
end
|
|
@@ -86,11 +86,11 @@ Here is a summary of the fields you can set in the `dispatch` method of the `int
|
|
|
86
86
|
For header, as seen above, you can set it using the following instance methods:
|
|
87
87
|
| Method | Named Parameters | Positional Parameters | Respective Resource Class |
|
|
88
88
|
|-----------------------|--------------------|------------------------|-----------------------------|
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
89
|
+
| `add_text_header` | - | the content header | `Warb::Resources::Text` |
|
|
90
|
+
| `add_image_header` | `link` | - | `Warb::Resources::Image` |
|
|
91
|
+
| `add_video_header` | `link` | - | `Warb::Resources::Video` |
|
|
92
|
+
| `add_document_header` | `link`, `filename` | - | `Warb::Resources::Document` |
|
|
93
93
|
|
|
94
94
|
Under the hood, the set header methods will create the respective resource object and call its `build_header` method to prepare the header for sending.
|
|
95
95
|
|
|
96
|
-
For the action, the `build_action` instance method will create a `Warb::Components::CTAAction` and return it
|
|
96
|
+
For the action, the `build_action` instance method will create a `Warb::Components::CTAAction` and return it
|
|
@@ -77,7 +77,7 @@ Warb.interactive_list.dispatch(recipient_number) do |message|
|
|
|
77
77
|
message.body = "Choose your preferred language"
|
|
78
78
|
message.footer = "You can change it later"
|
|
79
79
|
|
|
80
|
-
message.
|
|
80
|
+
message.add_text_header("Language Selection")
|
|
81
81
|
|
|
82
82
|
message.build_action(button_text: "Select") do |action|
|
|
83
83
|
section = action.add_section(title: nil)
|
|
@@ -108,7 +108,7 @@ Warb.interactive_list.dispatch(recipient_number) do |message|
|
|
|
108
108
|
message.body = "Choose your preferred language"
|
|
109
109
|
message.footer = "You can change it later"
|
|
110
110
|
|
|
111
|
-
message.
|
|
111
|
+
message.add_text_header("Language Selection")
|
|
112
112
|
|
|
113
113
|
message.build_action(button_text: "Select") do |action|
|
|
114
114
|
action.add_section(title: "American Languages") do |american_section|
|
|
@@ -23,7 +23,7 @@ Or you can use the block building strategy to simplify the process:
|
|
|
23
23
|
|
|
24
24
|
```ruby
|
|
25
25
|
Warb.interactive_reply_button.dispatch(recipient_number) do |message|
|
|
26
|
-
message.
|
|
26
|
+
message.add_image_header(media_id: "1341834336894773")
|
|
27
27
|
|
|
28
28
|
message.body = "Select a Language:"
|
|
29
29
|
message.footer = nil
|
|
@@ -38,23 +38,23 @@ or you can build the action with the buttons texts directly, passing them to the
|
|
|
38
38
|
|
|
39
39
|
```ruby
|
|
40
40
|
Warb.interactive_reply_button.dispatch(recipient_number, action: action) do |message|
|
|
41
|
-
message.
|
|
41
|
+
message.add_text_header(content: "Options")
|
|
42
42
|
message.body = "Select a Language:"
|
|
43
43
|
message.footer = nil
|
|
44
44
|
message.build_action(buttons_texts: ["Português", "English", "Español"])
|
|
45
45
|
end
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
Aside from text and image headers, you can also set a video or document as header using the respective methods, `
|
|
48
|
+
Aside from text and image headers, you can also set a video or document as header using the respective methods, `add_video_header` and `add_document_header`.
|
|
49
49
|
|
|
50
|
-
For text header, `
|
|
50
|
+
For text header, `add_text_header` receives a string, while for image, video, and document headers, you can pass a `media_id` or a `link` to the media file.
|
|
51
51
|
For header, as seen above, you can set it using the following instance methods:
|
|
52
52
|
| Method | Named Parameters | Positional Parameters | Respective Resource Class |
|
|
53
53
|
|-----------------------|--------------------------------|------------------------|-----------------------------|
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
56
|
-
| `
|
|
57
|
-
| `
|
|
54
|
+
| `add_text_header` | - | the content header | `Warb::Resources::Text` |
|
|
55
|
+
| `add_image_header` | `link`, `media_id` | - | `Warb::Resources::Image` |
|
|
56
|
+
| `add_video_header` | `link`, `media_id` | - | `Warb::Resources::Video` |
|
|
57
|
+
| `add_document_header` | `link`, `media_id`, `filename` | - | `Warb::Resources::Document` |
|
|
58
58
|
|
|
59
59
|
Either `link` or `media_id` can be used, but not both at the same time.
|
|
60
60
|
|
|
@@ -64,4 +64,4 @@ If you're using a `media_id`, it must be obtained from a previous upload using t
|
|
|
64
64
|
|
|
65
65
|
The `filename` for the document header is optional, but if provided, it will be used to determine the MIME type of the document.
|
|
66
66
|
|
|
67
|
-
For the `action` of the message, you can use the [`Warb::Components::ReplyButtonAction`](../components/reply_button_action.md) class to build the action with the buttons texts.
|
|
67
|
+
For the `action` of the message, you can use the [`Warb::Components::ReplyButtonAction`](../components/reply_button_action.md) class to build the action with the buttons texts.
|
|
@@ -0,0 +1,373 @@
|
|
|
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` | `add_image_header` | `media_id` or `link` |
|
|
220
|
+
| `video` | `add_video_header` | `media_id` or `link` |
|
|
221
|
+
| `document` | `add_document_header` | `media_id` or `link`, `filename` |
|
|
222
|
+
| `location` | `add_location_header` | `latitude`, `longitude`, `name`, and `address` |
|
|
223
|
+
| `text` | `add_text_header` | `content`, `parameter_name` |
|
|
224
|
+
|
|
225
|
+
Every time a call is made to any `add_header` method, a new header will be set, overwriting the previous one.
|
|
226
|
+
|
|
227
|
+
If you just want to change one attribute or another, `add_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.add_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
|
+
`add_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, `add_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 `add_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 `add_text_header` as follow:
|
|
255
|
+
```ruby
|
|
256
|
+
Warb.template.dispatch(recipient_number) do |template|
|
|
257
|
+
template.add_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
|
+
| `flow` | `add_flow_button` | `index`, `flow_token`, `flow_action_data` |
|
|
275
|
+
| `doesn't apply` | `add_button` | `instance`, `&block` |
|
|
276
|
+
|
|
277
|
+
You can either use the keyword parameters or set the attributes using a block:
|
|
278
|
+
|
|
279
|
+
```ruby
|
|
280
|
+
Warb.template.dispatch(recipient_number) do |template|
|
|
281
|
+
template.name = "order_confirmation"
|
|
282
|
+
template.language = Warb::Language::ENGLISH_US
|
|
283
|
+
|
|
284
|
+
# Add a quick reply button
|
|
285
|
+
template.add_quick_reply_button
|
|
286
|
+
|
|
287
|
+
# Add a dynamic URL button
|
|
288
|
+
template.add_dynamic_url_button do |button|
|
|
289
|
+
button.text = "dynamic-url-suffix"
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# Add a copy auth code button
|
|
293
|
+
template.add_auth_code_button do |button|
|
|
294
|
+
button.text = "4UTHC0D3"
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Add a copy code button
|
|
298
|
+
template.add_copy_code_button(index: 2) do |button|
|
|
299
|
+
button.coupon_code = "SAVE20"
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Add a voice call button
|
|
303
|
+
template.add_voice_call_button
|
|
304
|
+
|
|
305
|
+
# Add a flow button
|
|
306
|
+
template.add_flow_button do |button|
|
|
307
|
+
button.flow_token = 'FLOWTOKEN'
|
|
308
|
+
button.flow_action_data = { name: 'John' }
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
or if you'd rather to, you can add buttons using it's component and the `add_button` method:
|
|
314
|
+
|
|
315
|
+
```ruby
|
|
316
|
+
quick_reply_btn = Warb::Components::QuickReplyButton.new
|
|
317
|
+
|
|
318
|
+
Warb.template.dispatch(recipient_number) do |template|
|
|
319
|
+
template.name = "order_confirmation"
|
|
320
|
+
template.language = Warb::Language::ENGLISH_US
|
|
321
|
+
|
|
322
|
+
# Add a quick reply button
|
|
323
|
+
template.add_button(quick_reply_btn)
|
|
324
|
+
|
|
325
|
+
# Add a quick reply button, passing a block.
|
|
326
|
+
template.add_button(quick_reply_btn) { |button| button.index = 1 }
|
|
327
|
+
end
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
**Note**: The `index` parameter determines the order of the buttons in the template. Make sure the
|
|
331
|
+
indices match the button positions defined in your template. The `index` is automaticaly set if you
|
|
332
|
+
don't do it manually, but it is done based on the number of buttons added with the methods above,
|
|
333
|
+
so if your template has a button that doesn't need configuration like the static url button you'll
|
|
334
|
+
have provide the position of the other buttons.
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
#### Creating Templates
|
|
338
|
+
|
|
339
|
+
To create a template, you need to provide the template name, language, category and the body (components).
|
|
340
|
+
|
|
341
|
+
```ruby
|
|
342
|
+
Warb.template.create(
|
|
343
|
+
name: "my_template_001",
|
|
344
|
+
language: Warb::Language::ENGLISH_US,
|
|
345
|
+
category: Warb::Category::MARKETING,
|
|
346
|
+
body: Warb::Resources::Text.new(
|
|
347
|
+
text: "Hello {{1}}, welcome to our service!",
|
|
348
|
+
examples: ["John"]
|
|
349
|
+
)
|
|
350
|
+
)
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
**Required Parameters:**
|
|
354
|
+
| Parameter | Type | Description |
|
|
355
|
+
|------------|----------|----------------------------------------------|
|
|
356
|
+
| `name` | `String` | Unique template name (snake_case) |
|
|
357
|
+
| `language` | `String` | The language to use for the template |
|
|
358
|
+
| `category` | `String` | Template category (MARKETING, UTILITY, etc.) |
|
|
359
|
+
|
|
360
|
+
**Template Categories:**
|
|
361
|
+
- `Warb::Category::MARKETING` - Promotional content
|
|
362
|
+
- `Warb::Category::UTILITY` - Transactional messages
|
|
363
|
+
- `Warb::Category::AUTHENTICATION` - Security codes
|
|
364
|
+
|
|
365
|
+
**Note**: Currently, the creation only supports body text, more resource types are coming soon.
|
|
366
|
+
|
|
367
|
+
#### Deleting Templates
|
|
368
|
+
|
|
369
|
+
To delete a template, you need to provide the template name.
|
|
370
|
+
|
|
371
|
+
```ruby
|
|
372
|
+
Warb.template.delete("my_template_001")
|
|
373
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Resources
|
|
2
|
+
|
|
3
|
+
Most of the resources can be sent as message. Check the [messages](../messages/README.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) |
|
|
@@ -0,0 +1,22 @@
|
|
|
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.
|
|
@@ -0,0 +1,11 @@
|
|
|
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.
|
|
@@ -0,0 +1,9 @@
|
|
|
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.
|
data/docs/setup.md
CHANGED
|
@@ -43,4 +43,48 @@ Warb.setup do |config|
|
|
|
43
43
|
end
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
Also, note that calling `Warb.setup` multiple times **WILL NOT** override the previous configuration, so you can use it to change the global configuration at any time.
|
|
46
|
+
Also, note that calling `Warb.setup` multiple times **WILL NOT** override the previous configuration, so you can use it to change the global configuration at any time.
|
|
47
|
+
|
|
48
|
+
## Phone numbers (quality monitoring)
|
|
49
|
+
|
|
50
|
+
You can fetch all phone numbers attached to your WhatsApp Business Account (WABA) and their quality/operational signals.
|
|
51
|
+
|
|
52
|
+
Requirements: make sure you have configured a business_id in your global setup (this method uses the business context, not the sender/phone context).
|
|
53
|
+
```ruby
|
|
54
|
+
Warb.setup do |config|
|
|
55
|
+
config.access_token = "ACCESS_TOKEN"
|
|
56
|
+
config.business_id = "BUSINESS_ID" # <-- required here
|
|
57
|
+
config.sender_id = "SENDER_ID" # still used for message dispatch
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### Global usage
|
|
62
|
+
```ruby
|
|
63
|
+
phones = Warb.list_phone_numbers
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Sample response (unwrapped data array):
|
|
67
|
+
```ruby
|
|
68
|
+
=> [
|
|
69
|
+
{
|
|
70
|
+
"verified_name" => "Test",
|
|
71
|
+
"code_verification_status" => "NOT_VERIFIED",
|
|
72
|
+
"display_phone_number" => "00000000000",
|
|
73
|
+
"quality_rating" => "GREEN",
|
|
74
|
+
"platform_type" => "CLOUD_API",
|
|
75
|
+
"throughput" => { "level" => "STANDARD" },
|
|
76
|
+
"webhook_configuration" => { "application" => "https://example.com/" },
|
|
77
|
+
"id" => "(phone_number_id)"
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Notes
|
|
83
|
+
|
|
84
|
+
This method returns the data array directly. If you need paging cursors, call the raw client:
|
|
85
|
+
```ruby
|
|
86
|
+
raw = Warb.client.get("phone_numbers", {}, endpoint_prefix: :business_id)
|
|
87
|
+
raw.body # => { "data" => [...], "paging" => { "cursors" => {...} } }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Non-2xx responses raise Warb::RequestError (or subclasses), so you can rescue them in your app.
|
data/examples/audio.rb
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative
|
|
3
|
+
require_relative '../lib/warb'
|
|
4
4
|
|
|
5
5
|
# Configure your variables here
|
|
6
6
|
|
|
7
|
-
access_token =
|
|
8
|
-
business_id =
|
|
9
|
-
sender_id =
|
|
10
|
-
recipient_number =
|
|
7
|
+
access_token = ''
|
|
8
|
+
business_id = ''
|
|
9
|
+
sender_id = ''
|
|
10
|
+
recipient_number = ''
|
|
11
11
|
|
|
12
|
-
audio_link =
|
|
12
|
+
audio_link = ''
|
|
13
13
|
|
|
14
14
|
# We recommend testing one section at a time, as it can be overwhelming to see all the messages at once.
|
|
15
15
|
# So you can comment out the sections you don't want to test.
|
|
@@ -25,8 +25,8 @@ warb_from_setup = Warb.setup do |config|
|
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
# To send audio using its ID, you may need to retrieve it first, which can be retrieved this way
|
|
28
|
-
file_path =
|
|
29
|
-
file_type =
|
|
28
|
+
file_path = '' # fill this in with the file path pointing to wherever the audio is located
|
|
29
|
+
file_type = '' # fill this in with the mimetype of the audio to be uploaded
|
|
30
30
|
# allow values for file_type: audio/aac, audio/amr, audio/mpeg, audio/mp4 or audio/ogg
|
|
31
31
|
audio_id = warb_from_setup.audio.upload(file_path: file_path, file_type: file_type)
|
|
32
32
|
# if you already have an audio id, you can simply replace the above line with such id
|
|
@@ -53,8 +53,8 @@ warb_from_new = Warb.new(
|
|
|
53
53
|
)
|
|
54
54
|
|
|
55
55
|
# Same as stated above, if you need an audio id, you can upload it this way
|
|
56
|
-
file_path =
|
|
57
|
-
file_type =
|
|
56
|
+
file_path = '' # fill this in with the file path pointing to wherever the audio is located
|
|
57
|
+
file_type = '' # fill this in with the mimetype of the audio to be uploaded
|
|
58
58
|
# allow values for file_type: audio/aac, audio/amr, audio/mpeg, audio/mp4 or audio/ogg
|
|
59
59
|
audio_id = warb_from_setup.audio.upload(file_path: file_path, file_type: file_type)
|
|
60
60
|
# if you already have an audio id, you can simply replace the above line with such id
|