Skip to content

Commit

Permalink
release v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
EluvK committed Mar 10, 2023
1 parent fc8f5a2 commit 2a009fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Rust 实现,抽象了 cqhttp 的收发消息、openai ai 的消息对象,具

``` BASH
$./qbot --help
Usage: qbot [OPTIONS] <API_KEY>
Usage: qbot [OPTIONS] <API_KEY> <QQ>

Arguments:
<API_KEY> openai api key
<API_KEY> openai api key, start with `sk-`
<QQ> bot qq, to determined if @ bot

Options:
-w, --websocket <WEBSOCKET> cqhttp websocket address, default value is `ws://localhost:8080/ws`
Expand Down
9 changes: 5 additions & 4 deletions src/cqbot/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

pub struct Bot {
_qq: i64,
qq: u64,
proxy_addr: Option<String>,
api_key: String,
web_socket_stream: Box<WebSocketStream<MaybeTlsStream<TcpStream>>>,
Expand All @@ -20,10 +20,10 @@ pub struct Bot {
static OPENAIAPIURL: &str = "https://api.openai.com/v1/chat/completions";

impl Bot {
pub async fn new(url: &str, proxy: Option<String>, api_key: String) -> Self {
pub async fn new(url: &str, proxy: Option<String>, api_key: String, qq: u64) -> Self {
let (wss, _) = connect_async(url).await.expect("Fail to connect");
Self {
_qq: 0,
qq,
proxy_addr: proxy,
api_key,
web_socket_stream: Box::new(wss),
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Bot {
if let Message::Text(text) = message {
// println!("Handle Text:{}", &text);
let msg = serde_json::from_str::<RecvMsg>(&text).ok()?;
let (is_at, msg) = msg.pre_parse_msg();
let (is_at, msg) = msg.pre_parse_msg(self.qq);
println!("is_at:{:?}, RecvMessage:{:?}", is_at, &msg);
match msg.message_type() {
MessageType::Group => {
Expand Down Expand Up @@ -222,6 +222,7 @@ mod tests {
"ws://localhost:8080/ws",
Some("socks5h://127.0.0.1:1080".into()),
"sk-xx".into(),
222,
)
.await;
let return_message = bot
Expand Down
17 changes: 9 additions & 8 deletions src/cqbot/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ impl RecvMsg {
}

/// return (if is a at msg, clean msg field's cq code)
pub fn pre_parse_msg(mut self) -> (bool, Self) {
lazy_static! {
static ref AT_MSG_RE: Regex = Regex::new(r#"CQ:at,qq=[0-9]*"#).unwrap();
}
pub fn pre_parse_msg(mut self, bot_id: u64) -> (bool, Self) {
// lazy_static! {
// static ref AT_MSG_RE: Regex = Regex::new(r#"CQ:at,qq=[0-9]*"#).unwrap();
// }
let at_msg_re = Regex::new(format!("CQ:at,qq={}", bot_id).as_str()).unwrap();
let mut is_at_msg = false;
if AT_MSG_RE.is_match(&self.raw_message) {
if at_msg_re.is_match(&self.raw_message) {
is_at_msg = true;
}

Expand Down Expand Up @@ -170,16 +171,16 @@ mod tests {
let msg_str = r#"{"post_type":"message","message_type":"group","time":1678336087,"self_id":222,"sub_type":"normal","anonymous":null,"group_id":777,"raw_message":"[CQ:at,qq=222] 1","sender":{"age":0,"area":"","card":"","level":"","nickname":"Mr.Eucalypt","role":"member","sex":"unknown","title":"","user_id":999},"user_id":999,"message_id":1206430729,"font":0,"message":"[CQ:at,qq=222] 1","message_seq":134}"#;
let recv_msg = serde_json::from_str::<RecvMsg>(msg_str).unwrap();
println!("{:?}", recv_msg);
println!("{:?}", recv_msg.pre_parse_msg());
println!("{:?}", recv_msg.pre_parse_msg(222));

let msg_str_2 = r#"{"post_type":"message","message_type":"private","time":1678336074,"self_id":222,"sub_type":"friend","target_id":222,"message":"柠檬茶 1","raw_message":"柠檬茶 1","font":0,"sender":{"age":0,"nickname":"Mr.Eucalypt","sex":"unknown","user_id":999},"message_id":-111094286,"user_id":999}"#;
let recv_msg_2 = serde_json::from_str::<RecvMsg>(msg_str_2).unwrap();
println!("{:?}", recv_msg_2);
println!("{:?}", recv_msg_2.pre_parse_msg());
println!("{:?}", recv_msg_2.pre_parse_msg(222));

let msg_str_3 = r#"{"post_type":"message","message_type":"private","time":1678334227,"self_id":222,"sub_type":"friend","sender":{"age":0,"nickname":"Mr.Eucalypt","sex":"unknown","user_id":999},"message_id":67453260,"user_id":999,"target_id":222,"message":"[CQ:image,file=7ecb49dcfcdcca29feb728cf8811bb37.image,url=https://c2cpicdw.qpic.cn/offpic_new/999//999-3676750358-7ECB49DCFCDCCA29FEB728CF8811BB37/0?term=2\u0026amp;is_origin=0]","raw_message":"[CQ:image,file=7ecb49dcfcdcca29feb728cf8811bb37.image,url=https://c2cpicdw.qpic.cn/offpic_new/999//999-3676750358-7ECB49DCFCDCCA29FEB728CF8811BB37/0?term=2\u0026amp;is_origin=0]","font":0}"#;
let recv_msg_3 = serde_json::from_str::<RecvMsg>(msg_str_3).unwrap();
println!("{:?}", recv_msg_3);
println!("{:?}", recv_msg_3.pre_parse_msg());
println!("{:?}", recv_msg_3.pre_parse_msg(222));
}
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ struct Args {

/// openai api key, start with `sk-`
api_key: String,

/// bot qq, to determined if @ bot
qq: u64,
}

#[tokio::main]
async fn main() {
let args = Args::parse();
let cqhttp_ws = args.websocket.unwrap_or("ws://localhost:8080/ws".into());

let mut bot = Bot::new(&cqhttp_ws, args.proxy, args.api_key).await;
let mut bot = Bot::new(&cqhttp_ws, args.proxy, args.api_key, args.qq).await;

bot.loop_read().await;
}

0 comments on commit 2a009fe

Please sign in to comment.