whatsapp_cloud_api/models/
interactive_message.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Deserialize, Serialize, Debug, Clone)]
4pub struct Interactive {
5 action: InteractiveAction,
6 body: Option<InteractiveBody>,
7
8 #[serde(rename = "type")]
9 interactive_type: InteractiveType,
10}
11
12impl Interactive {
13 pub fn for_button(buttons: Vec<InteractiveActionButton>, body_text: &str) -> Self {
14 Self {
15 action: InteractiveAction::new_buttons(buttons),
16 interactive_type: InteractiveType::Button,
17 body: Some(InteractiveBody::new(body_text)),
18 }
19 }
20
21 pub fn for_list(
22 button: &str,
23 sections: Vec<InteractiveActionSection>,
24 body_text: &str,
25 ) -> Self {
26 Self {
27 action: InteractiveAction::new_list(button, sections),
28 interactive_type: InteractiveType::List,
29 body: Some(InteractiveBody::new(body_text)),
30 }
31 }
32}
33
34#[derive(Deserialize, Serialize, Debug, Clone)]
35pub struct InteractiveAction {
36 button: Option<String>,
37 buttons: Option<Vec<InteractiveActionButton>>,
38 catalog_id: Option<String>,
39 product_retailer_id: Option<String>,
40 sections: Option<Vec<InteractiveActionSection>>,
41 }
43
44impl InteractiveAction {
45 pub fn new_buttons(buttons: Vec<InteractiveActionButton>) -> Self {
46 InteractiveAction {
47 button: None,
48 buttons: Some(buttons),
49 catalog_id: None,
50 product_retailer_id: None,
51 sections: None,
52 }
53 }
54
55 pub fn new_list(button: &str, sections: Vec<InteractiveActionSection>) -> Self {
56 InteractiveAction {
57 button: Some(button.into()),
58 buttons: None,
59 catalog_id: None,
60 product_retailer_id: None,
61 sections: Some(sections),
62 }
63 }
64}
65
66#[derive(Deserialize, Serialize, Debug, Clone)]
67pub struct InteractiveBody {
68 text: String,
69}
70
71impl InteractiveBody {
72 fn new(text: &str) -> Self {
73 Self { text: text.into() }
74 }
75}
76
77#[derive(Deserialize, Serialize, Debug, Clone)]
78pub struct InteractiveActionButton {
79 #[serde(rename = "type")]
80 action_type: InteractiveActionButtonType,
81
82 reply: InteractiveActionButtonReply,
83}
84
85#[derive(Deserialize, Serialize, Debug, Clone)]
86pub struct InteractiveActionButtonReply {
87 title: String,
88 id: String,
89}
90
91impl InteractiveActionButton {
92 pub fn new(title: &str, id: &str) -> Self {
93 Self {
94 action_type: InteractiveActionButtonType::Reply,
95 reply: InteractiveActionButtonReply {
96 title: title.into(),
97 id: id.into(),
98 },
99 }
100 }
101}
102
103#[derive(Deserialize, Serialize, Debug, Clone)]
104#[serde(rename_all = "snake_case")]
105pub enum InteractiveActionButtonType {
106 Reply,
107}
108
109#[derive(Deserialize, Serialize, Debug, Clone)]
110#[serde(rename_all = "snake_case")]
111pub enum InteractiveType {
112 Button,
113 CatalogMessage,
114 List,
115 Product,
116 ProductList,
117 Flow,
118}
119
120#[derive(Deserialize, Serialize, Debug, Clone)]
121pub struct InteractiveActionSection {
122 rows: Vec<InteractiveActionSectionRow>,
124 title: Option<String>,
125}
126
127impl InteractiveActionSection {
128 pub fn new(rows: Vec<InteractiveActionSectionRow>) -> Self {
129 Self { rows, title: None }
130 }
131
132 pub fn with_title(rows: Vec<InteractiveActionSectionRow>, title: &str) -> Self {
133 Self {
134 rows,
135 title: Some(title.into()),
136 }
137 }
138}
139
140#[derive(Deserialize, Serialize, Debug, Clone)]
141pub struct InteractiveActionSectionRow {
142 id: String,
143 title: String,
144 description: Option<String>,
145}
146
147impl InteractiveActionSectionRow {
148 pub fn new(id: &str, title: &str) -> Self {
149 Self {
150 id: id.into(),
151 title: title.into(),
152 description: None,
153 }
154 }
155
156 pub fn with_description(id: &str, title: &str, description: &str) -> Self {
157 Self {
158 id: id.into(),
159 title: title.into(),
160 description: Some(description.into()),
161 }
162 }
163}