termii_rust/async_impl/rest/switch/messaging.rs
1//! Send messages to customers across termii channels.
2
3use std::sync::Arc;
4
5use crate::{
6 async_impl::http::client,
7 common::{
8 errors,
9 switch::messaging::{
10 MessageBulkRequest, MessageBulkResponse, MessageRequest, MessageResponse,
11 },
12 },
13};
14
15#[derive(Debug)]
16pub struct Messaging<'a> {
17 api_key: &'a str,
18 client: Arc<client::HttpClient>,
19}
20
21impl<'a> Messaging<'a> {
22 pub(crate) fn new(api_key: &'a str, client: Arc<client::HttpClient>) -> Messaging<'a> {
23 Messaging { api_key, client }
24 }
25
26 /// Send a message to a recipient.
27 ///
28 /// ## Examples
29 ///
30 /// ```rust
31 /// use termii_rust::{
32 /// async_impl::rest::termii,
33 /// common::switch::messaging::{Channel, MessageRequest, MessageType},
34 /// };
35 ///
36 /// let client = termii::Termii::new("Your API key");
37 ///
38 /// let message_payload = MessageRequest::new(
39 /// "234XXXXXXXXXX".to_string(),
40 /// "Your org sender id".to_string(),
41 /// "Your message".to_string(),
42 /// MessageType::Plain,
43 /// Channel::Generic,
44 /// );
45 ///
46 /// let message_response = client.switch.messaging.send(message_payload).await.unwrap();
47 ///
48 /// println!("{:?}", message_response);
49 /// ```
50 pub async fn send(
51 &self,
52 mut message: MessageRequest,
53 ) -> Result<MessageResponse, errors::HttpError> {
54 message.set_api_key(self.api_key);
55
56 let response = self
57 .client
58 .post("sms/send", None, None, Some(message))
59 .await?;
60
61 let message_response = response_or_error_text_async!(response, MessageResponse);
62
63 Ok(message_response)
64 }
65
66 /// Send a message to multiple recipients.
67 ///
68 /// ## Examples
69 ///
70 /// ```rust
71 /// use termii_rust::{
72 /// async_impl::rest::termii,
73 /// common::switch::messaging::{
74 /// Channel, MessageBulkRequest, MessageBulkResponse, MessageType,
75 /// },
76 /// };
77 ///
78 /// let client = termii::Termii::new("Your API key");
79 ///
80 /// let message_bulk_payload = MessageBulkRequest::new(
81 /// vec!["234XXXXXXXXXX".to_string(), "234XXXXXXXXXX".to_string()],
82 /// "Your org sender id".to_string(),
83 /// "Your message".to_string(),
84 /// MessageType::Plain,
85 /// Channel::Generic,
86 /// );
87 ///
88 /// let message_bulk_response = client
89 /// .switch
90 /// .messaging
91 /// .send_bulk(message_bulk_payload)
92 /// .await
93 /// .unwrap();
94 ///
95 /// println!("{:?}", message_bulk_response);
96 /// ```
97 pub async fn send_bulk(
98 &self,
99 mut message: MessageBulkRequest,
100 ) -> Result<MessageBulkResponse, errors::HttpError> {
101 message.set_api_key(self.api_key);
102
103 let response = self
104 .client
105 .post("sms/send/bulk", None, None, Some(message))
106 .await?;
107
108 let message_response = response_or_error_text_async!(response, MessageBulkResponse);
109
110 Ok(message_response)
111 }
112}