Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
支持语音收发
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Aug 12, 2023
1 parent 01406cb commit fd37b39
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ secret:反向http需要的HMAC签名,用来验证上报的数据确实来自

#### send_group_msg 发送群消息

目前支持文字、图片、at、回复、自定义音乐分享、qq/网易云音乐分享(使用[故梦api](https://blog.gumengya.com/api.html))
目前支持文字、图片、at、回复、自定义音乐分享、qq/网易云音乐分享(使用[故梦api](https://blog.gumengya.com/api.html))、语音

#### send_private_msg 发送私聊消息

目前支持文字、图片、回复、自定义音乐分享、qq/网易云音乐分享(使用[故梦api](https://blog.gumengya.com/api.html))
目前支持文字、图片、回复、自定义音乐分享、qq/网易云音乐分享(使用[故梦api](https://blog.gumengya.com/api.html))、语音

#### get_login_info 获取登录号信息

Expand Down Expand Up @@ -173,11 +173,11 @@ set_group_add_request 处理加群邀请(kook的bot被邀请就会同意,不

#### 群消息

目前接收文字、图片、at、回复
目前接收文字、图片、at、回复、语音

#### 私聊消息

目前接收文字、图片、回复
目前接收文字、图片、回复、语音

#### 生命周期

Expand Down
117 changes: 96 additions & 21 deletions src/kook_onebot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl KookOnebot {
}


pub async fn upload_image(&self,uri:&str)-> Result<String, Box<dyn std::error::Error + Send + Sync>> {
async fn upload_asset(&self,uri:&str)-> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let file_bin;
if uri.starts_with("http") {
file_bin = Self::http_post(uri,vec![],&HashMap::new(),false).await?;
Expand Down Expand Up @@ -657,6 +657,37 @@ impl KookOnebot {
return Ok(false);
}


async fn deal_audio_msg(&self,data:&serde_json::Value,msg:&mut String) -> Result<bool, Box<dyn std::error::Error + Send + Sync>> {

let message = data.get("content").ok_or("content not found")?.as_str().ok_or("content not str")?.to_owned();
let err = "get file err";
let js_arr:serde_json::Value = serde_json::from_str(&message)?;
let card_arr = js_arr.as_array().ok_or(err)?;
if card_arr.len() != 1 {
return Ok(false);
}

let md_arr = card_arr.get(0).unwrap().get("modules").ok_or(err)?.as_array().ok_or(err)?;
if md_arr.len() != 1 {
return Ok(false);
}
let obj = md_arr.get(0).unwrap();
let tp = obj.get("type").ok_or(err)?.as_str().ok_or(err)?;
if tp != "audio" {
return Ok(false);
}
if get_json_str(obj, "title") != "" && get_json_str(obj, "cover") != "" {
// 说明是音乐分享,不是语音
return Ok(true);
}
let url = obj.get("src").ok_or(err)?.as_str().ok_or(err)?;
let url_t = crate::cqtool::cq_params_encode(url);
msg.push_str(&format!("[CQ:record,file={},url={}]",url_t,url_t));
return Ok(true);

}

async fn deal_group_message_event(&self,data:&serde_json::Value,user_id:u64) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let group_id_str = data.get("target_id").ok_or("target_id not found")?.as_str().ok_or("target_id not str")?;
let group_id = group_id_str.parse::<u64>()?;
Expand All @@ -668,27 +699,39 @@ impl KookOnebot {
// 获取消息类型
let msg_type = data.get("type").ok_or("type not found")?.as_i64().ok_or("type not i64")?;

let mut msg = String::new();

// 处理卡牌消息
if msg_type == 10 { // 卡牌消息
// 处理群文件上传事件
if self.deal_group_file_upload_event(data,user_id).await? {
return Ok(());
}
}


let mut msg = String::new();
if self.deal_audio_msg(data,&mut msg).await? {
// do nothing
}
else {
// 未知的card
msg.push_str("卡片消息");
}
} else {
// 处理回复
if let Some(quote) = extra.get("quote") {
let rong_id = get_json_str(quote, "rong_id");
let cq_id = crate::msgid_tool::get_cq_msg_id(&rong_id).0;
msg.push_str(&format!("[CQ:reply,id={cq_id}]"));
}

// 处理回复
if let Some(quote) = extra.get("quote") {
let rong_id = get_json_str(quote, "rong_id");
let cq_id = crate::msgid_tool::get_cq_msg_id(&rong_id).0;
msg.push_str(&format!("[CQ:reply,id={cq_id}]"));
// 转为CQ格式
msg.push_str(&kook_msg_to_cq(msg_type,&message)?);
}

// 转为CQ格式
msg.push_str(&kook_msg_to_cq(msg_type,&message)?);
if msg == "" {
return Ok(());
}

// 存msg_id
let raw_msg_id = data.get("msg_id").ok_or("msg_id not found")?.as_str().ok_or("msg_id not str")?;
let msg_id = crate::msgid_tool::add_msg_id(QMessageStruct {raw_ids:vec![raw_msg_id.to_owned()], user_id });

Expand Down Expand Up @@ -724,20 +767,35 @@ impl KookOnebot {
remark: username.to_owned(),
};

// let message_id_str = data.get("msg_id").ok_or("msg_id not found")?.as_str().ok_or("msg_id not str")?;
let msg_type = data.get("type").ok_or("type not found")?.as_i64().ok_or("type not i64")?;

let mut msg = String::new();

// 处理回复
if let Some(quote) = extra.get("quote") {
let rong_id = get_json_str(quote, "rong_id");
let cq_id = crate::msgid_tool::get_cq_msg_id(&rong_id).0;
msg.push_str(&format!("[CQ:reply,id={cq_id}]"));
// 处理卡牌消息
if msg_type == 10 { // 卡牌消息

if self.deal_audio_msg(data,&mut msg).await? {
// do nothing
}
else {
// 未知的card
msg.push_str("卡片消息");
}
}else {
// 处理回复
if let Some(quote) = extra.get("quote") {
let rong_id = get_json_str(quote, "rong_id");
let cq_id = crate::msgid_tool::get_cq_msg_id(&rong_id).0;
msg.push_str(&format!("[CQ:reply,id={cq_id}]"));
}

// 转为CQ格式
msg.push_str(&kook_msg_to_cq(msg_type,&message)?);
}

// 转为CQ格式
msg.push_str(&kook_msg_to_cq(msg_type,&message)?);
if msg == "" {
return Ok(());
}

let raw_msg_id = data.get("msg_id").ok_or("msg_id not found")?.as_str().ok_or("msg_id not str")?;
let msg_id = crate::msgid_tool::add_msg_id(QMessageStruct {raw_ids:vec![raw_msg_id.to_owned()], user_id });
Expand Down Expand Up @@ -936,7 +994,7 @@ impl KookOnebot {
}
} else if tp == "image"{
let file = it.get("data").ok_or("data not found")?.get("file").ok_or("file not found")?.as_str().ok_or("file not str")?;
let file_url = self.upload_image(file).await?;
let file_url = self.upload_asset(file).await?;
to_send_data.push((2,file_url));
last_type = 2;
}
Expand Down Expand Up @@ -1045,6 +1103,23 @@ impl KookOnebot {
last_type = 10;
}
}
else if tp == "record" {
let data = it.get("data").ok_or("data not found")?;
let file = get_json_str(data, "file");
let url = self.upload_asset(&file).await?;
let js = serde_json::json!([{
"type": "card",
"theme": "secondary",
"size": "lg",
"modules": [
{
"type": "audio",
"src": url,
}]
}]);
to_send_data.push((10,js.to_string()));
last_type = 10;
}
else {
let j = serde_json::json!([it]);
let s = arr_to_cq_str(&j)?;
Expand Down Expand Up @@ -1367,7 +1442,7 @@ impl KookOnebot {
"retcode":0,
"data": {
"app_name":"kook-onebot",
"app_version":"0.0.8",
"app_version":"0.0.9",
"protocol_version":"v11"
},
"echo":echo
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use kook_onebot::KookOnebot;
use time::UtcOffset;
use ::time::format_description;
use tokio::sync::RwLock;
use hyper::{service::make_service_fn};
use hyper::service::make_service_fn;
use crate::config_tool::read_config;


Expand Down Expand Up @@ -81,7 +81,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
)).with_max_level(tracing::Level::INFO)
.init();

log::warn!("欢迎使用KookOnebot by super1207!!! v0.0.8");
log::warn!("欢迎使用KookOnebot by super1207!!! v0.0.9");

log::warn!("开源地址:https://github.com/super1207/KookOneBot");

Expand Down

0 comments on commit fd37b39

Please sign in to comment.