use super::{C2CMessageUser, MessageAttachment, MessageReference};
use crate::models::Timestamp;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::models::message::C2CMessageParams;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct C2CMessage {
pub id: Option<String>,
pub content: Option<String>,
pub message_reference: Option<MessageReference>,
#[serde(default)]
pub mentions: Vec<C2CMessageUser>,
#[serde(default)]
pub attachments: Vec<MessageAttachment>,
pub msg_seq: Option<u64>,
pub timestamp: Option<Timestamp>,
pub author: Option<C2CMessageUser>,
pub message_scene: Option<Value>,
#[serde(skip)]
pub event_id: Option<String>,
}
impl C2CMessage {
pub async fn reply(
&self,
api: &crate::api_impl::BotApi,
content: &str,
) -> Result<crate::models::api::MessageResponse, crate::error::BotError> {
if let (Some(user_openid), Some(msg_id)) = (
self.author.as_ref().and_then(|a| a.user_openid.as_ref()),
&self.id,
) {
let params = C2CMessageParams {
msg_type: 0,
content: Some(content.to_string()),
msg_id: Some(msg_id.clone()),
msg_seq: Some(1),
event_id: self.event_id.clone(),
..Default::default()
};
api.send_c2c_message(user_openid, params).await
} else {
Err(crate::error::BotError::InvalidData(
"Missing user_openid or message_id for C2C reply".to_string(),
))
}
}
}