warb 0.1.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.
Files changed (104) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +8 -0
  4. data/CHANGELOG.md +25 -0
  5. data/README.md +135 -0
  6. data/Rakefile +12 -0
  7. data/docs/README.md +16 -0
  8. data/docs/components/README.md +24 -0
  9. data/docs/components/address.md +68 -0
  10. data/docs/components/button.md +61 -0
  11. data/docs/components/copy_code_button.md +57 -0
  12. data/docs/components/cta_action.md +13 -0
  13. data/docs/components/email.md +40 -0
  14. data/docs/components/list_action.md +29 -0
  15. data/docs/components/name.md +49 -0
  16. data/docs/components/org.md +42 -0
  17. data/docs/components/phone.md +57 -0
  18. data/docs/components/reply_button_action.md +32 -0
  19. data/docs/components/row.md +13 -0
  20. data/docs/components/section.md +30 -0
  21. data/docs/components/url.md +40 -0
  22. data/docs/components/url_button.md +57 -0
  23. data/docs/images/contact-with-wa_id.png +0 -0
  24. data/docs/images/contact-without-wa_id.png +0 -0
  25. data/docs/messages/README.md +80 -0
  26. data/docs/messages/audio.md +119 -0
  27. data/docs/messages/contact.md +134 -0
  28. data/docs/messages/document.md +122 -0
  29. data/docs/messages/flow.md +12 -0
  30. data/docs/messages/image.md +116 -0
  31. data/docs/messages/indicator.md +39 -0
  32. data/docs/messages/interactive_call_to_action_url.md +96 -0
  33. data/docs/messages/interactive_list.md +159 -0
  34. data/docs/messages/interactive_reply_button.md +67 -0
  35. data/docs/messages/location.md +34 -0
  36. data/docs/messages/location_request.md +21 -0
  37. data/docs/messages/reaction.md +23 -0
  38. data/docs/messages/sticker.md +116 -0
  39. data/docs/messages/template.md +327 -0
  40. data/docs/messages/text.md +47 -0
  41. data/docs/messages/video.md +116 -0
  42. data/docs/resources/currency.md +22 -0
  43. data/docs/resources/date_time.md +11 -0
  44. data/docs/resources/index.md +14 -0
  45. data/docs/resources/text.md +9 -0
  46. data/docs/setup.md +46 -0
  47. data/docs/webhook.md +24 -0
  48. data/examples/audio.rb +86 -0
  49. data/examples/document.rb +116 -0
  50. data/examples/image.rb +97 -0
  51. data/examples/interactive_call_to_action_url.rb +177 -0
  52. data/examples/interactive_list.rb +201 -0
  53. data/examples/interactive_reply_button.rb +174 -0
  54. data/examples/location.rb +85 -0
  55. data/examples/location_request.rb +55 -0
  56. data/examples/message.rb +61 -0
  57. data/examples/sticker.rb +86 -0
  58. data/examples/video.rb +96 -0
  59. data/examples/webhook.rb +144 -0
  60. data/lib/warb/client.rb +46 -0
  61. data/lib/warb/components/action.rb +121 -0
  62. data/lib/warb/components/address.rb +31 -0
  63. data/lib/warb/components/button.rb +29 -0
  64. data/lib/warb/components/component.rb +19 -0
  65. data/lib/warb/components/copy_code_button.rb +30 -0
  66. data/lib/warb/components/email.rb +21 -0
  67. data/lib/warb/components/name.rb +29 -0
  68. data/lib/warb/components/org.rb +23 -0
  69. data/lib/warb/components/phone.rb +23 -0
  70. data/lib/warb/components/quick_reply_button.rb +15 -0
  71. data/lib/warb/components/url.rb +21 -0
  72. data/lib/warb/components/url_button.rb +30 -0
  73. data/lib/warb/components/voice_call_button.rb +15 -0
  74. data/lib/warb/configuration.rb +13 -0
  75. data/lib/warb/connection.rb +47 -0
  76. data/lib/warb/dispatcher.rb +16 -0
  77. data/lib/warb/dispatcher_concern.rb +73 -0
  78. data/lib/warb/indicator_dispatcher.rb +31 -0
  79. data/lib/warb/language.rb +8 -0
  80. data/lib/warb/media_dispatcher.rb +46 -0
  81. data/lib/warb/resources/audio.rb +19 -0
  82. data/lib/warb/resources/contact.rb +89 -0
  83. data/lib/warb/resources/currency.rb +45 -0
  84. data/lib/warb/resources/date_time.rb +34 -0
  85. data/lib/warb/resources/document.rb +32 -0
  86. data/lib/warb/resources/flow.rb +34 -0
  87. data/lib/warb/resources/image.rb +31 -0
  88. data/lib/warb/resources/interactive_call_to_action_url.rb +48 -0
  89. data/lib/warb/resources/interactive_list.rb +36 -0
  90. data/lib/warb/resources/interactive_reply_button.rb +48 -0
  91. data/lib/warb/resources/location.rb +31 -0
  92. data/lib/warb/resources/location_request.rb +24 -0
  93. data/lib/warb/resources/reaction.rb +19 -0
  94. data/lib/warb/resources/resource.rb +59 -0
  95. data/lib/warb/resources/sticker.rb +19 -0
  96. data/lib/warb/resources/template.rb +166 -0
  97. data/lib/warb/resources/text.rb +47 -0
  98. data/lib/warb/resources/video.rb +31 -0
  99. data/lib/warb/template_dispatcher.rb +10 -0
  100. data/lib/warb/utils.rb +5 -0
  101. data/lib/warb/version.rb +5 -0
  102. data/lib/warb.rb +69 -0
  103. data/sig/warb.rbs +4 -0
  104. metadata +172 -0
