obmsg 0.1.0

For quickly processing message events in the onebot protocol.
Documentation
/// 判断一个事件是否为群聊消息。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
///
/// # 返回值
///
/// 如果事件是群聊消息则返回`true`,否则返回`false`
pub fn is_group_msg(event:&serde_json::Value) -> bool {
    let post_type_json = &event["post_type"];
    if let Some(post_type) = post_type_json.as_str() {
        if post_type == "message" {
            let message_type_json = &event["message_type"];
            if let Some(message_type) = message_type_json.as_str() {
                if message_type == "group" {
                    return true;
                }
            }
        }
    }
    return false;
}

/// 判断一个事件是否为私聊消息。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
///
/// # 返回值
///
/// 如果事件是私聊消息则返回`true`,否则返回`false`
pub fn is_private_msg(event:&serde_json::Value) -> bool {
    let post_type_json = &event["post_type"];
    if let Some(post_type) = post_type_json.as_str() {
        if post_type == "message" {
            let message_type_json = &event["message_type"];
            if let Some(message_type) = message_type_json.as_str() {
                if message_type == "private" {
                    return true;
                }
            }
        }
    }
    return false;
}

/// 从事件中提取CQ格式的消息字符串。
///
/// # 参数
///
/// * `event` - 表示要处理的事件的JSON值
///
/// # 返回值
///
/// 如果找到CQ消息则返回`Some(String)`,否则返回`None`
fn _get_cq_message(event:&serde_json::Value) -> Option<String> {
    let message_json = &event["message"];
    if let Some(message) = message_json.as_str() {
        return Some(message.to_string());
    } else if message_json.is_array() {
        if let Ok(message) = cqtool::to_str_msg(message_json) {
            return Some(message);
        }
    }
    return None;
}

/// 检查消息是否与提供的关键字完全匹配。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 用于与消息进行匹配的字符串
///
/// # 返回值
///
/// 如果消息与关键字完全匹配则返回`true`,否则返回`false`
pub fn full_match(event:&serde_json::Value,key:&str) -> bool {
    if let Some(cq_message) = _get_cq_message(event) {
        if cq_message == key {
            return true;
        }
    }
    return false;
}

/// 检查消息是否包含提供的关键字字符串。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息中搜索的子字符串
///
/// # 返回值
///
/// 如果消息包含关键字则返回`true`,否则返回`false`
pub fn fuzzy_match(event:&serde_json::Value,key:&str) -> bool {
    if let Some(cq_message) = _get_cq_message(event) {
        if cq_message.contains(key) {
            return true;
        }
    }
    return false;
}

/// 使用正则表达式匹配消息并返回所有匹配项。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `regex` - 要匹配的正则表达式模式
///
/// # 返回值
///
/// 一个向量的向量,其中每个内部向量包含匹配的组
pub fn regex_match(event:&serde_json::Value,regex:&str) -> Vec<Vec<String>> {
    if let Some(cq_message) = _get_cq_message(event) {
        let re = fancy_regex::Regex::new(regex).unwrap();
        let mut result = Vec::new();
        for cap_result in re.captures_iter(&cq_message) {
            if let Ok(cap) = cap_result {
                let mut group = Vec::new();
                for i in 0..cap.len() {
                    if let Some(m) = cap.get(i) {
                        group.push(m.as_str().to_string());
                    }
                }
                result.push(group);
            }
        }
        return result;
    }
    return Vec::new();
}

/// 检查消息是否以提供的前缀开头并返回剩余部分。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息开头匹配的前缀
///
/// # 返回值
///
/// 如果匹配成功,则返回`Some(String)`,包含前缀之后的消息部分,否则返回`None`
pub fn prefix_match(event:&serde_json::Value,key:&str) -> Option<String> {
    if let Some(cq_message) = _get_cq_message(event) {
        if cq_message.starts_with(key) {
            // 返回非前缀部分
            return Some(cq_message[key.len()..].to_owned());
        }
    }
    return None;
}

