Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d36483e6f8 | ||
|
|
768b2b6d08 | ||
|
|
e1e730fd1c |
@@ -5,6 +5,10 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
name: Check
|
name: Check
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
name: Check
|
name: Check
|
||||||
|
|||||||
Generated
+683
-276
File diff suppressed because it is too large
Load Diff
+3
-11
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "swoosh"
|
name = "swoosh"
|
||||||
version = "0.4.0"
|
version = "0.5.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.96"
|
rust-version = "1.96"
|
||||||
description = "Library to send text messages"
|
description = "Library to send text messages"
|
||||||
@@ -8,17 +8,9 @@ description = "Library to send text messages"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
serde_json = { version = "1.0.150" }
|
serde_json = { version = "1.0.150" }
|
||||||
tokio = { version = "1.52.3", features = ["full"] }
|
|
||||||
futures = { version = "0.3.32" }
|
|
||||||
http = { version = "1.4.2" }
|
|
||||||
reqwest = { version = "0.13.4", features = ["form", "json", "blocking", "multipart", "stream"] }
|
reqwest = { version = "0.13.4", features = ["form", "json", "blocking", "multipart", "stream"] }
|
||||||
rand = { version = "0.10.1" }
|
|
||||||
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
|
time = { version = "0.3.49", features = ["formatting", "macros", "parsing", "serde"] }
|
||||||
uuid = { version = "1.23.3", features = ["v4", "serde"] }
|
base64-ng = { version = "1.3.5" }
|
||||||
base64-ng = { version = "1.0.8" }
|
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.5.0", features = ["config", "contact", "message"] }
|
||||||
const_format = { version = "0.2.36" }
|
|
||||||
josekit = { version = "0.10.3" }
|
|
||||||
textsender_models = { git = "ssh://git@git.kundeng.us/phoenix/textsender_models.git", tag = "v0.4.0", features = ["config", "contact", "message"] }
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = { version = "3.27.0" }
|
|
||||||
|
|||||||
+106
@@ -1 +1,107 @@
|
|||||||
pub mod twilio;
|
pub mod twilio;
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct SendMsg {
|
||||||
|
config: VendorConfig,
|
||||||
|
message: Message,
|
||||||
|
recipient: Recipient,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SendMsg {
|
||||||
|
pub fn load_config(
|
||||||
|
&mut self,
|
||||||
|
auth_token: &str,
|
||||||
|
phone_number: &str,
|
||||||
|
service_sid: &str,
|
||||||
|
account_sid: &str,
|
||||||
|
) {
|
||||||
|
self.config = VendorConfig {
|
||||||
|
auth_token: auth_token.to_string(),
|
||||||
|
phone_number: phone_number.to_string(),
|
||||||
|
service_sid: service_sid.to_string(),
|
||||||
|
account_sid: account_sid.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_message(&mut self, content: &str) {
|
||||||
|
self.message = Message {
|
||||||
|
content: content.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_recipient(&mut self, recpient: &str) {
|
||||||
|
self.recipient = Recipient {
|
||||||
|
phone_number: recpient.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_message(
|
||||||
|
&self,
|
||||||
|
param: &crate::twilio::types::Parameters,
|
||||||
|
) -> Result<reqwest::Response, std::io::Error> {
|
||||||
|
if self.recipient.phone_number.is_empty() {
|
||||||
|
Err(std::io::Error::other("Recipient not provided"))
|
||||||
|
} else if self.message.content.is_empty() {
|
||||||
|
Err(std::io::Error::other("Message not provided"))
|
||||||
|
} else if self.config.phone_number.is_empty()
|
||||||
|
|| self.config.service_sid.is_empty()
|
||||||
|
|| self.config.auth_token.is_empty()
|
||||||
|
|| self.config.account_sid.is_empty()
|
||||||
|
{
|
||||||
|
Err(std::io::Error::other("Config not populated"))
|
||||||
|
} else {
|
||||||
|
let client = match reqwest::Client::builder().build() {
|
||||||
|
Ok(client) => client,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let url = twilio::api::twilio_url(&self.config.account_sid);
|
||||||
|
let headers = match twilio::api::init_headers(
|
||||||
|
&self.config.account_sid,
|
||||||
|
&self.config.auth_token,
|
||||||
|
) {
|
||||||
|
Ok(headers) => headers,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let params = twilio::api::initial_params(
|
||||||
|
&self.recipient.phone_number,
|
||||||
|
&self.message.content,
|
||||||
|
&self.config.phone_number,
|
||||||
|
&self.config.service_sid,
|
||||||
|
param,
|
||||||
|
);
|
||||||
|
let request = client.post(url).headers(headers).form(¶ms);
|
||||||
|
match request.send().await {
|
||||||
|
Ok(response) => match response.status() {
|
||||||
|
reqwest::StatusCode::CREATED => Ok(response),
|
||||||
|
status => Err(std::io::Error::other(format!(
|
||||||
|
"Unaccounted status: {status:?}"
|
||||||
|
))),
|
||||||
|
},
|
||||||
|
Err(err) => Err(std::io::Error::other(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct VendorConfig {
|
||||||
|
pub auth_token: String,
|
||||||
|
pub phone_number: String,
|
||||||
|
pub service_sid: String,
|
||||||
|
pub account_sid: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Message {
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Recipient {
|
||||||
|
pub phone_number: String,
|
||||||
|
}
|
||||||
|
|||||||
+163
-36
@@ -1,5 +1,7 @@
|
|||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
/// Converts the response into a json value
|
||||||
|
/// That way you do not need to share swoosh's dependency
|
||||||
pub async fn response_to_json(response: reqwest::Response) -> serde_json::Value {
|
pub async fn response_to_json(response: reqwest::Response) -> serde_json::Value {
|
||||||
match response.json().await {
|
match response.json().await {
|
||||||
Ok(json_body) => json_body,
|
Ok(json_body) => json_body,
|
||||||
@@ -7,10 +9,14 @@ pub async fn response_to_json(response: reqwest::Response) -> serde_json::Value
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_mssage(
|
/// Backwards compatible
|
||||||
|
pub use send_message as send_mssage;
|
||||||
|
|
||||||
|
/// Send a message
|
||||||
|
pub async fn send_message(
|
||||||
msg: &textsender_models::message::Message,
|
msg: &textsender_models::message::Message,
|
||||||
contact: &textsender_models::contact::Contact,
|
contact: &textsender_models::contact::Contact,
|
||||||
param: crate::twilio::types::Parameters,
|
param: &crate::twilio::types::Parameters,
|
||||||
config: &textsender_models::config::auxiliary::TwilioConfig,
|
config: &textsender_models::config::auxiliary::TwilioConfig,
|
||||||
) -> Result<reqwest::Response, std::io::Error> {
|
) -> Result<reqwest::Response, std::io::Error> {
|
||||||
if config.account_sid.is_empty() {
|
if config.account_sid.is_empty() {
|
||||||
@@ -18,7 +24,6 @@ pub async fn send_mssage(
|
|||||||
} else if config.auth_token.is_empty() {
|
} else if config.auth_token.is_empty() {
|
||||||
Err(std::io::Error::other(" Auth token is empty"))
|
Err(std::io::Error::other(" Auth token is empty"))
|
||||||
} else {
|
} else {
|
||||||
let now = time::OffsetDateTime::now_utc();
|
|
||||||
match reqwest::Client::builder().build() {
|
match reqwest::Client::builder().build() {
|
||||||
Ok(client) => {
|
Ok(client) => {
|
||||||
let mut headers = reqwest::header::HeaderMap::new();
|
let mut headers = reqwest::header::HeaderMap::new();
|
||||||
@@ -30,27 +35,7 @@ pub async fn send_mssage(
|
|||||||
let auth = generate_auth(config).unwrap();
|
let auth = generate_auth(config).unwrap();
|
||||||
headers.insert("Authorization", auth.parse().unwrap());
|
headers.insert("Authorization", auth.parse().unwrap());
|
||||||
|
|
||||||
let mut params = std::collections::HashMap::new();
|
let params = init_params(contact, msg, config, param);
|
||||||
params.insert("To", contact.phone_number.as_str());
|
|
||||||
params.insert("ProvideFeedback", "true");
|
|
||||||
params.insert("ForceDelivery", "false");
|
|
||||||
params.insert("ContentRetention", "retain");
|
|
||||||
params.insert("AddressRetention", "obfuscate");
|
|
||||||
params.insert("SmartEncoded", "true");
|
|
||||||
params.insert("ShortenUrls", "true");
|
|
||||||
let date = match param.schedule_at {
|
|
||||||
Some(date_value) => date_value.to_string(),
|
|
||||||
None => String::new(),
|
|
||||||
};
|
|
||||||
if param.schedule && is_scheduleable(&Some(now), ¶m.schedule_at) {
|
|
||||||
params.insert("ScheduleType", "fixed");
|
|
||||||
params.insert("SendAt", date.as_str());
|
|
||||||
}
|
|
||||||
params.insert("SendAsMms", "true");
|
|
||||||
params.insert("RiskCheck", "enable");
|
|
||||||
params.insert("From", config.phone_number.as_str());
|
|
||||||
params.insert("MessagingServiceSid", config.service_sid.as_str());
|
|
||||||
params.insert("Body", msg.content.as_str());
|
|
||||||
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json",
|
"https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json",
|
||||||
@@ -73,16 +58,135 @@ pub async fn send_mssage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_auth(
|
pub fn twilio_url(account_sid: &str) -> String {
|
||||||
config: &textsender_models::config::auxiliary::TwilioConfig,
|
format!(
|
||||||
) -> Result<String, base64_ng::EncodeError> {
|
"https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json",
|
||||||
let input = format!("{}:{}", config.account_sid, config.auth_token);
|
account_sid
|
||||||
match base64_ng::encode(input.as_bytes()) {
|
)
|
||||||
Ok(encoded) => Ok(format!("Basic {encoded}")),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
|
pub fn init_headers(
|
||||||
|
account_sid: &str,
|
||||||
|
auth_token: &str,
|
||||||
|
) -> Result<reqwest::header::HeaderMap, std::io::Error> {
|
||||||
|
let mut headers = reqwest::header::HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
"Content-Type",
|
||||||
|
match "application/x-www-form-urlencoded".parse() {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let auth = match g_auth(account_sid, auth_token) {
|
||||||
|
Ok(auth) => auth,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
headers.insert(
|
||||||
|
"Authorization",
|
||||||
|
match format!("Basic {auth}").parse() {
|
||||||
|
Ok(auth) => auth,
|
||||||
|
Err(err) => {
|
||||||
|
return Err(std::io::Error::other(err));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initial_params(
|
||||||
|
recpient: &str,
|
||||||
|
content: &str,
|
||||||
|
config_source_phone_number: &str,
|
||||||
|
config_service_sid: &str,
|
||||||
|
param: &crate::twilio::types::Parameters,
|
||||||
|
) -> std::collections::HashMap<String, String> {
|
||||||
|
let now = time::OffsetDateTime::now_utc();
|
||||||
|
let mut params = std::collections::HashMap::new();
|
||||||
|
params.insert(String::from("To"), recpient.to_string());
|
||||||
|
params.insert(String::from("ProvideFeedback"), String::from("true"));
|
||||||
|
params.insert(String::from("ForceDelivery"), String::from("false"));
|
||||||
|
params.insert(String::from("ContentRetention"), String::from("retain"));
|
||||||
|
params.insert(String::from("AddressRetention"), String::from("obfuscate"));
|
||||||
|
params.insert(String::from("SmartEncoded"), String::from("true"));
|
||||||
|
params.insert(String::from("ShortenUrls"), String::from("true"));
|
||||||
|
let date = match param.schedule_at {
|
||||||
|
Some(date_value) => match convert_time_to_iso(date_value) {
|
||||||
|
Ok(converted) => converted,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => String::new(),
|
||||||
|
};
|
||||||
|
let scheduled_at = match param.schedule_at {
|
||||||
|
Some(s) => s,
|
||||||
|
None => time::OffsetDateTime::now_utc(),
|
||||||
|
};
|
||||||
|
if param.schedule && is_scheduleable(Some(&now), Some(&scheduled_at)) {
|
||||||
|
params.insert(String::from("ScheduleType"), String::from("fixed"));
|
||||||
|
params.insert(String::from("SendAt"), date.clone());
|
||||||
|
}
|
||||||
|
params.insert(String::from("SendAsMms"), String::from("true"));
|
||||||
|
params.insert(String::from("RiskCheck"), String::from("enable"));
|
||||||
|
params.insert(String::from("From"), config_source_phone_number.to_string());
|
||||||
|
params.insert(
|
||||||
|
String::from("MessagingServiceSid"),
|
||||||
|
config_service_sid.to_string(),
|
||||||
|
);
|
||||||
|
params.insert(String::from("Body"), content.to_string());
|
||||||
|
|
||||||
|
params
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_params(
|
||||||
|
contact: &textsender_models::contact::Contact,
|
||||||
|
message: &textsender_models::message::Message,
|
||||||
|
config: &textsender_models::config::auxiliary::TwilioConfig,
|
||||||
|
param: &crate::twilio::types::Parameters,
|
||||||
|
) -> std::collections::HashMap<String, String> {
|
||||||
|
let now = time::OffsetDateTime::now_utc();
|
||||||
|
let mut params = std::collections::HashMap::new();
|
||||||
|
params.insert(String::from("To"), contact.phone_number.clone());
|
||||||
|
params.insert(String::from("ProvideFeedback"), String::from("true"));
|
||||||
|
params.insert(String::from("ForceDelivery"), String::from("false"));
|
||||||
|
params.insert(String::from("ContentRetention"), String::from("retain"));
|
||||||
|
params.insert(String::from("AddressRetention"), String::from("obfuscate"));
|
||||||
|
params.insert(String::from("SmartEncoded"), String::from("true"));
|
||||||
|
params.insert(String::from("ShortenUrls"), String::from("true"));
|
||||||
|
let date = match param.schedule_at {
|
||||||
|
Some(date_value) => match convert_time_to_iso(date_value) {
|
||||||
|
Ok(converted) => converted,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error: {err:?}");
|
||||||
|
String::new()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => String::new(),
|
||||||
|
};
|
||||||
|
let scheduled_at = match param.schedule_at {
|
||||||
|
Some(s) => s,
|
||||||
|
None => time::OffsetDateTime::now_utc(),
|
||||||
|
};
|
||||||
|
if param.schedule && is_scheduleable(Some(&now), Some(&scheduled_at)) {
|
||||||
|
params.insert(String::from("ScheduleType"), String::from("fixed"));
|
||||||
|
params.insert(String::from("SendAt"), date.clone());
|
||||||
|
}
|
||||||
|
params.insert(String::from("SendAsMms"), String::from("true"));
|
||||||
|
params.insert(String::from("RiskCheck"), String::from("enable"));
|
||||||
|
params.insert(String::from("From"), config.phone_number.clone());
|
||||||
|
params.insert(
|
||||||
|
String::from("MessagingServiceSid"),
|
||||||
|
config.service_sid.clone(),
|
||||||
|
);
|
||||||
|
params.insert(String::from("Body"), message.content.clone());
|
||||||
|
|
||||||
|
/*
|
||||||
let mut params = std::collections::HashMap::new();
|
let mut params = std::collections::HashMap::new();
|
||||||
params.insert("StatusCallback", "http://OjQozHznkhNTTR.vpnrM1zdXFuiQ");
|
params.insert("StatusCallback", "http://OjQozHznkhNTTR.vpnrM1zdXFuiQ");
|
||||||
params.insert("MaxPrice", "1");
|
params.insert("MaxPrice", "1");
|
||||||
@@ -98,13 +202,36 @@ fn generate_auth(
|
|||||||
params.insert("ShortenUrls", "true");
|
params.insert("ShortenUrls", "true");
|
||||||
params.insert("SendAsMms", "true");
|
params.insert("SendAsMms", "true");
|
||||||
params.insert("RiskCheck", "enable");
|
params.insert("RiskCheck", "enable");
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
params
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_time_to_iso(time: time::OffsetDateTime) -> Result<String, time::error::Format> {
|
||||||
|
use time::format_description::well_known::Iso8601;
|
||||||
|
time.format(&Iso8601::DEFAULT)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_auth(
|
||||||
|
config: &textsender_models::config::auxiliary::TwilioConfig,
|
||||||
|
) -> Result<String, base64_ng::EncodeError> {
|
||||||
|
let input = format!("{}:{}", config.account_sid, config.auth_token);
|
||||||
|
match base64_ng::encode(input.as_bytes()) {
|
||||||
|
Ok(encoded) => Ok(format!("Basic {encoded}")),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn g_auth(account_sid: &str, auth_token: &str) -> Result<String, base64_ng::EncodeError> {
|
||||||
|
let input = format!("{}:{}", account_sid, auth_token);
|
||||||
|
base64_ng::encode(input.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
const DEFAULT_SCHEDULING_SECONDS: i64 = 300;
|
const DEFAULT_SCHEDULING_SECONDS: i64 = 300;
|
||||||
|
|
||||||
fn is_scheduleable(
|
fn is_scheduleable(
|
||||||
now: &Option<time::OffsetDateTime>,
|
now: Option<&time::OffsetDateTime>,
|
||||||
scheduled: &Option<time::OffsetDateTime>,
|
scheduled: Option<&time::OffsetDateTime>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match scheduled {
|
match scheduled {
|
||||||
Some(schedule_at) => {
|
Some(schedule_at) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user