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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +25 -0
- data/README.md +135 -0
- data/Rakefile +12 -0
- data/docs/README.md +16 -0
- data/docs/components/README.md +24 -0
- data/docs/components/address.md +68 -0
- data/docs/components/button.md +61 -0
- data/docs/components/copy_code_button.md +57 -0
- data/docs/components/cta_action.md +13 -0
- data/docs/components/email.md +40 -0
- data/docs/components/list_action.md +29 -0
- data/docs/components/name.md +49 -0
- data/docs/components/org.md +42 -0
- data/docs/components/phone.md +57 -0
- data/docs/components/reply_button_action.md +32 -0
- data/docs/components/row.md +13 -0
- data/docs/components/section.md +30 -0
- data/docs/components/url.md +40 -0
- data/docs/components/url_button.md +57 -0
- data/docs/images/contact-with-wa_id.png +0 -0
- data/docs/images/contact-without-wa_id.png +0 -0
- data/docs/messages/README.md +80 -0
- data/docs/messages/audio.md +119 -0
- data/docs/messages/contact.md +134 -0
- data/docs/messages/document.md +122 -0
- data/docs/messages/flow.md +12 -0
- data/docs/messages/image.md +116 -0
- data/docs/messages/indicator.md +39 -0
- data/docs/messages/interactive_call_to_action_url.md +96 -0
- data/docs/messages/interactive_list.md +159 -0
- data/docs/messages/interactive_reply_button.md +67 -0
- data/docs/messages/location.md +34 -0
- data/docs/messages/location_request.md +21 -0
- data/docs/messages/reaction.md +23 -0
- data/docs/messages/sticker.md +116 -0
- data/docs/messages/template.md +327 -0
- data/docs/messages/text.md +47 -0
- data/docs/messages/video.md +116 -0
- data/docs/resources/currency.md +22 -0
- data/docs/resources/date_time.md +11 -0
- data/docs/resources/index.md +14 -0
- data/docs/resources/text.md +9 -0
- data/docs/setup.md +46 -0
- data/docs/webhook.md +24 -0
- data/examples/audio.rb +86 -0
- data/examples/document.rb +116 -0
- data/examples/image.rb +97 -0
- data/examples/interactive_call_to_action_url.rb +177 -0
- data/examples/interactive_list.rb +201 -0
- data/examples/interactive_reply_button.rb +174 -0
- data/examples/location.rb +85 -0
- data/examples/location_request.rb +55 -0
- data/examples/message.rb +61 -0
- data/examples/sticker.rb +86 -0
- data/examples/video.rb +96 -0
- data/examples/webhook.rb +144 -0
- data/lib/warb/client.rb +46 -0
- data/lib/warb/components/action.rb +121 -0
- data/lib/warb/components/address.rb +31 -0
- 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/email.rb +21 -0
- data/lib/warb/components/name.rb +29 -0
- data/lib/warb/components/org.rb +23 -0
- data/lib/warb/components/phone.rb +23 -0
- data/lib/warb/components/quick_reply_button.rb +15 -0
- data/lib/warb/components/url.rb +21 -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 +13 -0
- data/lib/warb/connection.rb +47 -0
- data/lib/warb/dispatcher.rb +16 -0
- data/lib/warb/dispatcher_concern.rb +73 -0
- data/lib/warb/indicator_dispatcher.rb +31 -0
- data/lib/warb/language.rb +8 -0
- data/lib/warb/media_dispatcher.rb +46 -0
- data/lib/warb/resources/audio.rb +19 -0
- data/lib/warb/resources/contact.rb +89 -0
- data/lib/warb/resources/currency.rb +45 -0
- data/lib/warb/resources/date_time.rb +34 -0
- data/lib/warb/resources/document.rb +32 -0
- data/lib/warb/resources/flow.rb +34 -0
- data/lib/warb/resources/image.rb +31 -0
- data/lib/warb/resources/interactive_call_to_action_url.rb +48 -0
- data/lib/warb/resources/interactive_list.rb +36 -0
- data/lib/warb/resources/interactive_reply_button.rb +48 -0
- data/lib/warb/resources/location.rb +31 -0
- data/lib/warb/resources/location_request.rb +24 -0
- data/lib/warb/resources/reaction.rb +19 -0
- data/lib/warb/resources/resource.rb +59 -0
- data/lib/warb/resources/sticker.rb +19 -0
- data/lib/warb/resources/template.rb +166 -0
- data/lib/warb/resources/text.rb +47 -0
- data/lib/warb/resources/video.rb +31 -0
- data/lib/warb/template_dispatcher.rb +10 -0
- data/lib/warb/utils.rb +5 -0
- data/lib/warb/version.rb +5 -0
- data/lib/warb.rb +69 -0
- data/sig/warb.rbs +4 -0
- metadata +172 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Interactive Reply Button Action
|
|
2
|
+
|
|
3
|
+
An interactive reply button action allows you to create a set of buttons that, when clicked, send a predefined reply back to the sender.
|
|
4
|
+
|
|
5
|
+
You don't need to create the buttons manually, as the `Warb::Resources::InteractiveReplyButton` class provides a convenient way to build the action with the buttons texts.
|
|
6
|
+
|
|
7
|
+
The only `parameter` possible to be set is `buttons_texts`, which is an array of strings representing the texts for each button:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
action = Warb::Components::ReplyButtonAction.new(buttons_texts: ["Português", "English", "Español"])
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
and could be used like this:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
Warb.interactive_reply_button.dispatch(recipient_number, action: action) do |message|
|
|
17
|
+
# Set the header, body, and footer as needed
|
|
18
|
+
end
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
You can also use the `add_button_text` method to add buttons one by one:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
action = Warb::Components::ReplyButtonAction.new
|
|
25
|
+
action.add_button_text("Português")
|
|
26
|
+
action.add_button_text("English")
|
|
27
|
+
action.add_button_text("Español")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Also, note that you can only add up to 3 buttons in a single action
|
|
31
|
+
|
|
32
|
+
Check the [Interactive Reply Button Message](../messages/interactive_reply_button.md) documentation for more details on how to use this action in a message.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Row
|
|
2
|
+
|
|
3
|
+
Row is a component that represents a single item in a section of an interactive list message. It is used to display an option or choice to the user.
|
|
4
|
+
|
|
5
|
+
You don't need to create a row by youself, as the `Warb::Components::Section` will handle that for you.
|
|
6
|
+
|
|
7
|
+
Here are its attributes:
|
|
8
|
+
| Attribute | Type | Required | Max Length | Description |
|
|
9
|
+
|-------------|--------|----------|------------|-----------------------------------------------------------------------------|
|
|
10
|
+
| title | String | Yes | 24 | The title of the row, which is displayed to the user |
|
|
11
|
+
| description | String | No | 72 | A brief description of the row, providing additional context or information |
|
|
12
|
+
|
|
13
|
+
Check its usage in the [section component documentation](./section.md) or a more complete guide in the [interactive list message documentation](../messages/interactive_list.md)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Section
|
|
2
|
+
|
|
3
|
+
`Warb::Components::Section` is a component that represents a group of items of an interactive list message. It is used to display multiple options, grouped under a common title.
|
|
4
|
+
|
|
5
|
+
You don't need to create a section by yourself, as the `Warb::Components::ListAction` will handle that for you.
|
|
6
|
+
|
|
7
|
+
Here are its attributes:
|
|
8
|
+
| Attribute | Type | Required | Max Length | Description |
|
|
9
|
+
|-----------|--------|----------|--------------------------------------------------------|------------------------------------------------|
|
|
10
|
+
| title | String | Yes | 24 | The title of the section |
|
|
11
|
+
| rows | Array | Yes | 10 (combining all sections within the message) | The available options the user can choose from |
|
|
12
|
+
|
|
13
|
+
The section component instance also offers a `add_row` method to add a row to the section after its creation.
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
section = Warb::Components::Section.new(title: "Languages")
|
|
17
|
+
|
|
18
|
+
section.add_row(title: "Português")
|
|
19
|
+
section.add_row(title: "English")
|
|
20
|
+
|
|
21
|
+
# or using a block
|
|
22
|
+
section.add_row do |row|
|
|
23
|
+
row.title = "Español"
|
|
24
|
+
row.description = "Spanish language"
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Under the hood, it will create a [`Warb::Components::Row`](./row.md) for each item added to the section.
|
|
29
|
+
|
|
30
|
+
Check its complete usage in the [interactive list message documentation](../messages/interactive_list.md)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# URL
|
|
2
|
+
|
|
3
|
+
URL, `Warb::Components::URL`, is a component used in the `Contact` message type.
|
|
4
|
+
|
|
5
|
+
It represents an Organization with the following fields:
|
|
6
|
+
|
|
7
|
+
|Attribute | Type | Description | Required |
|
|
8
|
+
|----------|----------|--------------------------------------------------------------------------------------|----------|
|
|
9
|
+
| `url` | `String` | Website URL associated with the contact or their company | No |
|
|
10
|
+
| `type` | `String` | Type of website. For example, company, work, personal, Facebook Page, Instagram, etc | No |
|
|
11
|
+
|
|
12
|
+
When using the `Warb::Components::URL` component directly, you can set these attributes in the initializer or by assigning them directly to the instance.
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
url = Warb::Components::URL.new(
|
|
16
|
+
url: "https://example.com",
|
|
17
|
+
type: "company"
|
|
18
|
+
)
|
|
19
|
+
```
|
|
20
|
+
or
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
url = Warb::Components::URL.new
|
|
24
|
+
url.url = "https://example.com"
|
|
25
|
+
url.type = "company"
|
|
26
|
+
```
|
|
27
|
+
But it is better to use it in the context of a `Contact` message type, where you can build the URL using the `add_url` method:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
Warb.contact.dispatch(recipient_number) do |contact|
|
|
31
|
+
# Adding a URL using a block
|
|
32
|
+
contact.add_url do |url|
|
|
33
|
+
url.url = "https://example.com"
|
|
34
|
+
url.type = "company"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Adding a URL with parameters
|
|
38
|
+
contact.add_url(url: "https://personal.example.com", type: "personal")
|
|
39
|
+
end
|
|
40
|
+
```
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
```
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Messaging
|
|
2
|
+
|
|
3
|
+
To send any message, you can simply call `.dispatch` from either `Warb` module or any `Warb::Client` instance (please, check the [setup](./setup.md) page for more info on how to setup your client), using the corresponding wrapper for the [message type](#messages-types) you want to send:
|
|
4
|
+
|
|
5
|
+
## General Usage
|
|
6
|
+
|
|
7
|
+
Basically, you can send any type of message in the same way as bellow, changing the specific params:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
Warb.<type_of_message_you_want_to_send>.dispatch(<recipient_number>, reply_to: <message_id>, **specific_params)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Here is what you need to do:
|
|
14
|
+
- replace `<type_of_message_you_want_to_send>` with the appropriate wrapper based on the type of message you want to send
|
|
15
|
+
- replace `<recipient_number>` with the phone number of the intended recipient
|
|
16
|
+
- optionally replace `<message_id>` with the ID of a previous message in the conversation if you want to send a reply to that message
|
|
17
|
+
- if you're not replying to a message, you can either set the `reply_to` parameter to `nil` or omit it entirely — `nil` is the default value
|
|
18
|
+
|
|
19
|
+
Except for the `reply_to` parameter, you can pass any additional named parameters that are specific to the type of message you are sending.
|
|
20
|
+
|
|
21
|
+
Such parameters are described in the documentation for each message type, which you can find in the [Messages Types](#messages-types) section below.
|
|
22
|
+
|
|
23
|
+
For instance, to send a simple text message, you can:
|
|
24
|
+
|
|
25
|
+
**Note**: Assume `Warb.setup` has already been called and the variable `recipient_number` is defined
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
Warb.message.dispatch(recipient_number, text: "Hello from warb")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
You can also pass a block to `.dispatch` to build the message:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
Warb.message.dispatch(recipient_number) do |message|
|
|
35
|
+
message.text = "Hello from warb"
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
> In the example above, the variable `message` used in the block is an instance of `Warb::Resources::Text`
|
|
39
|
+
|
|
40
|
+
Under the hood, the `.dispatch` method will:
|
|
41
|
+
- create a message resource object, based on the used wrapper
|
|
42
|
+
- set its attributes based on the parameters you provide
|
|
43
|
+
- call its `build_payload` method to prepare the message for sending
|
|
44
|
+
- and finally, send the message
|
|
45
|
+
|
|
46
|
+
Inside the given block, you can modify modify the message before it's sent.
|
|
47
|
+
|
|
48
|
+
While using a block just to set a single attribute may seem excessive — it becomes quite useful when dealing with more complex message types, such as contact or interactive messages.
|
|
49
|
+
|
|
50
|
+
## Messages Types
|
|
51
|
+
|
|
52
|
+
Messages are sent using a helper dispatcher and its resource class. Here is a list of available message types, their corresponding resource classes, and dispatch wrappers:
|
|
53
|
+
|
|
54
|
+
| Message Type | Resource Class | Dispatch Wrapper | Documentation |
|
|
55
|
+
|--------------------------------|-----------------------------------------------|-------------------------------------|-------------------------------------------------------------------------------|
|
|
56
|
+
| Simple Dispatchers |
|
|
57
|
+
| Contact | `Warb::Resources::Contact` | `contact` | [Contact Message](./contact.md) |
|
|
58
|
+
| Interactive Call to Action URL | `Warb::Resources::InteractiveCalltoActionURL` | `interactive_call_to_action_url` | [Interactive Call to Action URL Message](./interactive_call_to_action_url.md) |
|
|
59
|
+
| Interactive List | `Warb::Resources::InteractiveList` | `interactive_list` | [Interactive List Message](./interactive_list.md) |
|
|
60
|
+
| Interactive Reply Button | `Warb::Resources::InteractiveReplyButton` | `interactive_reply_button` | [Interactive Reply Button Message](./interactive_reply_button.md) |
|
|
61
|
+
| Flow | `Warb::Resources::Flow` | `flow` | [Flow](./flow.md) |
|
|
62
|
+
| Location | `Warb::Resources::Location` | `location` | [Location Message](./location.md) |
|
|
63
|
+
| Location Request | `Warb::Resources::LocationRequest` | `location_request` | [Location Request Message](./location_request.md) |
|
|
64
|
+
| Reaction | `Warb::Resources::Reaction` | `reaction` | [Reaction Message](./reaction.md) |
|
|
65
|
+
| Text | `Warb::Resources::Text` | `message` | [Text Message](./text.md) |
|
|
66
|
+
| Media Dispatchers |
|
|
67
|
+
| Audio | `Warb::Resources::Audio` | `audio` | [Audio Message](./audio.md) |
|
|
68
|
+
| Document | `Warb::Resources::Document` | `document` | [Document Message](./document.md) |
|
|
69
|
+
| Image | `Warb::Resources::Image` | `image` | [Image Message](./image.md) |
|
|
70
|
+
| Sticker | `Warb::Resources::Sticker` | `sticker` | [Sticker Message](./sticker.md) |
|
|
71
|
+
| Video | `Warb::Resources::Video` | `video` | [Video Message](./video.md) |
|
|
72
|
+
| Special Dispatchers |
|
|
73
|
+
| Indicator | `Warb::Resources::Indicator` | `indicator` | [Indicator Message](./indicator.md) |
|
|
74
|
+
| Template | `Warb::Resources::Template` | `template` | [Template Message](./template.md) |
|
|
75
|
+
|
|
76
|
+
> Simple Dispatcher doesn't offer any additional functionality, they just send messages.
|
|
77
|
+
|
|
78
|
+
> Media Dispatchers, aside from sending messages, also provide methods to upload and download media files.
|
|
79
|
+
|
|
80
|
+
> Special Dispatchers are used for specific purposes, such as sending indicators, reactions or templates.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Audio
|
|
2
|
+
|
|
3
|
+
For starters, not all audio formats are supported by WhatsApp. Here is a table of supported audio formats:
|
|
4
|
+
|
|
5
|
+
| Audio Type | Extension | MIME Type | Max Size |
|
|
6
|
+
| ---------- | --------- | ------------------------------------------------------------------------------- | -------- |
|
|
7
|
+
| AAC | `.aac` | `audio/aac` | 16 MB |
|
|
8
|
+
| AMR | `.amr` | `audio/amr` | 16 MB |
|
|
9
|
+
| MP3 | `.mp3` | `audio/mpeg` | 16 MB |
|
|
10
|
+
| MP4 Audio | `.m4a` | `audio/mp4` | 16 MB |
|
|
11
|
+
| OGG Audio | `.ogg` | `audio/ogg` (OPUS codecs only; base `audio/ogg` not supported; mono input only) | 16 MB |
|
|
12
|
+
|
|
13
|
+
To send an audio message, use the `audio` dispatch wrapper as follow:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
recipient_number = "..."
|
|
17
|
+
Warb.audio.dispatch(recipient_number, ...)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**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.
|
|
21
|
+
|
|
22
|
+
You can send a simple audio message like this:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
Warb.audio.dispatch(recipient_number, link: "https://example.com/audio.ogg")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
You can also use the block building strategy to send an audio message:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
Warb.audio.dispatch(recipient_number) do |audio|
|
|
32
|
+
audio.link = "https://example.com/audio.ogg"
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
As seen above, the `link` field is used to set the audio file URL. This must be a direct link to an audio file hosted online.
|
|
37
|
+
|
|
38
|
+
Also, if using a link, the audio file must be publicly accessible, meaning it should not require any authentication or special permissions to access.
|
|
39
|
+
|
|
40
|
+
Aside from a link, you can also send an audio by using its ID. For that, you need to have the ID of the audio file already uploaded to WhatsApp.
|
|
41
|
+
|
|
42
|
+
#### Upload
|
|
43
|
+
|
|
44
|
+
Since our `audio` wrapper is a media dispatcher, it supports media related operations, like `upload` and `download` method.
|
|
45
|
+
|
|
46
|
+
So, we can use the `upload` method to upload an audio file and get its ID:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
audio_id = Warb.audio.upload(file_path: "path/to/audio.ogg", file_type: "audio/ogg")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
> `file_type` must be one of the supported audio types listed [above](#), and `file_path` is the path to the audio file you want to upload.
|
|
53
|
+
|
|
54
|
+
**Note**: At this point, it is not possible to retrieve the ID of an audio file that was previously uploaded, so keep the ID returned by the `upload` method for later use.
|
|
55
|
+
|
|
56
|
+
**Note**: Also, note that uploaded audio are stored in WhatsApp servers for 30 days, after that period they will be deleted and you won't be able to use the ID anymore.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
#### Sending with Audio ID
|
|
60
|
+
|
|
61
|
+
With the audio id in hands, you can use the it to send the audio message:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
Warb.audio.dispatch(recipient_number, media_id: audio_id)
|
|
65
|
+
```
|
|
66
|
+
You can also use the block building strategy to send an audio message with an ID:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
Warb.audio.dispatch(recipient_number) do |audio|
|
|
70
|
+
audio.media_id = audio_id
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**Note**: Either only one of the `link` or `media_id` fields can be set at a time.
|
|
75
|
+
|
|
76
|
+
#### Downloading Audio
|
|
77
|
+
|
|
78
|
+
You can also download an audio file using its ID:
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
Warb.audio.download(media_id: audio_id, file_path: "path/to/save/audio.ogg")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This will download the audio file to the specified path.
|
|
85
|
+
|
|
86
|
+
Under the hood, the `download` method will use `retrieve` method to get the download URL.
|
|
87
|
+
|
|
88
|
+
With the URL in hands, the `download` method will, in fact, download the file to the specified path.
|
|
89
|
+
|
|
90
|
+
The `retrieve` receives a single parameter, which is the `media_id` of the audio file you want to retrieve.
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
Warb.audio.retrieve(media_id: audio_id)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
And here is a sample response from the `retrieve` method:
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"url": "https://lookaside.fbsbx.com/...",
|
|
101
|
+
"mime_type": "audio/ogg",
|
|
102
|
+
"sha256": "4b4719...",
|
|
103
|
+
"file_size": 4799,
|
|
104
|
+
"id": "134183...",
|
|
105
|
+
"messaging_product": "whatsapp"
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### Deleting Audio
|
|
110
|
+
|
|
111
|
+
You can delete an audio file using its ID:
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
Warb.audio.delete(audio_id)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
This will delete the audio file from WhatsApp servers and return `true` on success.
|
|
118
|
+
|
|
119
|
+
This is useful if you want to free up space or if you no longer need the audio file.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Contact
|
|
2
|
+
|
|
3
|
+
Send a contact using the `contact` wrapper:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
recipient_number = "..."
|
|
7
|
+
Warb.contact.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 contact like this:
|
|
13
|
+
```ruby
|
|
14
|
+
name = Warb::Components::Name.new formatted_name: "Full Name", preffix: "Mr"
|
|
15
|
+
|
|
16
|
+
Warb.contact.dispatch(recipient_number, name: name)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
But it may be tiring writing this all, so the next example bellow shows a different approach to send the same message:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
Warb.contact.dispatch(recipient_number) do |contact|
|
|
23
|
+
contact.build_name formatted_name: "Full Name", prefix: "Mr"
|
|
24
|
+
end
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This block building strategy can be used to build even larger contacts, for exemple:
|
|
28
|
+
```ruby
|
|
29
|
+
Warb.contact.dispatch(recipient_number) do |contact|
|
|
30
|
+
contact.build_name formatted_name: "Full Name", prefix: "Mr"
|
|
31
|
+
|
|
32
|
+
country = "Country"
|
|
33
|
+
country_code = "Country Code"
|
|
34
|
+
|
|
35
|
+
# you can set everything inside the block
|
|
36
|
+
contact.add_address do |address|
|
|
37
|
+
address.street = "First Street"
|
|
38
|
+
address.city = "First City"
|
|
39
|
+
address.state = "First State"
|
|
40
|
+
address.zip = "First ZIP"
|
|
41
|
+
address.country = country
|
|
42
|
+
address.country_code = country_code
|
|
43
|
+
address.type = "HOME"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# you can, as well, pass some values to the add_address method
|
|
47
|
+
second_address = contact.add_address(type: "WORK") do |address|
|
|
48
|
+
address.street = "Second Street"
|
|
49
|
+
address.city = "Second City"
|
|
50
|
+
address.state = "Second State"
|
|
51
|
+
address.zip = "Second ZIP"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# and also, assign values using the returned object
|
|
55
|
+
second_address.country = country
|
|
56
|
+
second_address.country_code = country_code
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
There are more fields which can be set in the contact type message. Here is a more complete example:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
Warb.contact.dispatch(recipient_number) do |contact|
|
|
64
|
+
# name (a Warb::Components::Name instance)
|
|
65
|
+
contact.build_name do |name|
|
|
66
|
+
name.formatted_name = "Full Name"
|
|
67
|
+
name.prefix = "Mr"
|
|
68
|
+
name.suffix = "IV"
|
|
69
|
+
name.first_name = "Full"
|
|
70
|
+
name.last_name = "Name"
|
|
71
|
+
name.middle_name = "Middle"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# string in the format YYYY-MM-DD
|
|
75
|
+
contact.birthday = "1980-06-06"
|
|
76
|
+
|
|
77
|
+
# addresses (a list of Warb::Components::Address)
|
|
78
|
+
country = "Country"
|
|
79
|
+
country_code = "Country Code"
|
|
80
|
+
|
|
81
|
+
contact.add_address do |address|
|
|
82
|
+
address.street = "First Street"
|
|
83
|
+
address.city = "First City"
|
|
84
|
+
address.state = "First State"
|
|
85
|
+
address.zip = "First ZIP"
|
|
86
|
+
address.country = country
|
|
87
|
+
address.country_code = country_code
|
|
88
|
+
address.type = "HOME"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
contact.add_address do |address|
|
|
92
|
+
address.street = "Second Street"
|
|
93
|
+
address.city = "Second City"
|
|
94
|
+
address.state = "Second State"
|
|
95
|
+
address.zip = "Second ZIP"
|
|
96
|
+
address.country = country
|
|
97
|
+
address.country_code = country_code
|
|
98
|
+
address.type = "WORK"
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# emails (a list of Warb::Components::Email)
|
|
102
|
+
contact.add_email(email: "personal@email.com", type: "HOME")
|
|
103
|
+
contact.add_email(email: "work@email.com", type: "WORK")
|
|
104
|
+
|
|
105
|
+
# urls (a list of Warb::Components::URL)
|
|
106
|
+
contact.add_url(type: "HOME", url: "https://primary.home.example.com")
|
|
107
|
+
contact.add_url(type: "HOME", url: "https://secondary.home.example.com")
|
|
108
|
+
contact.add_url(type: "WORK", url: "https://work.example.com")
|
|
109
|
+
|
|
110
|
+
# phones (a list of Warb::Components::Phone)
|
|
111
|
+
contact.add_phone(type: "HOME", phone: "...")
|
|
112
|
+
contact.add_phone(type: "HOME", phone: "...", wa_id: "...")
|
|
113
|
+
contact.add_phone(type: "WORK", phone: "...")
|
|
114
|
+
|
|
115
|
+
# org (a Warb::Components::Org instance)
|
|
116
|
+
contact.build_org do |org|
|
|
117
|
+
org.company = "Inc"
|
|
118
|
+
org.title = "Manager"
|
|
119
|
+
org.department = "IT"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Here are the specific params which can be passed to the `dispatch` method using the `contact` wrapper:
|
|
125
|
+
|
|
126
|
+
| Field | Type / Description | Required | Documentation |
|
|
127
|
+
| ----------- | ------------------------------------------------ | -------- | ----------------------------------- |
|
|
128
|
+
| `name` | A `Warb::Components::Name` instance | YES | [Name](../components/name.md) |
|
|
129
|
+
| `birthday` | A `String` in the format `YYYY-MM-DD`. | NO | — |
|
|
130
|
+
| `org` | A `Warb::Components::Org` instance | NO | [Org](../components/org.md) |
|
|
131
|
+
| `addresses` | A list of `Warb::Components::Address` instances | NO | [Address](../components/address.md) |
|
|
132
|
+
| `emails` | A list of `Warb::Components::Email` instances | NO | [Email](../components/email.md) |
|
|
133
|
+
| `urls` | A list of `Warb::Components::URL` instances | NO | [URL](../components/url.md) |
|
|
134
|
+
| `phones` | A list of `Warb::Components::Phone` instances | NO | [Phone](../components/phone.md) |
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Document
|
|
2
|
+
|
|
3
|
+
For starters, not all documents are supported by WhatsApp. Here is a table of supported documents:
|
|
4
|
+
|
|
5
|
+
| Document Type | Extension | MIME Type | Max Size |
|
|
6
|
+
|-----------------------|-----------|-----------------------------------------------------------------------------|----------|
|
|
7
|
+
| Text | `.txt` | `text/plain` | 100 MB |
|
|
8
|
+
| Microsoft Excel | `.xls` | `application/vnd.ms-excel` | 100 MB |
|
|
9
|
+
| Microsoft Excel | `.xlsx` | `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` | 100 MB |
|
|
10
|
+
| Microsoft Word | `.doc` | `application/msword` | 100 MB |
|
|
11
|
+
| Microsoft Word | `.docx` | `application/vnd.openxmlformats-officedocument.wordprocessingml.document` | 100 MB |
|
|
12
|
+
| Microsoft PowerPoint | `.ppt` | `application/vnd.ms-powerpoint` | 100 MB |
|
|
13
|
+
| Microsoft PowerPoint | `.pptx` | `application/vnd.openxmlformats-officedocument.presentationml.presentation` | 100 MB |
|
|
14
|
+
| PDF | `.pdf` | `application/pdf` | 100 MB |
|
|
15
|
+
|
|
16
|
+
To send a document message, use the `document` dispatch wrapper as follows:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
recipient_number = "..."
|
|
20
|
+
Warb.document.dispatch(recipient_number, ...)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**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.
|
|
24
|
+
|
|
25
|
+
You can send a simple document message like this:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
Warb.document.dispatch(recipient_number, link: "https://example.com/document.pdf")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
You can also use the block building strategy to send a document message:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
Warb.document.dispatch(recipient_number) do |document|
|
|
35
|
+
document.link = "https://example.com/document.pdf"
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
As seen above, the `link` field is used to set the document file URL. This must be a direct link to a file hosted online.
|
|
40
|
+
|
|
41
|
+
Also, if using a link, the document file must be publicly accessible, meaning it should not require any authentication or special permissions to access.
|
|
42
|
+
|
|
43
|
+
Aside from a link, you can also send a document by using its ID. For that, you need to have the ID of the document file already uploaded to WhatsApp.
|
|
44
|
+
|
|
45
|
+
#### Upload
|
|
46
|
+
|
|
47
|
+
Since our `document` wrapper is a media dispatcher, it supports media-related operations, like `upload` and `download` methods.
|
|
48
|
+
|
|
49
|
+
So, we can use the `upload` method to upload a document file and get its ID:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
document_id = Warb.document.upload(file_path: "path/to/document.pdf", file_type: "application/pdf")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
> `file_type` must be one of the supported document types listed [above](#), and `file_path` is the path to the document file you want to upload.
|
|
56
|
+
|
|
57
|
+
**Note**: At this point, it is not possible to retrieve the ID of a document file that was previously uploaded, so keep the ID returned by the `upload` method for later use.
|
|
58
|
+
|
|
59
|
+
**Note**: Also, note that uploaded documents 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.
|
|
60
|
+
|
|
61
|
+
#### Sending with Document ID
|
|
62
|
+
|
|
63
|
+
With the document ID in hand, you can use it to send the document message:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
Warb.document.dispatch(recipient_number, media_id: document_id)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
You can also use the block building strategy to send a document message with an ID:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
Warb.document.dispatch(recipient_number) do |document|
|
|
73
|
+
document.media_id = document_id
|
|
74
|
+
end
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Note**: Either only one of the `link` or `media_id` fields can be set at a time.
|
|
78
|
+
|
|
79
|
+
#### Downloading Document
|
|
80
|
+
|
|
81
|
+
You can also download a document file using its ID:
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
Warb.document.download(media_id: document_id, file_path: "path/to/save/document.pdf")
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
This will download the document file to the specified path.
|
|
88
|
+
|
|
89
|
+
Under the hood, the `download` method will use the `retrieve` method to get the download URL.
|
|
90
|
+
|
|
91
|
+
With the URL in hand, the `download` method will, in fact, download the file to the specified path.
|
|
92
|
+
|
|
93
|
+
The `retrieve` method receives a single parameter, which is the `media_id` of the document file you want to retrieve.
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
Warb.document.retrieve(media_id: document_id)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
And here is a sample response from the `retrieve` method:
|
|
100
|
+
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"url": "https://lookaside.fbsbx.com/...",
|
|
104
|
+
"mime_type": "application/pdf",
|
|
105
|
+
"sha256": "4b4719...",
|
|
106
|
+
"file_size": 1048576,
|
|
107
|
+
"id": "134183...",
|
|
108
|
+
"messaging_product": "whatsapp"
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### Deleting Document
|
|
113
|
+
|
|
114
|
+
You can delete a document file using its ID:
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
Warb.document.delete(document_id)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
This will delete the document file from WhatsApp servers and return `true` on success.
|
|
121
|
+
|
|
122
|
+
This is useful if you want to free up space or if you no longer need the document file.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Flow
|
|
2
|
+
|
|
3
|
+
Flow is a special type of message. We can summarize it simply as a form.
|
|
4
|
+
|
|
5
|
+
At this point of writing, this gem only supports sending existing flows, and it can only send flows which are in draft.
|
|
6
|
+
|
|
7
|
+
To send flows, you can do as following:
|
|
8
|
+
```ruby
|
|
9
|
+
Warb.flow.dispatch(recipient_number, flow_id: "flow_id", screen: "screen")
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`flow_id` must be the ID of the flow and `screen` must be the ID of the first screen of the flow.
|