1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//! This module contains the `Button` enum and the `ButtonModel` struct.
//!
//! # Examples
//!
//! Creating a new button:
//!
//! ```rust
//! use russenger::prelude::*;
//!
//! async fn index(res: Res, req: Req) -> Result<()> {
//! let payload = Payload::new("/hello_world", Some(Data::new("HelloWorld")));
//! let buttons = vec![
//! Button::Postback { title: "Click me", payload },
//! ];
//! let button_model = ButtonModel::new("sender_id", "Hello, user1!", buttons);
//! res.send(button_model).await?;
//!
//! Ok(())
//! }
//!
//! async fn hello_world(res: Res, req: Req) -> Result<()> {
//! let value: String = req.data.get_value()?;
//! res.send(TextModel::new(&req.user, &value)).await?;
//!
//! Ok(())
//! }
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//! App::init().await?
//! .attach(router![
//! ("/", index),
//! ("/hello_world", hello_world)])
//! .launch()
//! .await?;
//! Ok(())
//! }
//! ```
use Serialize;
use json;
use Value;
use ;
/// `Button` is an enum that represents different types of buttons that can be used in a Messenger conversation.
///
/// # Variants
///
/// * `AccountUnlink` - Represents an account unlink button.
/// * `AccountLink { url: String }` - Represents an account link button. The `url` field is the URL to be opened when the button is clicked.
/// * `WebUrl { title: String, url: String }` - Represents a web URL button. The `title` field is the title of the button, and the `url` field is the URL to be opened when the button is clicked.
/// * `Postback { title: String, payload: Payload }` - Represents a postback button. The `title` field is the title of the button, and the `payload` field is the payload to be sent back to the server when the button is clicked.
/// * `PhoneNumber { title: String, payload: Payload }` - Represents a phone number button. The `title` field is the title of the button, and the `payload` field is the phone number to be dialed when the button is clicked.
///
/// # Examples
///
/// Creating a `Button` and converting it to a `Value`:
///
/// ```rust
/// use russenger::response_models::data::Data;
/// use russenger::response_models::payload::Payload;
/// use russenger::response_models::button::Button;
// Creating an AccountUnlink button
/// let account_unlink_button = Button::AccountUnlink;
///
/// // Creating an AccountLink button
/// let account_link_button = Button::AccountLink {
/// url: "https://www.facebook.com/your_account_page",
/// };
///
/// // Creating a WebUrl button
/// let web_url_button = Button::WebUrl {
/// title: "Visit Website",
/// url: "https://example.com".to_owned(),
/// };
///
/// // Creating a Postback button
/// let postback_button = Button::Postback {
/// title: "Click me",
/// payload: Payload::new(HelloWorld, Some(Data::new("HelloWorld", None))),
/// };
///
/// // Creating a PhoneNumber button
/// let phone_number_button = Button::PhoneNumber {
/// title: "Call me",
/// payload: Payload::new(HelloWorld, Some(Data::new("<PhoneNumber>", None))),
/// };
///
/// use russenger::prelude::*;
///
/// async fn hello_world(res: Res, req: Req) -> Result<()> {
/// let payload: String = req.data.get_value()?;
/// res.send(TextModel::new(&req.user, &payload)).await?;
///
/// Ok(())
/// }
/// ```
/// The `ButtonModel` struct represents a button template message.
///
/// The button template sends a text message with up to three buttons attached. This template gives the message recipient different options to choose from, such as predefined answers to questions or actions to take.
///
/// # Fields
///
/// * `recipient`: The recipient of the message. This is a `Recipient` struct that contains the Facebook user ID of the recipient.
/// * `message`: The message to be sent. This is a JSON value that contains the button template.
///
/// # Methods
///
/// * `new`: This method creates a new `ButtonModel`. It takes a sender ID, a text, and a vector of buttons as arguments.
///
/// # Examples
///
/// Creating a new `ButtonModel` and sending it using the `res.send()` method:
///
/// ```rust
/// use russenger::prelude::*;
///
/// async fn index(res: Res, req: Req) {
/// let buttons = vec![
/// Button::WebUrl {title: "Click Me".to_owned(), url: "https://link.test.com".to_owned()},
/// // More Button ...
/// ];
/// res.send(ButtonModel::new(&req.user, "Option", buttons)).await?;
///
/// Ok(())
/// }
/// ```
///
/// # References
///
/// * [Facebook Button Template Documentation](https://developers.facebook.com/docs/messenger-platform/send-messages/template/button)