@@ -0,0 +1,327 @@
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.
@@ -0,0 +1,47 @@
1
+ # Text
2
+
3
+ Send a simple text message using the `text` wrapper:
4
+
5
+ ```ruby
6
+ recipient_number = "..."
7
+ Warb.message.dispatch(recipient_number, ...)
8
+ ```
9
+
10
+ **Note**: For the examples below, take into account `Warb.setup` was called to configure the global client instance, and that the variable `recipient_number` is already set.
11
+
12
+ You can send a simple text message like this:
13
+
14
+ ```ruby
15
+ Warb.message.dispatch(recipient_number, content: "Hello, World!")
16
+ ```
17
+
18
+ You can also use the block building strategy to send a text message:
19
+
20
+ ```ruby
21
+ Warb.message.dispatch(recipient_number) do |text|
22
+ text.content = "Hey, check this out! https://www.google.com"
23
+ text.preview_url = true
24
+ end
25
+ ```
26
+
27
+ > If `preview_url` is set to `true`, the recipient WhatsApp phone/computer will try to generate a preview for the URL in the content.
28
+
29
+ | Field | Type | Description | Required |
30
+ |---------------|---------|-----------------------------------------------------------------------------|------------------------------------------|
31
+ | `preview_url` | Boolean | Whether to generate a preview for URLs in the content. Defaults to `false`. | No |
32
+ | `content` | String | The text content of the message. | Yes * |
33
+ | `text` | String | The text content of the message. | Yes * |
34
+ | `message` | String | The text content of the message. | Yes * |
35
+
36
+ > `content`, `text`, and `message` are interchangeable, you can use any of them to set the text content of the message.
37
+ > But if using multiple of them, the priority is: `content` > `text` > `message`.
38
+
39
+ Also, remember that you can use `reply_to` to reply to a previous message:
40
+
41
+ ```ruby
42
+ Warb.message.dispatch(recipient_number, content: "Hello, World!", reply_to: "message_id")
43
+ ```
44
+
45
+ Since text messages are the most common to be sent as replies, we're reforcing the use of `reply_to` here in the `text` wrapper. But keep in mind it can used for any other message type, like `image`, `video`, `document`, etc.
46
+
47
+ **Remember**: it is a param for the `dispatch` method, not a field of the message itself.
@@ -0,0 +1,116 @@
1
+ # Video
2
+
3
+ For starters, not all video formats are supported by WhatsApp. Here is a table of supported video formats:
4
+
5
+ | Video Type | Extension | MIME Type | Max Size |
6
+ |------------|-----------|--------------|----------|
7
+ | MP4 | `.mp4` | `video/mp4` | 16 MB |
8
+ | 3GPP | `.3gp` | `video/3gpp` | 16 MB |
9
+
10
+ To send a video message, use the `video` dispatch wrapper as follows:
11
+
12
+ ```ruby
13
+ recipient_number = "..."
14
+ Warb.video.dispatch(recipient_number, ...)
15
+ ```
16
+
17
+ **Note**: For the examples below, take into account `Warb.setup` was called to configure the global client instance, and that the variable `recipient_number` is already set.
18
+
19
+ You can send a simple video message like this:
20
+
21
+ ```ruby
22
+ Warb.video.dispatch(recipient_number, link: "https://example.com/video.mp4")
23
+ ```
24
+
25
+ You can also use the block building strategy to send a video message:
26
+
27
+ ```ruby
28
+ Warb.video.dispatch(recipient_number) do |video|
29
+ video.link = "https://example.com/video.mp4"
30
+ end
31
+ ```
32
+
33
+ As seen above, the `link` field is used to set the video file URL. This must be a direct link to a video file hosted online.
34
+
35
+ Also, if using a link, the video file must be publicly accessible, meaning it should not require any authentication or special permissions to access.
36
+
37
+ Aside from a link, you can also send a video by using its ID. For that, you need to have the ID of the video file already uploaded to WhatsApp.
38
+
39
+ #### Upload
40
+
41
+ Since our `video` wrapper is a media dispatcher, it supports media-related operations, like `upload` and `download` methods.
42
+
43
+ So, we can use the `upload` method to upload a video file and get its ID:
44
+
45
+ ```ruby
46
+ video_id = Warb.video.upload(file_path: "path/to/video.mp4", file_type: "video/mp4")
47
+ ```
48
+
49
+ > `file_type` must be one of the supported video types listed [above](#), and `file_path` is the path to the video file you want to upload.
50
+
51
+ **Note**: At this point, it is not possible to retrieve the ID of a video file that was previously uploaded, so keep the ID returned by the `upload` method for later use.
52
+
53
+ **Note**: Also, note that uploaded videos are stored on WhatsApp servers for 30 days. After that period, they will be deleted and you won't be able to use the ID anymore.
54
+
55
+ #### Sending with Video ID
56
+
57
+ With the video ID in hand, you can use it to send the video message:
58
+
59
+ ```ruby
60
+ Warb.video.dispatch(recipient_number, media_id: video_id)
61
+ ```
62
+
63
+ You can also use the block building strategy to send a video message with an ID:
64
+
65
+ ```ruby
66
+ Warb.video.dispatch(recipient_number) do |video|
67
+ video.media_id = video_id
68
+ end
69
+ ```
70
+
71
+ **Note**: Either only one of the `link` or `media_id` fields can be set at a time.
72
+
73
+ #### Downloading Video
74
+
75
+ You can also download a video file using its ID:
76
+
77
+ ```ruby
78
+ Warb.video.download(media_id: video_id, file_path: "path/to/save/video.mp4")
79
+ ```
80
+
81
+ This will download the video file to the specified path.
82
+
83
+ Under the hood, the `download` method will use the `retrieve` method to get the download URL.
84
+
85
+ With the URL in hand, the `download` method will, in fact, download the file to the specified path.
86
+
87
+ The `retrieve` method receives a single parameter, which is the `media_id` of the video file you want to retrieve.
88
+
89
+ ```ruby
90
+ Warb.video.retrieve(media_id: video_id)
91
+ ```
92
+
93
+ And here is a sample response from the `retrieve` method:
94
+
95
+ ```json
96
+ {
97
+ "url": "https://lookaside.fbsbx.com/...",
98
+ "mime_type": "video/mp4",
99
+ "sha256": "4b4719...",
100
+ "file_size": 1048576,
101
+ "id": "134183...",
102
+ "messaging_product": "whatsapp"
103
+ }
104
+ ```
105
+
106
+ #### Deleting Video
107
+
108
+ You can delete a video file using its ID:
109
+
110
+ ```ruby
111
+ Warb.video.delete(video_id)
112
+ ```
113
+
114
+ This will delete the video file from WhatsApp servers and return `true` on success.
115
+
116
+ This is useful if you want to free up space or if you no longer need the video file.
@@ -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,14 @@
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) |
@@ -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 ADDED
@@ -0,0 +1,46 @@
1
+ # Setup
2
+
3
+ ## Global
4
+ You can configure `Warb` globally as follow:
5
+
6
+ ```ruby
7
+ Warb.setup do |config|
8
+ config.access_token = "access_token"
9
+ config.sender_id = "sender_id"
10
+ end
11
+ ```
12
+
13
+ This way, every time you use any method from Warb, like `Warb.message` or `Warb.audio`, the global configuration is going to be used automatically.
14
+
15
+ So if you're running a Rails Application, you can put this code in an initializer file, like `config/initializers/warb.rb`.
16
+
17
+ If you're not using Rails, you can put it in your main application file, like `app.rb` or `main.rb`.
18
+
19
+
20
+ ## Local
21
+
22
+ Instead of using the global configuration, you might want to use a different configuration for a specific task. In such cases, it's possible to instatiate a client directly using `Warb.new`:
23
+
24
+ ```ruby
25
+ client = Warb.new(access_token: "access_token", sender_id: "sender_id")
26
+ client.message.dispatch(...)
27
+ ```
28
+
29
+ Also, it is possible to reuse the global configuration but override some of the parameters, like `sender_id`:
30
+
31
+ ```ruby
32
+ client = Warb.new(sender_id: "another_id")
33
+ # This client will use the global `access_token` but a different `sender_id`
34
+ client.message.dispatch(...)
35
+ ```
36
+
37
+ Aside from the `access_token` and `sender_id`, you can also pass any other parameter that is supported by the `Warb::Client` class, a logger, or a custom HTTP client.
38
+
39
+ ```ruby
40
+ Warb.setup do |config|
41
+ config.logger = Logger.new($stdout) # or any other logger you prefer
42
+ config.adapter = :net_http # or any other HTTP client adapter you prefer
43
+ end
44
+ ```
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.
data/docs/webhook.md ADDED
@@ -0,0 +1,24 @@
1
+ # Webhook
2
+
3
+ You need to setup a webhook to receive messages from WhatsApp. The webhook is a URL that WhatsApp will call whenever there is an event related to your WhatsApp Business Account, such as receiving a message or a status update.
4
+
5
+ Such status or updates include:
6
+ - New Messages
7
+ - Message delivery status
8
+ - Message read status
9
+ - Message reaction
10
+ - Message template status
11
+ - Message interactive list response
12
+ - Message interactive reply button response
13
+ - Errors responses
14
+
15
+ To set up a webhook, follow these steps:
16
+ 1. **Create a Webhook URL**: This is the URL that WhatsApp will call to send you updates. It should be publicly accessible and able to handle POST requests.
17
+ 2. **Configure the Webhook in WhatsApp**: Go to your WhatsApp Business Account settings and configure the webhook URL. You will need to provide the URL and select the events you want to receive.
18
+ 3. **Handle Incoming Webhook Requests**: In your application, you need to create an endpoint that can handle incoming POST requests from WhatsApp. This endpoint should parse the incoming data and respond with a 200 OK status to acknowledge receipt of the message.
19
+ 4. **Verify the Webhook**: WhatsApp will send a verification request to your webhook URL. You need to respond with the `hub.challenge` parameter to verify that you own the webhook URL.
20
+ 5. **Test the Webhook**: Send a test message to your WhatsApp Business Account to ensure that your webhook is working correctly and that you are receiving updates as expected.
21
+
22
+ After configuring the webhook in the WhatsApp Business Account settings, for a quick start, you can refer to [webhook.rb example](../examples/webhook.rb) to see how to handle incoming webhook requests in your application.
23
+
24
+ Or you can refer to the official [Webhook Setup](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/set-up-webhooks) or [Webhook payload structure](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components) for more details.