1 unstable release
Uses new Rust 2024
0.1.0 | Mar 13, 2025 |
---|
#500 in Web programming
119 downloads per month
14KB
150 lines
obmsg
用于快速处理onebot协议中的消息事件。
功能特点
- 检测消息类型(群聊或私聊)
- 对消息进行各种模式匹配(精确、模糊、前缀、正则表达式)
- 生成适当的回复JSON结构
安装方法
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();
}
}
API文档
消息类型检测
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文件。
Dependencies
~2.7–4MB
~79K SLoC