| Crates.io | obmsg |
| lib.rs | obmsg |
| version | 0.1.0 |
| created_at | 2025-03-13 13:04:25.919215+00 |
| updated_at | 2025-03-13 13:04:25.919215+00 |
| description | For quickly processing message events in the onebot protocol. |
| homepage | |
| repository | https://github.com/super1207/obmsg-rs |
| max_upload_size | |
| id | 1590885 |
| size | 18,860 |
用于快速处理onebot协议中的消息事件。
cargo add obmsg
use obmsg::{is_group_msg, is_private_msg};
use serde_json::json;
fn handle_message(event: &serde_json::Value) {
if is_group_msg(event) {
println!("收到了一条群聊消息");
} else if is_private_msg(event) {
println!("收到了一条私聊消息");
}
}
use obmsg::{full_match, fuzzy_match, prefix_match, regex_match};
use serde_json::json;
fn process_command(event: &serde_json::Value) {
// 完全匹配
if full_match(event, "/help") {
// 发送帮助文本
}
// 模糊匹配(包含)
if fuzzy_match(event, "你好") {
// 回应问候
}
// 前缀匹配
if let Some(remainder) = prefix_match(event, "/echo ") {
// 回显剩余部分
println!("回显: {}", remainder);
}
// 正则表达式匹配
let matches = regex_match(event, r"^/roll (\d+)d(\d+)$");
for match_groups in matches {
if match_groups.len() >= 3 {
let dice_count = match_groups[1].parse::<i32>().unwrap_or(1);
let dice_sides = match_groups[2].parse::<i32>().unwrap_or(6);
// 掷骰子...
}
}
}
use obmsg::auto_gen_reply;
fn respond_to_message(event: &serde_json::Value) {
if let Ok(json_to_send) = auto_gen_reply(event, "你好,我收到了你的消息!") {
ob_conn.call_api(&json_to_send).await.unwrap();
}
}
is_group_msg(event) - 检查事件是否为群聊消息is_private_msg(event) - 检查事件是否为私聊消息full_match(event, key) - 精确消息匹配fuzzy_match(event, key) - 包含子字符串匹配prefix_match(event, key) - 前缀匹配(返回剩余部分)regex_match(event, regex) - 正则表达式匹配(返回捕获组)group_full_macth(event, key) - 群聊消息的精确匹配
group_fuzzy_match(event, key) - 群聊消息的模糊匹配
group_prefix_match(event, key) - 群聊消息的前缀匹配
group_regex_match(event, regex) - 群聊消息的正则表达式匹配
private_full_macth(event, key) - 私聊消息的精确匹配
private_fuzzy_match(event, key) - 私聊消息的模糊匹配
private_prefix_match(event, key) - 私聊消息的前缀匹配
private_regex_match(event, regex) - 私聊消息的正则表达式匹配
auto_gen_reply(event, msg) - 为给定事件生成适当的JSON响应本项目采用MIT许可证 - 详情请参阅LICENSE文件。