Compare commits

..

10 Commits

Author SHA1 Message Date
linyuchen
0a3ae76b89 v1.2.7 2024-01-20 23:24:49 +08:00
linyuchen
ddd60a6a79 fix voice record type 2024-01-20 23:24:24 +08:00
linyuchen
e4d8c5e72e Merge remote-tracking branch 'origin/main' 2024-01-20 22:28:03 +08:00
linyuchen
6d33fb8b14 check gif 2024-01-20 08:38:14 +08:00
linyuchen
2350e4dc75 Update README.md 2024-01-13 18:27:30 +08:00
linyuchen
600addbf82 fix: 打开插件设置界面导致插件多次监听 2023-12-14 02:10:30 +08:00
linyuchen
f07f0111cd perf: auto remove send files 2023-12-12 20:45:54 +08:00
linyuchen
923f72e5d3 perf: message data checker 2023-12-09 16:59:16 +08:00
linyuchen
5b4001e411 Merge pull request #2 from YDHusky/main
修改发送消息返回数据
2023-12-09 14:46:16 +08:00
husky
b950f01d51 修改发送消息返回数据 2023-12-08 23:24:22 +08:00
10 changed files with 162 additions and 56 deletions

View File

@@ -56,3 +56,6 @@
![](doc/image/example.jpg)
*暂时不支持`"message": "hello"`这种message为字符串的形式*
## onebot11文档
<https://11.onebot.dev/>

View File

