front_api/
messages.rs

1use anyhow::Result;
2
3use crate::Client;
4#[derive(Clone, Debug)]
5pub struct Messages {
6    pub client: Client,
7}
8
9impl Messages {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        Self { client }
13    }
14
15    #[doc = "Reply to conversation\n\nReply to a conversation by sending a message and appending \
16             it to the conversation.\n\n**Parameters:**\n\n- `conversation_id: &'astr`: The \
17             conversation ID (required)\n\n```rust,no_run\nasync fn \
18             example_messages_reply_to_conversation() -> anyhow::Result<()> {\n    let client = \
19             front_api::Client::new_from_env();\n    let result: front_api::types::MessageResponse \
20             = client\n        .messages()\n        .reply_to_conversation(\n            \
21             \"some-string\",\n            \
22             &serde_json::Value::String(\"some-string\".to_string()),\n        )\n        \
23             .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
24    #[tracing::instrument]
25    pub async fn reply_to_conversation<'a>(
26        &'a self,
27        conversation_id: &'a str,
28        body: &crate::types::OutboundReplyMessage,
29    ) -> Result<crate::types::MessageResponse, crate::types::error::Error> {
30        let mut req = self.client.client.request(
31            http::Method::POST,
32            &format!(
33                "{}/{}",
34                self.client.base_url,
35                "conversations/{conversation_id}/messages"
36                    .replace("{conversation_id}", conversation_id)
37            ),
38        );
39        req = req.bearer_auth(&self.client.token);
40        req = req.json(body);
41        let resp = req.send().await?;
42        let status = resp.status();
43        if status.is_success() {
44            let text = resp.text().await.unwrap_or_default();
45            serde_json::from_str(&text).map_err(|err| {
46                crate::types::error::Error::from_serde_error(
47                    format_serde_error::SerdeError::new(text.to_string(), err),
48                    status,
49                )
50            })
51        } else {
52            Err(crate::types::error::Error::UnexpectedResponse(resp))
53        }
54    }
55
56    #[doc = "Get message\n\nFetch a message.\n\n**Parameters:**\n\n- `message_id: &'astr`: The \
57             message ID (required)\n\n```rust,no_run\nasync fn example_messages_get() -> \
58             anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let \
59             result: front_api::types::MessageResponse = \
60             client.messages().get(\"some-string\").await?;\n    println!(\"{:?}\", result);\n    \
61             Ok(())\n}\n```"]
62    #[tracing::instrument]
63    pub async fn get<'a>(
64        &'a self,
65        message_id: &'a str,
66    ) -> Result<crate::types::MessageResponse, crate::types::error::Error> {
67        let mut req = self.client.client.request(
68            http::Method::GET,
69            &format!(
70                "{}/{}",
71                self.client.base_url,
72                "messages/{message_id}".replace("{message_id}", message_id)
73            ),
74        );
75        req = req.bearer_auth(&self.client.token);
76        let resp = req.send().await?;
77        let status = resp.status();
78        if status.is_success() {
79            let text = resp.text().await.unwrap_or_default();
80            serde_json::from_str(&text).map_err(|err| {
81                crate::types::error::Error::from_serde_error(
82                    format_serde_error::SerdeError::new(text.to_string(), err),
83                    status,
84                )
85            })
86        } else {
87            Err(crate::types::error::Error::UnexpectedResponse(resp))
88        }
89    }
90
91    #[doc = "Get message seen status\n\nGet the seen receipts for the given message. If no seen-by \
92             information is available, there will be a single entry for the first time the message \
93             was seen by any recipient. If seen-by information is available, there will be an \
94             entry for each recipient who has seen the message.\n\n**Parameters:**\n\n- \
95             `message_id: &'astr`: The message ID (required)\n\n```rust,no_run\nasync fn \
96             example_messages_get_seen_status() -> anyhow::Result<()> {\n    let client = \
97             front_api::Client::new_from_env();\n    let result: \
98             front_api::types::GetMessageSeenStatusResponse =\n        \
99             client.messages().get_seen_status(\"some-string\").await?;\n    println!(\"{:?}\", \
100             result);\n    Ok(())\n}\n```"]
101    #[tracing::instrument]
102    pub async fn get_seen_status<'a>(
103        &'a self,
104        message_id: &'a str,
105    ) -> Result<crate::types::GetMessageSeenStatusResponse, crate::types::error::Error> {
106        let mut req = self.client.client.request(
107            http::Method::GET,
108            &format!(
109                "{}/{}",
110                self.client.base_url,
111                "messages/{message_id}/seen".replace("{message_id}", message_id)
112            ),
113        );
114        req = req.bearer_auth(&self.client.token);
115        let resp = req.send().await?;
116        let status = resp.status();
117        if status.is_success() {
118            let text = resp.text().await.unwrap_or_default();
119            serde_json::from_str(&text).map_err(|err| {
120                crate::types::error::Error::from_serde_error(
121                    format_serde_error::SerdeError::new(text.to_string(), err),
122                    status,
123                )
124            })
125        } else {
126            Err(crate::types::error::Error::UnexpectedResponse(resp))
127        }
128    }
129
130    #[doc = "Mark message seen\n\nMark an outbound message from Front as seen. Note, the message \
131             seen route should only be called in response to an actual end-user's message-seen \
132             action. In accordance with this behavior, the route is rate limited to 10 requests \
133             per hour.\n\n**Parameters:**\n\n- `message_id: &'astr`: The message ID \
134             (required)\n\n```rust,no_run\nasync fn example_messages_mark_seen() -> \
135             anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    \
136             client\n        .messages()\n        .mark_seen(\"some-string\", \
137             &front_api::types::MarkMessageSeenRequestBody {})\n        .await?;\n    \
138             Ok(())\n}\n```"]
139    #[tracing::instrument]
140    pub async fn mark_seen<'a>(
141        &'a self,
142        message_id: &'a str,
143        body: &crate::types::MarkMessageSeenRequestBody,
144    ) -> Result<(), crate::types::error::Error> {
145        let mut req = self.client.client.request(
146            http::Method::POST,
147            &format!(
148                "{}/{}",
149                self.client.base_url,
150                "messages/{message_id}/seen".replace("{message_id}", message_id)
151            ),
152        );
153        req = req.bearer_auth(&self.client.token);
154        req = req.json(body);
155        let resp = req.send().await?;
156        let status = resp.status();
157        if status.is_success() {
158            Ok(())
159        } else {
160            Err(crate::types::error::Error::UnexpectedResponse(resp))
161        }
162    }
163
164    #[doc = "Create conversation\n\nSend a new message from a channel.\n\n**Parameters:**\n\n- \
165             `channel_id: &'astr`: The sending channel ID (required)\n\n```rust,no_run\nasync fn \
166             example_messages_create_conversation() -> anyhow::Result<()> {\n    let client = \
167             front_api::Client::new_from_env();\n    let result: front_api::types::MessageResponse \
168             = client\n        .messages()\n        .create_conversation(\n            \
169             \"some-string\",\n            \
170             &serde_json::Value::String(\"some-string\".to_string()),\n        )\n        \
171             .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
172    #[tracing::instrument]
173    pub async fn create_conversation<'a>(
174        &'a self,
175        channel_id: &'a str,
176        body: &crate::types::OutboundMessage,
177    ) -> Result<crate::types::MessageResponse, crate::types::error::Error> {
178        let mut req = self.client.client.request(
179            http::Method::POST,
180            &format!(
181                "{}/{}",
182                self.client.base_url,
183                "channels/{channel_id}/messages".replace("{channel_id}", channel_id)
184            ),
185        );
186        req = req.bearer_auth(&self.client.token);
187        req = req.json(body);
188        let resp = req.send().await?;
189        let status = resp.status();
190        if status.is_success() {
191            let text = resp.text().await.unwrap_or_default();
192            serde_json::from_str(&text).map_err(|err| {
193                crate::types::error::Error::from_serde_error(
194                    format_serde_error::SerdeError::new(text.to_string(), err),
195                    status,
196                )
197            })
198        } else {
199            Err(crate::types::error::Error::UnexpectedResponse(resp))
200        }
201    }
202
203    #[doc = "Receive custom messages\n\nReceive a custom message in Front. This endpoint is \
204             available for custom channels **ONLY**.\n\n**Parameters:**\n\n- `channel_id: &'astr`: \
205             The channel ID (required)\n\n```rust,no_run\nasync fn \
206             example_messages_receive_custom() -> anyhow::Result<()> {\n    let client = \
207             front_api::Client::new_from_env();\n    let result: \
208             front_api::types::ReceiveCustomMessageResponse = client\n        .messages()\n        \
209             .receive_custom(\n            \"some-string\",\n            \
210             &serde_json::Value::String(\"some-string\".to_string()),\n        )\n        \
211             .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
212    #[tracing::instrument]
213    pub async fn receive_custom<'a>(
214        &'a self,
215        channel_id: &'a str,
216        body: &crate::types::CustomMessage,
217    ) -> Result<crate::types::ReceiveCustomMessageResponse, crate::types::error::Error> {
218        let mut req = self.client.client.request(
219            http::Method::POST,
220            &format!(
221                "{}/{}",
222                self.client.base_url,
223                "channels/{channel_id}/incoming_messages".replace("{channel_id}", channel_id)
224            ),
225        );
226        req = req.bearer_auth(&self.client.token);
227        req = req.json(body);
228        let resp = req.send().await?;
229        let status = resp.status();
230        if status.is_success() {
231            let text = resp.text().await.unwrap_or_default();
232            serde_json::from_str(&text).map_err(|err| {
233                crate::types::error::Error::from_serde_error(
234                    format_serde_error::SerdeError::new(text.to_string(), err),
235                    status,
236                )
237            })
238        } else {
239            Err(crate::types::error::Error::UnexpectedResponse(resp))
240        }
241    }
242
243    #[doc = "Import message\n\nImport a new message in an inbox.\n\n**Parameters:**\n\n- \
244             `inbox_id: &'astr`: The Inbox ID (required)\n\n```rust,no_run\nasync fn \
245             example_messages_import_inbox() -> anyhow::Result<()> {\n    let client = \
246             front_api::Client::new_from_env();\n    let result: \
247             front_api::types::ImportInboxMessageResponse = client\n        .messages()\n        \
248             .import_inbox(\n            \"some-string\",\n            \
249             &serde_json::Value::String(\"some-string\".to_string()),\n        )\n        \
250             .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
251    #[tracing::instrument]
252    pub async fn import_inbox<'a>(
253        &'a self,
254        inbox_id: &'a str,
255        body: &crate::types::ImportMessage,
256    ) -> Result<crate::types::ImportInboxMessageResponse, crate::types::error::Error> {
257        let mut req = self.client.client.request(
258            http::Method::POST,
259            &format!(
260                "{}/{}",
261                self.client.base_url,
262                "inboxes/{inbox_id}/imported_messages".replace("{inbox_id}", inbox_id)
263            ),
264        );
265        req = req.bearer_auth(&self.client.token);
266        req = req.json(body);
267        let resp = req.send().await?;
268        let status = resp.status();
269        if status.is_success() {
270            let text = resp.text().await.unwrap_or_default();
271            serde_json::from_str(&text).map_err(|err| {
272                crate::types::error::Error::from_serde_error(
273                    format_serde_error::SerdeError::new(text.to_string(), err),
274                    status,
275                )
276            })
277        } else {
278            Err(crate::types::error::Error::UnexpectedResponse(resp))
279        }
280    }
281}