|
| 1 | +//! Models for calling WhatsApp endpoints. |
| 2 | +
|
| 3 | +use serde_derive::{Deserialize, Serialize}; |
| 4 | +use validator::Validate; |
| 5 | + |
| 6 | +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)] |
| 7 | +#[serde(rename_all = "camelCase")] |
| 8 | +pub struct TextMessageContent { |
| 9 | + /// Content of the message being sent. |
| 10 | + #[validate(length(min = 1, max = 4096))] |
| 11 | + pub text: String, |
| 12 | + |
| 13 | + /// Allows for URL preview from within the message. If set to true, the message content must |
| 14 | + /// contain a URL starting with https:// or http://. Defaults to false. |
| 15 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 16 | + pub preview_url: Option<bool>, |
| 17 | +} |
| 18 | + |
| 19 | +impl TextMessageContent { |
| 20 | + pub fn new(text: String) -> Self { |
| 21 | + TextMessageContent { |
| 22 | + text, |
| 23 | + preview_url: None, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Validate)] |
| 29 | +#[serde(rename_all = "camelCase")] |
| 30 | +pub struct SendTextRequestBody { |
| 31 | + /// Registered WhatsApp sender number. Must be in international format and comply with |
| 32 | + /// WhatsApp's requirements. |
| 33 | + #[validate(length(min = 1, max = 24))] |
| 34 | + pub from: String, |
| 35 | + |
| 36 | + /// Message recipient number. Must be in international format. |
| 37 | + #[validate(length(min = 1, max = 24))] |
| 38 | + pub to: String, |
| 39 | + |
| 40 | + /// The ID that uniquely identifies the message sent. |
| 41 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 42 | + #[validate(length(min = 0, max = 50))] |
| 43 | + pub message_id: Option<String>, |
| 44 | + |
| 45 | + /// The content object to build a message that will be sent. |
| 46 | + #[validate] |
| 47 | + pub content: TextMessageContent, |
| 48 | + |
| 49 | + /// Custom client data that will be included in a Delivery Report. |
| 50 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 51 | + #[validate(length(min = 0, max = 4000))] |
| 52 | + pub callback_data: Option<String>, |
| 53 | + |
| 54 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 55 | + #[validate(url)] |
| 56 | + pub notify_url: Option<String>, |
| 57 | +} |
| 58 | + |
| 59 | +impl SendTextRequestBody { |
| 60 | + pub fn new(from: String, to: String, content: TextMessageContent) -> Self { |
| 61 | + SendTextRequestBody { |
| 62 | + from, |
| 63 | + to, |
| 64 | + message_id: None, |
| 65 | + content, |
| 66 | + callback_data: None, |
| 67 | + notify_url: None, |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 73 | +#[serde(rename_all = "camelCase")] |
| 74 | +pub struct Status { |
| 75 | + /// Status group ID. |
| 76 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 77 | + pub group_id: Option<i32>, |
| 78 | + |
| 79 | + /// Status group name. |
| 80 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 81 | + pub group_name: Option<String>, |
| 82 | + |
| 83 | + /// Action that should be taken to eliminate the error. |
| 84 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 85 | + pub action: Option<String>, |
| 86 | + |
| 87 | + /// Status ID. |
| 88 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 89 | + pub id: Option<i32>, |
| 90 | + |
| 91 | + /// Status name. |
| 92 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 93 | + pub name: Option<String>, |
| 94 | + |
| 95 | + /// Human-readable description of the status. |
| 96 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 97 | + pub description: Option<String>, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 101 | +#[serde(rename_all = "camelCase")] |
| 102 | +pub struct SendTextResponseBody { |
| 103 | + /// The destination address of the message. |
| 104 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 105 | + pub to: Option<String>, |
| 106 | + |
| 107 | + /// Number of messages required to deliver. |
| 108 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 109 | + pub message_count: Option<i32>, |
| 110 | + |
| 111 | + /// The ID that uniquely identifies the message sent. If not passed, it will be automatically |
| 112 | + /// generated and returned in a response. |
| 113 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 114 | + pub message_id: Option<String>, |
| 115 | + |
| 116 | + /// Indicates the status of the message and how to recover from an error should there be any. |
| 117 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 118 | + pub status: Option<Status>, |
| 119 | +} |
0 commit comments