@@ -4,7 +4,7 @@
"name": "LLOneBot",
"slug": "LLOneBot",
"description": "LiteLoaderQQNT的OneBotApi",
"version": "1.2.3",
"version": "1.2.7",
"thumbnail": "./icon.png",
"author": {
"name": "linyuchen",

View File

@@ -8,4 +8,6 @@ export const CHANNEL_UPDATE_FRIENDS = "llonebot_update_friends"
export const CHANNEL_LOG = "llonebot_log"
export const CHANNEL_POST_ONEBOT_DATA = "llonebot_post_onebot_data"
export const CHANNEL_SET_SELF_INFO= "llonebot_set_self_info"
export const CHANNEL_DOWNLOAD_FILE= "llonebot_download_file"
export const CHANNEL_DOWNLOAD_FILE= "llonebot_download_file"
export const CHANNEL_DELETE_FILE= "llonebot_delete_file"
export const CHANNEL_GET_RUNNING_STATUS= "llonebot_get_running_status"

View File

@@ -114,7 +114,7 @@ export type SendMessage = {
text: string, // 纯文本
}
} | {
type: "image" | "voice",
type: "image" | "voice" | "record",
file: string, // 本地路径
data?: {
file: string // 本地路径

2
src/global.d.ts vendored
View File

@@ -44,6 +44,8 @@ declare var llonebot: {
getConfig():Promise<Config>;
setSelfInfo(selfInfo: SelfInfo):void;
downloadFile(arg: {uri: string, localFilePath: string}):Promise<string>;
deleteFile(path: string[]):Promise<void>;
getRunningStatus(): Promise<boolean>;
};
declare global {

View File

@@ -1,10 +1,49 @@
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
const express = require("express");
const bodyParser = require('body-parser');
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg} from "../common/types";
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg, SendMessage} from "../common/types";
import {friends, groups, selfInfo} from "./data";
// @SiberianHusky 2021-08-15
function checkSendMessage(sendMsgList: SendMessage[]) {
function checkUri(uri: string): boolean {
const pattern = /^(file:\/\/|http:\/\/|https:\/\/|base64:\/\/)/;
return pattern.test(uri);
}
for (let msg of sendMsgList) {
if (msg["type"] && msg["data"]) {
let type = msg["type"];
let data = msg["data"];
if (type === "text" && !data["text"]) {
return 400;
} else if (["image", "voice"].includes(type)) {
if (!data["file"]) {
return 400;
}
else{
if (checkUri(data["file"])) {
return 200;
}
else{
return 400;
}
}
} else if (type === "at" && !data["qq"]) {
return 400;
} else if (type === "reply" && !data["id"]) {
return 400;
}
}
else{
return 400
}
}
return 200;
}
// ==end==
function handlePost(jsonData: any) {
if (!jsonData.params) {
jsonData.params = jsonData
@@ -21,11 +60,20 @@ function handlePost(jsonData: any) {
} else if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
if (jsonData.action == "send_private_msg") {
jsonData.message_type = "private"
}
else {
} else {
jsonData.message_type = "group"
}
sendIPCSendQQMsg(jsonData);
// @SiberianHuskY 2021-10-20 22:00:00
resData.status = checkSendMessage(jsonData.message);
if (resData.status == 200) {
resData.message = "发送成功";
resData.data = jsonData.message;
sendIPCSendQQMsg(jsonData);
} else {
resData.message = "发送失败, 请检查消息格式";
resData.data = jsonData.message;
}
// == end ==
} else if (jsonData.action == "get_group_list") {
resData["data"] = groups.map(group => {
return {
@@ -41,8 +89,7 @@ function handlePost(jsonData: any) {
})
}
})
}
else if (jsonData.action == "get_group_info") {
} else if (jsonData.action == "get_group_info") {
let group = groups.find(group => group.uid == jsonData.params.group_id)
if (group) {
resData["data"] = {
@@ -51,10 +98,9 @@ function handlePost(jsonData: any) {
member_count: group.members.length,
}
}
}
else if (jsonData.action == "get_group_member_info") {
} else if (jsonData.action == "get_group_member_info") {
let member = groups.find(group => group.uid == jsonData.params.group_id)?.members?.find(member => member.uin == jsonData.params.user_id)
resData["data"] ={
resData["data"] = {
user_id: member.uin,
user_name: member.nick,
user_display_name: member.cardName || member.nick,
@@ -62,8 +108,7 @@ function handlePost(jsonData: any) {
card: member.cardName,
role: OnebotGroupMemberRole[member.role],
}
}
else if (jsonData.action == "get_group_member_list") {
} else if (jsonData.action == "get_group_member_list") {
let group = groups.find(group => group.uid == jsonData.params.group_id)
if (group) {
resData["data"] = group?.members?.map(member => {
@@ -109,6 +154,7 @@ export function startExpress(port: number) {
res.send(resData)
});
}
const actionList: PostDataAction[] = ["get_login_info", "send_private_msg", "send_group_msg",
"get_group_list", "get_friend_list", "delete_msg", "get_group_member_list", "get_group_member_info"]

View File

@@ -13,19 +13,21 @@ import {
CHANNEL_SET_CONFIG,
CHANNEL_START_HTTP_SERVER,
CHANNEL_UPDATE_FRIENDS,
CHANNEL_UPDATE_GROUPS
CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS
} from "../common/IPCChannel";
import {ConfigUtil} from "./config";
import {startExpress} from "./HttpServer";
import {log} from "./utils";
import {isGIF, log} from "./utils";
import {friends, groups, selfInfo} from "./data";
const fs = require('fs');
let running = false;
// 加载插件时触发
function onLoad(plugin: any) {
log("main onLoaded");
function getConfigUtil() {
const configFilePath = path.join(plugin.path.data, `config_${selfInfo.user_id}.json`)
return new ConfigUtil(configFilePath)
@@ -53,7 +55,10 @@ function onLoad(plugin: any) {
let buffer = await blob.arrayBuffer();
fs.writeFileSync(arg.localFilePath, Buffer.from(buffer));
}
// todo: 需要识别gif格式
if (isGIF(arg.localFilePath)) {
fs.renameSync(arg.localFilePath, arg.localFilePath + ".gif");
arg.localFilePath += ".gif";
}
return arg.localFilePath;
})
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
@@ -122,6 +127,17 @@ function onLoad(plugin: any) {
ipcMain.handle(CHANNEL_SET_SELF_INFO, (event: any, arg: SelfInfo) => {
selfInfo.user_id = arg.user_id;
selfInfo.nickname = arg.nickname;
running = true;
})
ipcMain.on(CHANNEL_DELETE_FILE, (event: any, arg: string[]) => {
for (const path of arg) {
fs.unlinkSync(path);
}
})
ipcMain.handle(CHANNEL_GET_RUNNING_STATUS, (event: any, arg: any) => {
return running;
})
}

View File

@@ -5,4 +5,12 @@ export function log(msg: any) {
fs.appendFile("./llonebot.log", currentDateTime + ":" + msg + "\n", (err: any) => {
})
}
export function isGIF(path: string) {
const buffer = Buffer.alloc(4);
const fd = fs.openSync(path, 'r');
fs.readSync(fd, buffer, 0, 4, 0);
fs.closeSync(fd);
return buffer.toString() === 'GIF8'
}

View File

@@ -3,10 +3,18 @@
import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types";
import {
CHANNEL_DOWNLOAD_FILE,
CHANNEL_GET_CONFIG, CHANNEL_SET_SELF_INFO, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG,
CHANNEL_GET_CONFIG,
CHANNEL_SET_SELF_INFO,
CHANNEL_LOG,
CHANNEL_POST_ONEBOT_DATA,
CHANNEL_RECALL_MSG,
CHANNEL_SEND_MSG,
CHANNEL_SET_CONFIG,
CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS, CHANNEL_UPDATE_GROUPS
CHANNEL_START_HTTP_SERVER,
CHANNEL_UPDATE_FRIENDS,
CHANNEL_UPDATE_GROUPS,
CHANNEL_DELETE_FILE,
CHANNEL_GET_RUNNING_STATUS
} from "./common/IPCChannel";
@@ -50,8 +58,14 @@ contextBridge.exposeInMainWorld("llonebot", {
setSelfInfo(selfInfo: SelfInfo){
ipcRenderer.invoke(CHANNEL_SET_SELF_INFO, selfInfo)
},
downloadFile: async (arg: {uri: string, localFilePath: string}) => {
downloadFile: (arg: {uri: string, localFilePath: string}) => {
return ipcRenderer.invoke(CHANNEL_DOWNLOAD_FILE, arg);
},
deleteFile: async (localFilePath: string[]) => {
ipcRenderer.send(CHANNEL_DELETE_FILE, localFilePath);
},
getRunningStatus: () => {
return ipcRenderer.invoke(CHANNEL_GET_RUNNING_STATUS);
}
// startExpress,
});

View File

@@ -4,6 +4,7 @@
// const { ipcRenderer } = require('electron');
import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types";
import * as stream from "stream";
import {raw} from "express";
let self_qq: string = ""
let groups: Group[] = []
@@ -20,7 +21,7 @@ async function getUserInfo(uid: string): Promise<User> {
return user
}
async function getFriends(){
async function getFriends() {
let _friends = await window.LLAPI.getFriendsList(false)
for (let friend of _friends) {
let existFriend = friends.find(f => f.uin == friend.uin)
@@ -30,6 +31,8 @@ async function getFriends(){
}
window.llonebot.updateFriends(friends)
return friends
}
async function getFriend(qq: string) {
@@ -210,6 +213,7 @@ async function listenSendMessage(postData: PostDataSendMsg) {
}
}
if (peer) {
let sendFiles: string[] = [];
for (let message of postData.params.message) {
if (message.type == "at") {
// @ts-ignore
@@ -223,25 +227,26 @@ async function listenSendMessage(postData: PostDataSendMsg) {
message.content = `@${atMember.cardName || atMember.nick}`
} else if (message.type == "text") {
message.content = message.data?.text || message.content
} else if (message.type == "image" || message.type == "voice") {
} else if (message.type == "image" || message.type == "voice" || message.type == "record") {
// todo: 收到的应该是uri格式的需要转成本地的, uri格式有三种http, file, base64
let url = message.data?.file || message.file
let uri = new URL(url);
let ext: string;
if (message.type == "image") {
// todo: 需要识别gif格式
ext = ".png"
}
if (message.type == "voice") {
if (message.type == "voice" || message.type == "record") {
message.type = "voice"
ext = ".amr"
}
let localFilePath = `${Date.now()}${ext}`
if (uri.protocol == "file:") {
localFilePath = url.split("file://")[1]
} else {
await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath})
localFilePath = await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath})
}
message.file = localFilePath
sendFiles.push(localFilePath);
} else if (message.type == "reply") {
let msgId = message.data?.id || message.msgId
let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId)
@@ -249,9 +254,13 @@ async function listenSendMessage(postData: PostDataSendMsg) {
message.msgSeq = replyMessage?.raw.msgSeq || ""
}
}
// 发送完之后要删除下载的文件
console.log("发送消息", postData)
window.LLAPI.sendMessage(peer, postData.params.message).then(res => console.log("消息发送成功:", peer, postData.params.message),
window.LLAPI.sendMessage(peer, postData.params.message).then(res => {
console.log("消息发送成功:", peer, postData.params.message)
if (sendFiles.length) {
window.llonebot.deleteFile(sendFiles);
}
},
err => console.log("消息发送失败", postData, err))
}
}
@@ -264,14 +273,11 @@ function recallMessage(msgId: string) {
let chatListEle: HTMLCollectionOf<Element>
function onLoad() {
window.llonebot.listenSendMessage((postData: PostDataSendMsg) => {
listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err))
})
window.llonebot.listenRecallMessage((arg: { message_id: string }) => {
recallMessage(arg.message_id)
})
async function onLoad(arg: any) {
let runningStatus = await window.llonebot.getRunningStatus();
if (runningStatus) {
return;
}
async function getGroupsMembers(groupsArg: Group[]) {
// 批量获取群成员列表
@@ -288,9 +294,12 @@ function onLoad() {
getGroupsMembers(failedGroups).then()
}, 1000)
} else {
console.log("全部群成员获取完毕", groups)
window.llonebot.log("全部群成员获取完毕")
}
}
await getFriends();
await getGroups();
await getGroupsMembers(groups);
function onNewMessages(messages: MessageElement[]) {
async function func(messages: MessageElement[]) {
@@ -303,25 +312,31 @@ function onLoad() {
func(messages).then(() => {
})
console.log("chatListEle", chatListEle)
// console.log("chatListEle", chatListEle)
}
getFriends().then();
getGroups().then(() => {
getGroupsMembers(groups).then(() => {
window.LLAPI.on("new-messages", onNewMessages);
window.LLAPI.on("new-send-messages", onNewMessages);
})
})
window.LLAPI.on("new-messages", onNewMessages);
window.LLAPI.on("new-send-messages", onNewMessages);
window.LLAPI.getAccountInfo().then(accountInfo => {
window.LLAPI.getUserInfo(accountInfo.uid).then(userInfo => {
window.llonebot.setSelfInfo({
user_id: accountInfo.uin,
nickname: userInfo.nickName
});
window.llonebot.startExpress();
})
let accountInfo = await window.LLAPI.getAccountInfo();
window.llonebot.log("getAccountInfo " + JSON.stringify(accountInfo));
if (!accountInfo.uid) {
return;
}
let selfInfo = await window.LLAPI.getUserInfo(accountInfo.uid);
window.llonebot.setSelfInfo({
user_id: accountInfo.uin,
nickname: selfInfo.nickName
});
window.llonebot.log("selfInfo " + JSON.stringify(selfInfo))
window.llonebot.startExpress();
window.llonebot.listenSendMessage((postData: PostDataSendMsg) => {
listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err))
})
window.llonebot.listenRecallMessage((arg: { message_id: string }) => {
recallMessage(arg.message_id)
})
window.llonebot.log("llonebot loaded");
window.LLAPI.add_qmenu((qContextMenu: Node) => {
let btn = document.createElement("a")