113 lines
4.0 KiB
Rust
113 lines
4.0 KiB
Rust
pub struct MessageEventResponse {
|
|
pub app: std::sync::Arc<crate::app::App>,
|
|
pub token: std::sync::Arc<schedtxt_models::token::LoginResult>,
|
|
}
|
|
|
|
impl MessageEventResponse {
|
|
pub async fn record_message_event_response(
|
|
&self,
|
|
mer: &schedtxt_models::message::event::MessageEventResponse,
|
|
) -> Result<schedtxt_models::message::event::MessageEventResponse, std::io::Error> {
|
|
let client = reqwest::Client::new();
|
|
let endpoint = String::from("api/v1/schedule/message/event/response/record");
|
|
let api_url = format!("{}/{endpoint}", self.app.api_url);
|
|
|
|
println!("Url: {api_url:?}");
|
|
|
|
use time::format_description::well_known::Iso8601;
|
|
let sent = match mer.sent.unwrap().format(&Iso8601::DEFAULT) {
|
|
Ok(converted) => converted,
|
|
Err(err) => {
|
|
return Err(std::io::Error::other(err.to_string()));
|
|
}
|
|
};
|
|
|
|
let payload = serde_json::json!({
|
|
"scheduled_message_event_id": mer.scheduled_message_event_id.unwrap(),
|
|
"response": mer.response,
|
|
"user_id": mer.user_id,
|
|
"sent": sent,
|
|
"status": schedtxt_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED,
|
|
"contact_id": mer.contact_id,
|
|
"message_id": mer.message_id
|
|
});
|
|
|
|
println!("Payload: {payload:?}");
|
|
|
|
let (key, header) = super::auth_header(&self.token.access_token);
|
|
|
|
match client
|
|
.post(api_url)
|
|
.json(&payload)
|
|
.header(key, header)
|
|
.send()
|
|
.await
|
|
{
|
|
Ok(response) => match response.status() {
|
|
reqwest::StatusCode::CREATED | reqwest::StatusCode::OK => {
|
|
println!("Response: {response:?}");
|
|
match response.text().await {
|
|
Ok(val) => match parse_response(&val).await {
|
|
Ok(login_result) => Ok(login_result),
|
|
Err(err) => Err(err),
|
|
},
|
|
Err(err) => Err(std::io::Error::other(err)),
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other(
|
|
"Error recording Message Event Response",
|
|
)),
|
|
},
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|
|
|
|
/*
|
|
*
|
|
* {
|
|
"scheduled_message_event_id": "{{scheduled_message_event_id}}",
|
|
"response": {{text_response_raw}},
|
|
"user_id": "{{user_id}}",
|
|
"sent": "2026-06-20T17:10:00Z",
|
|
"status": "{{mer_status_scheduled}}",
|
|
"contact_id": "{{contact_id}}",
|
|
"message_id": "{{message_id}}"
|
|
}
|
|
*
|
|
*/
|
|
}
|
|
|
|
async fn parse_response(
|
|
response: &str,
|
|
) -> Result<schedtxt_models::message::event::MessageEventResponse, std::io::Error> {
|
|
match serde_json::from_str::<serde_json::Value>(response) {
|
|
Ok(j) => {
|
|
println!("Good");
|
|
let message = &j["message"];
|
|
let data = &j["data"];
|
|
|
|
println!("Message: {message:?}");
|
|
println!("Data: {data:?}");
|
|
match j.get("data") {
|
|
Some(serde_json::Value::Array(lrs)) => {
|
|
if lrs.is_empty() {
|
|
Err(std::io::Error::other("Error response is empty"))
|
|
} else {
|
|
let lr = lrs[0].clone();
|
|
let ll: schedtxt_models::message::event::MessageEventResponse =
|
|
match serde_json::from_value(lr) {
|
|
Ok(lr) => lr,
|
|
Err(err) => {
|
|
return Err(std::io::Error::other(err.to_string()));
|
|
}
|
|
};
|
|
Ok(ll)
|
|
}
|
|
}
|
|
_ => Err(std::io::Error::other("Error parsing response")),
|
|
}
|
|
}
|
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
|
}
|
|
}
|