about summary refs log tree commit diff
path: root/tools/sendsms/src/message.rs
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2022-09-05 16:59:20 -0700
committerFranck Cuny <franck@fcuny.net>2022-09-07 19:12:12 -0700
commitc853a5078b0a8dee22bb69b971b8315f66033f49 (patch)
tree3c05da1eb7c9d39e936de21108f42a59765ab4ef /tools/sendsms/src/message.rs
parentmeta: ignore build for rust projects (diff)
downloadworld-c853a5078b0a8dee22bb69b971b8315f66033f49.tar.gz
feat(tool/sendsms): a CLI to send SMS
This is a new tool to send SMS via Twilio's API. For now it supports a
single subcommand: reboot. Using that subcommand, a SMS will be send
with the name of the host and the IP address for the defined network
interface. This is useful to be notified when one of my machine reboot,
and what's the IP for the main interface (this is useful since my ISP
does not provide a static IP).

Change-Id: I5886a2c77ebd344ab3befa51a6bdd3d65bcc85d4
Diffstat (limited to 'tools/sendsms/src/message.rs')
-rwxr-xr-xtools/sendsms/src/message.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/sendsms/src/message.rs b/tools/sendsms/src/message.rs
new file mode 100755
index 0000000..9aa94a4
--- /dev/null
+++ b/tools/sendsms/src/message.rs
@@ -0,0 +1,76 @@
+use crate::config::Config;
+use reqwest::blocking::Client;
+use serde::Deserialize;
+use std::collections::HashMap;
+use std::fmt::{self, Display, Formatter};
+
+const TWILIO_BASE_URL: &str = "https://api.twilio.com/2010-04-01/Accounts";
+
+#[derive(Deserialize, Debug)]
+pub struct Message {
+    pub from: String,
+    pub to: String,
+    pub body: String,
+}
+
+// list of possible values: https://www.twilio.com/docs/sms/api/message-resource#message-status-values
+#[derive(Debug, Deserialize, Clone)]
+#[allow(non_camel_case_types)]
+pub enum MessageStatus {
+    accepted,
+    scheduled,
+    queued,
+    sending,
+    sent,
+    receiving,
+    received,
+    delivered,
+    undelivered,
+    failed,
+    read,
+    canceled,
+}
+
+#[derive(Deserialize, Debug, Clone)]
+pub struct MessageResponse {
+    pub status: Option<MessageStatus>,
+}
+
+#[derive(Debug)]
+pub enum TwilioError {
+    HTTPError(reqwest::StatusCode),
+}
+
+impl Display for TwilioError {
+    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+        match *self {
+            TwilioError::HTTPError(ref s) => write!(f, "Invalid HTTP status code: {}", s),
+        }
+    }
+}
+
+impl Message {
+    pub fn send(&self, config: &Config) -> Result<MessageResponse, TwilioError> {
+        let url = format!("{}/{}/Messages.json", TWILIO_BASE_URL, config.account_sid);
+
+        let mut form = HashMap::new();
+        form.insert("From", &self.from);
+        form.insert("To", &self.to);
+        form.insert("Body", &self.body);
+
+        let client = Client::new();
+        let response = client
+            .post(url)
+            .basic_auth(&config.account_sid, Some(&config.auth_token))
+            .form(&form)
+            .send()
+            .unwrap();
+
+        match response.status() {
+            reqwest::StatusCode::CREATED | reqwest::StatusCode::OK => {}
+            other => return Err(TwilioError::HTTPError(other)),
+        };
+
+        Ok(response.json().unwrap())
+    }
+}