Skip to content

Commit

Permalink
llonebot poke,llsatori get_msg、face
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Oct 13, 2024
1 parent b6641b5 commit a2c3139
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 10 deletions.
78 changes: 70 additions & 8 deletions src/botconn/onebot11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hyper::header::HeaderValue;
use tokio::net::TcpStream;
use tokio_tungstenite::{tungstenite, connect_async};

use crate::{cqapi::{cq_add_log, cq_add_log_w, cq_get_app_directory1}, mytool::read_json_str, RT_PTR};
use crate::{cqapi::{cq_add_log, cq_add_log_w, cq_get_app_directory1}, mytool::{read_json_str, str_msg_to_arr}, RT_PTR};

use super::BotConnectTrait;

Expand Down Expand Up @@ -72,10 +72,29 @@ impl OneBot11Connect {
}
}

async fn get_poke_segment(&self,json: &mut serde_json::Value) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let platform = self.get_platform().await?;
if !(platform == "lagrange" || platform == "llonebot" || platform == "napcat") {
return Ok("".to_owned());
}
let mut ret_id = "".to_owned();
let params = json.get_mut("params").ok_or("params is none")?;
if let Some(message) = params.get_mut("message") {
let msg_arr = message.as_array_mut().ok_or("message is not array")?;
if msg_arr.len() == 1 {
let tp = read_json_str(&message[0], "type");
if tp == "poke" {
ret_id = read_json_str(&message[0]["data"], "id");
}
}
}
Ok(ret_id)
}

