diff --git a/src/service/core.rs b/src/service/core.rs index 5f31188..ebd55a2 100644 --- a/src/service/core.rs +++ b/src/service/core.rs @@ -97,7 +97,7 @@ impl Service { 5. Starting with the message - Done 6. Get the contact - Done 7. Send the message - 8. Record the message event response + 8. Record the message event response - Added, not used 9. Add to scheduler "# ); diff --git a/src/service/mer.rs b/src/service/mer.rs new file mode 100644 index 0000000..894edc3 --- /dev/null +++ b/src/service/mer.rs @@ -0,0 +1,103 @@ +pub struct MessageEventResponse { + pub app: crate::app::App, +} + +impl MessageEventResponse { + pub async fn record_message_event_response( + &self, + mer: &textsender_models::message::event::MessageEventResponse, + ) -> Result { + let client = reqwest::Client::new(); + let endpoint = String::from("api/v1/schedule/message/event/response/record"); + let api_url = format!("{}/{endpoint}", self.app.auth_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": textsender_models::message::event::MESSAGE_EVENT_RESPONSE_STATUS_SCHEDULED, + "contact_id": mer.contact_id, + "message_id": mer.message_id + }); + + println!("Payload: {payload:?}"); + + match client.post(api_url).json(&payload).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 { + match serde_json::from_str::(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: textsender_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())), + } +} diff --git a/src/service/mod.rs b/src/service/mod.rs index 5779824..851ce6d 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -2,6 +2,7 @@ pub mod auth; pub mod contact; pub mod core; pub mod event; +pub mod mer; pub mod message; pub mod queue;