41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
pub const ID: &str = "code2";
|
|
pub const CODE: &str = "Qj4eAn";
|
|
pub const NEXT_STEP_TEXT: &str = "TODO: replace this with the next step.";
|
|
|
|
const SELF_NOTIFICATION: &str = "Code 2 has been submitted.";
|
|
|
|
pub(super) async fn on_submit(live: bool) -> Result<(), String> {
|
|
if !live {
|
|
leptos::logging::log!("Code 2 SMS (not sent; server is offline): {SELF_NOTIFICATION}");
|
|
return Ok(());
|
|
}
|
|
|
|
let secrets = crate::secrets::runtime();
|
|
|
|
let mut payload = serde_json::json!({
|
|
"message": SELF_NOTIFICATION,
|
|
"recipients": [&secrets.recipient_self],
|
|
});
|
|
if let Some(device_id) = &secrets.textbee_device_id {
|
|
payload["deviceId"] = serde_json::Value::String(device_id.clone());
|
|
}
|
|
|
|
let response = reqwest::Client::new()
|
|
.post("https://api.textbee.dev/api/v1/gateway/send-sms")
|
|
.header("x-api-key", &secrets.textbee_api_key)
|
|
.json(&payload)
|
|
.send()
|
|
.await
|
|
.map_err(|error| format!("TextBee request failed: {error}"))?;
|
|
|
|
let status = response.status();
|
|
if !status.is_success() {
|
|
let response_body = response.text().await.map_err(|error| {
|
|
format!("TextBee returned HTTP {status}; unable to read error: {error}")
|
|
})?;
|
|
return Err(format!("TextBee returned HTTP {status}: {response_body}"));
|
|
}
|
|
|
|
Ok(())
|
|
}
|