/// 使用正则表达式匹配群聊消息并返回所有匹配项。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `regex` - 要匹配的正则表达式模式
///
/// # 返回值
///
/// 包含匹配组的向量的向量,如果不是群聊消息则返回空向量
pub fn group_regex_match(event:&serde_json::Value,regex:&str) -> Vec<Vec<String>> {
    if !is_group_msg(event) {
        return Vec::new();
    }
    return regex_match(event,regex);
}

/// 检查群聊消息是否以提供的前缀开头并返回剩余部分。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息开头匹配的前缀
///
/// # 返回值
///
/// 如果在群聊消息中匹配成功,则返回`Some(String)`,包含前缀之后的部分,否则返回`None`
pub fn group_prefix_match(event:&serde_json::Value,key:&str) -> Option<String> {
    if !is_group_msg(event) {
        return None;
    }
    return prefix_match(event,key);
}

/// 检查群聊消息是否包含提供的关键字字符串。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息中搜索的子字符串
///
/// # 返回值
///
/// 如果是群聊消息且包含关键字则返回`true`,否则返回`false`
pub fn group_fuzzy_match(event:&serde_json::Value,key:&str) -> bool {
    if !is_group_msg(event) {
        return false;
    }
    return fuzzy_match(event,key);
}

/// 使用正则表达式匹配私聊消息并返回所有匹配项。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `regex` - 要匹配的正则表达式模式
///
/// # 返回值
///
/// 包含匹配组的向量的向量,如果不是私聊消息则返回空向量
pub fn private_regex_match(event:&serde_json::Value,regex:&str) -> Vec<Vec<String>> {
    if !is_private_msg(event) {
        return Vec::new();
    }
    return regex_match(event,regex);
}

/// 检查私聊消息是否以提供的前缀开头并返回剩余部分。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息开头匹配的前缀
///
/// # 返回值
///
/// 如果在私聊消息中匹配成功,则返回`Some(String)`,包含前缀之后的部分,否则返回`None`
pub fn private_prefix_match(event:&serde_json::Value,key:&str) -> Option<String> {
    if !is_private_msg(event) {
        return None;
    }
    return prefix_match(event,key);
}

/// 检查私聊消息是否包含提供的关键字字符串。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 要在消息中搜索的子字符串
///
/// # 返回值
///
/// 如果是私聊消息且包含关键字则返回`true`,否则返回`false`
pub fn private_fuzzy_match(event:&serde_json::Value,key:&str) -> bool {
    if !is_private_msg(event) {
        return false;
    }
    return fuzzy_match(event,key);
}

/// 检查群聊消息是否与提供的关键字完全匹配。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 用于与消息进行匹配的字符串
///
/// # 返回值
///
/// 如果是群聊消息且与关键字完全匹配则返回`true`,否则返回`false`
pub fn group_full_macth(event:&serde_json::Value,key:&str) -> bool {
    if !is_group_msg(event) {
        return false;
    }
    return full_match(event,key);
}

/// 检查私聊消息是否与提供的关键字完全匹配。
///
/// # 参数
///
/// * `event` - 表示要检查的事件的JSON值
/// * `key` - 用于与消息进行匹配的字符串
///
/// # 返回值
///
/// 如果是私聊消息且与关键字完全匹配则返回`true`,否则返回`false`
pub fn private_full_macth(event:&serde_json::Value,key:&str) -> bool {
    if !is_private_msg(event) {
        return false;
    }
    return full_match(event,key);
}

/// 为给定(消息)事件自动生成回复JSON对象。
///
/// # 参数
///
/// * `event` - 表示要回复的消息事件的JSON值
/// * `msg` - 要发送的消息内容
///
/// # 返回值
///
/// 包含JSON响应对象的`Result`或错误信息
pub fn auto_gen_reply(event:&serde_json::Value,msg:&str) -> Result<serde_json::Value,Box<dyn std::error::Error>> {
    if is_group_msg(event) {
        return Ok(serde_json::json!({
            "action": "send_group_msg",
            "params": {
                "group_id": event["group_id"],
                "message": msg
            }
        }));
    } else if is_private_msg(event) {
        return Ok(serde_json::json!({
            "action": "send_private_msg",
            "params": {
                "user_id": event["user_id"],
                "message": msg
            }
        }));
    }
    return Err("unsupport message_type".into());
}