async fn deal_music_segment(&self,json: &mut serde_json::Value) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {

let platform = self.get_platform().await?;
if platform != "qq" {
if !(platform == "lagrange" || platform == "llonebot" || platform == "cqhttp" || platform == "napcat") {
return Ok(());
}

Expand Down Expand Up @@ -163,23 +182,27 @@ impl OneBot11Connect {
let self_id = self.self_id.read().unwrap().to_owned();
let ret = self.call_api("onebot11", &self_id, "", &mut send_json).await?;
let platform = ret["data"]["app_name"].as_str().unwrap_or("").to_ascii_lowercase();
if platform.contains("lagrange") || platform.contains("llonebot") || platform.contains("cqhttp") || platform.contains("napcat") {
*self.real_platform.write().unwrap() = Some("qq".to_string());
if platform.contains("lagrange") {
*self.real_platform.write().unwrap() = Some("lagrange".to_string());
} else if platform.contains("llonebot") {
*self.real_platform.write().unwrap() = Some("lagrange".to_string());
} else if platform.contains("cqhttp") {
*self.real_platform.write().unwrap() = Some("cqhttp".to_string());
} else if platform.contains("napcat") {
*self.real_platform.write().unwrap() = Some("napcat".to_string());
} else {
*self.real_platform.write().unwrap() = Some("".to_string());
}
}
let read_platform = self.real_platform.read().unwrap().to_owned();
if let Some(platform) = read_platform {
if platform == "qq" {
return Ok("qq".to_string());
}
return Ok(platform);
}
return Ok("".to_string());
}
pub async fn get_avatar(&self,user_id:&str) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let platform = self.get_platform().await?;
if platform == "qq" {
if platform == "lagrange" || platform == "llonebot" || platform == "cqhttp" || platform == "napcat" {
return Ok(format!("https://thirdqq.qlogo.cn/g?b=qq&nk={user_id}&s=640"));
}
return Err("can't get avatar".into());
Expand Down Expand Up @@ -466,7 +489,46 @@ impl BotConnectTrait for OneBot11Connect {
}

if action == "send_msg" || action == "send_group_msg" || action == "send_private_msg" {
let message = json["params"].get_mut("message").ok_or("not found message segment")?;
if message.is_string() {
let message_arr = str_msg_to_arr(message).map_err(|x|{
format!("str_msg_to_arr err:{:?}",x)
})?;
*message = message_arr;
}
self.deal_music_segment(json).await?;
let to_poke = self.get_poke_segment(json).await?;
if to_poke != "" {
let group_id = read_json_str(&json["params"], "group_id");
if group_id != "" {
let group_id = &json["params"]["group_id"];
let mut to_send = serde_json::json!({
"action":"group_poke",
"params":{
"group_id": group_id,
"user_id": to_poke.parse::<u64>()?
}
});
self.call_api("", "", "", &mut to_send).await?;
}else {
let mut to_send = serde_json::json!({
"action":"friend_poke",
"params":{
"user_id": to_poke.parse::<u64>()?
}
});
self.call_api("", "", "", &mut to_send).await?;
}
let ret_json = serde_json::json!({
"status":"ok",
"retcode":0,
"data": {
"message_id":crate::redlang::get_random()?.to_string()
},
"echo":echo
});
return Ok(ret_json);
}
}

let (tx_ay, mut rx_ay) = tokio::sync::mpsc::channel::<serde_json::Value>(1);
Expand Down
98 changes: 96 additions & 2 deletions src/botconn/satoriv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Satoriv1Connect {
}
}

fn satori_msg_to_cq_msg(html:&str,channel_id:&str) -> Result<String,Box<dyn std::error::Error + Send + Sync>> {
fn satori_msg_to_cq_msg(html:&str,channel_id:&str,platform:&str) -> Result<String,Box<dyn std::error::Error + Send + Sync>> {
let mut out = String::new();
let childen = pre_parse_satori_msg(html)?;

Expand Down Expand Up @@ -176,6 +176,13 @@ impl Satoriv1Connect {
let text = html_escape::decode_html_entities(&text_str);
out += &cq_text_encode(&text);
}
else if child.tag_name == "face" {
if platform == "llonebot" {
let face_id = child.attr_map.get("id").ok_or("No id at llonebot face element")?;
let face_id_t = cq_params_encode(&face_id);
out += &format!("[CQ:face,id={face_id_t}]");
}
}
}
Ok(out)
}
Expand Down Expand Up @@ -228,6 +235,12 @@ impl Satoriv1Connect {
}

}
else if tp == "face" {
if platform == "llonebot" {
let id: &str = it.get("data").ok_or("data not found")?.get("id").ok_or("id not found")?.as_str().ok_or("id not a string")?;
out += &format!("<face id={} />", serde_json::json!(id));
}
}
}
Ok(out)
}
Expand Down Expand Up @@ -296,7 +309,7 @@ impl Satoriv1Connect {
let content = read_json_str(message, "content");
let channel = body.get("channel").ok_or("channel 不存在")?; // 没有channel就无法回复
let channel_id =read_json_str(channel, "id");
let cq_msg = Self::satori_msg_to_cq_msg(&content,&channel_id)?;
let cq_msg = Self::satori_msg_to_cq_msg(&content,&channel_id,&platform)?;
if guild_opt.is_some(){ //group
if user_id == self_id { //机器人自己的消息,忽略
return Ok(());
Expand Down Expand Up @@ -733,6 +746,84 @@ impl Satoriv1Connect {
}));

}
async fn get_msg(self_t:&Satoriv1Connect,json:&serde_json::Value,platform:&str,self_id:&str) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync>> {

let params = read_json_obj_or_null(json, "params");

let new_message_id = read_json_str(&params, "message_id");

let t = new_message_id.split("b73d7536-d8fa-4dda-b194-4acc51898a91").collect::<Vec<&str>>();
let message_id = t.get(0).ok_or("can't get message_id")?;
let channel_id = t.get(1).ok_or("can't get channel_id")?;

let to_send = serde_json::json!({
"channel_id":channel_id,
"message_id":message_id,
});

let satori_msg = http_post(&format!("{}/message.get",self_t.http_url),platform,self_id,&self_t.token,&to_send).await?;

let content = satori_msg["content"].as_str().ok_or("not found content in satori message")?;

let cq_message = Self::satori_msg_to_cq_msg(content,channel_id,platform)?;
let created_at_str = read_json_str(&satori_msg, "created_at");
let mut created_at = 0;
if created_at_str != "" {
created_at = created_at_str.parse::<u64>()?;
}
let guild = &satori_msg["guild"];
let mut message_type = "group".to_owned();
if !guild.is_object() || guild.as_object().unwrap().is_empty() {
message_type = "private".to_owned();
}

let user = &satori_msg["user"];
let sender;
if user.is_object() && !user.as_object().unwrap().is_empty() {
if message_type == "group" {
sender = serde_json::json!({
"user_id":read_json_str(&user, "id"),
"nickname":read_json_str(&user, "name"),
"card":read_json_str(&user, "nick"),
"sex":"unknown",
"age":0,
"area":"",
"level":"0",
"role":"member",
"title":""
});
}else {
let nick = read_json_str(&user, "nick");
let name = read_json_str(&user, "name");
let nickname;
if nick != "" {
nickname = nick;
}else {
nickname = name;
}
sender = serde_json::json!({
"user_id":read_json_str(&user, "id"),
"nickname":nickname,
"sex":"unknown",
"age":0,
});
}
}else {
sender = serde_json::Value::Null;
}
return Ok(serde_json::json!({
"retcode":0,
"status":"ok",
"data":{
"time":created_at,
"message_type":message_type,
"message_id":new_message_id,
"sender":sender,
"message":cq_message
}
}));

}
}


Expand Down Expand Up @@ -921,6 +1012,9 @@ impl BotConnectTrait for Satoriv1Connect {
else if action == "delete_msg" {
return Self::delete_msg(self,json,platform,self_id).await;
}
else if action == "get_msg" {
return Self::get_msg(self,json,platform,self_id).await;
}
return Ok(serde_json::json!({
"retcode":1404,
"status":"failed"
Expand Down

0 comments on commit a2c3139

Please sign in to comment.