-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigron_sms.rs
251 lines (214 loc) · 7.66 KB
/
tigron_sms.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Rust-module to send text-messages through Tigron's SMS-API using SOAP.
Written by: Niel Duysters (contact@ndvibes.com)
*/
use regex::Regex;
use xml::reader::{EventReader, XmlEvent};
// Client to send a text-message through Tigron's API
pub struct TigronSms {
pub credentials: (String, String),
}
// Basic SOAP-client to interact with API
// Note: This SOAP-client will only suffice for the sms use-case.
struct SoapClient {
pub url: String,
pub ns: String,
pub credentials: (String, String),
}
// Basic XML Parser to interpet the response from the Tigron-API
// Note: This parser will only suffice for interpreting the response of the 'info' procedure.
struct XmlResponseParser;
impl TigronSms {
/*
Method to send a text-message
:param to: Telephone number to send message to. Format: +xx.xxxxxxxxx
:param from: Source of message. Format: +xx.xxxxxxxxx
:param message: Content of message to send
:return Result<(), &str>: Returns Ok() if successfull. Returns an error otherwise.
*/
pub async fn send(&self, to: String, from: String, message: String) -> Result<(), &str> {
// Input validation
let phone_number_regex = Regex::new(r"\+\d{2,3}\.\d{7,12}").unwrap();
if message.len() == 0 {
return Err("Message cannot be empty.");
}
if message.len() > 160 {
return Err("Message cannot be more than 160 characters");
}
if !phone_number_regex.is_match(&to) || !phone_number_regex.is_match(&from) {
return Err(
"Phone numbers must be in the format: +xx.yyyyyyyyy where xx is the country code.",
);
}
let soap_client = SoapClient {
url: "https://api.tigron.net/soap".to_string(),
ns: "https://www.tigron.net/ns/".to_string(),
credentials: (
self.credentials.0.to_string(),
self.credentials.1.to_string(),
),
};
let user_id = &*self.get_user_id().await;
if user_id.is_empty() {
return Err("User not found. Are your credentials correct?");
}
let sms_params = vec![
("user_id", user_id),
("from", &*from),
("to", &*to),
("message", &*message),
];
soap_client.call("sms", "send_sms", Some(sms_params)).await;
Ok(())
}
// Function to retrieve user_id
async fn get_user_id(&self) -> String {
let soap_client = SoapClient {
url: "https://api.tigron.net/soap".to_string(),
ns: "https://www.tigron.net/ns/".to_string(),
credentials: (
self.credentials.0.to_string(),
self.credentials.1.to_string(),
),
};
let response = soap_client.call("user", "info", None).await;
let response_items = XmlResponseParser::parse(&response).await;
let user_id = XmlResponseParser::value(&response_items, "id").await;
user_id
}
}
impl SoapClient {
/*
Send a command to the API and retrieve XML
:param service: Service of API to execute a command on. E.g: "sms"
:param cmd: The command to execute. E.g: "send_sms"
:param params: Parameters of the command. E.g: [("from", "xxxx.xxx.xxx"), ("to", "yyyy.yyy.yyy")]
:param String: Returns the body of the API-response
*/
pub async fn call(
&self,
service: &str,
cmd: &str,
params: Option<std::vec::Vec<(&str, &str)>>,
) -> String {
let http = reqwest::Client::new();
let params = match params {
Some(params) => params,
None => std::vec::Vec::new(),
};
let cmd_xml = self.cmd_and_params_to_wsdl(cmd, params).await;
let soap_body = self.soap_body(cmd_xml).await;
let response = http
.post(&format!(
"{url}/{service}?WSDL",
url = self.url,
service = service
))
.header("Content-Type", "application/xml")
.body(soap_body)
.send()
.await
.expect("Failed to get response.")
.text()
.await
.unwrap();
response
}
// Convert the array from the command and params-array into WSDL/XML format
async fn cmd_and_params_to_wsdl(
&self,
cmd: &str,
params: std::vec::Vec<(&str, &str)>,
) -> String {
let mut xml = String::new();
for param in params.iter() {
let element = format!("<{key}>{value}</{key}>", key = param.0, value = param.1);
xml = format!("{}{}", xml, element);
}
xml = format!(
"<{cmd} xmlns=\"{ns}\">{params}</{cmd}>",
cmd = cmd,
params = xml,
ns = self.ns
);
xml
}
// Function to get the full WSDL for the call
async fn soap_body(&self, cmd_xml: String) -> String {
let wsdl = format!(
r#"<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<authenticate_user xmlns="{ns}">
<username>{username}</username>
<password>{password}</password>
</authenticate_user>
</soap:Header>
<soap:Body>
{cmd}
</soap:Body>
</soap:Envelope>"#,
ns = self.ns,
username = self.credentials.0,
password = self.credentials.1,
cmd = cmd_xml
);
wsdl
}
}
impl XmlResponseParser {
/*
:param xml: Takes XML as input. E.g: <item><key>xxx</key><value>yyy</value></item>
:return Vec<(String, String)>: Returns a vector of tuples (key, value)
*/
async fn parse(xml: &str) -> std::vec::Vec<(String, String)> {
let mut return_items: std::vec::Vec<(String, String)> = std::vec::Vec::new();
let parser = EventReader::from_str(xml);
let mut read_key = false;
let mut read_value = false;
let mut key: String = String::new();
for e in parser {
match e {
Ok(XmlEvent::StartElement { name, .. }) => {
read_key = false;
read_value = false;
if name.local_name == "key" {
read_key = true;
}
if name.local_name == "value" {
read_value = true;
}
}
Ok(XmlEvent::Characters(text)) => {
if read_key {
key = text.to_string();
}
if read_value {
return_items.push((key.to_string(), text.to_string()));
}
}
Err(_e) => {
break;
}
_ => {}
}
}
return_items
}
/*
Returns the value of the matching key
:param items: Array of returned_items retrieved from API-response
:param key: The key we want to retrieve the value from
:return String: Returns the value matching the key
*/
async fn value(items: &std::vec::Vec<(String, String)>, key: &str) -> String {
for pair in items.iter() {
if pair.0 == key {
return pair.1.to_string();
}
}
"".to_string()
}
}