mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
feature: image|voice support uri
This commit is contained in:
parent
38e198c902
commit
3d3974d0c7
@ -6,4 +6,6 @@ export const CHANNEL_START_HTTP_SERVER = "llonebot_start_http_server"
|
||||
export const CHANNEL_UPDATE_GROUPS = "llonebot_update_groups"
|
||||
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_POST_ONEBOT_DATA = "llonebot_post_onebot_data"
|
||||
export const CHANNEL_GET_SELF_INFO= "llonebot_get_self_info"
|
||||
export const CHANNEL_DOWNLOAD_FILE= "llonebot_download_file"
|
@ -17,6 +17,18 @@ export type GroupMemberInfo = {
|
||||
uin: string; // QQ号
|
||||
}
|
||||
|
||||
export const OnebotGroupMemberRole = {
|
||||
4: 'owner',
|
||||
3: 'admin',
|
||||
2: 'member'
|
||||
}
|
||||
|
||||
|
||||
export type SelfInfo = {
|
||||
user_id: string;
|
||||
nickname: string;
|
||||
}
|
||||
|
||||
export type User = {
|
||||
avatarUrl?: string;
|
||||
bio?: string; // 签名
|
||||
@ -126,8 +138,11 @@ export type SendMessage = {
|
||||
}
|
||||
}
|
||||
|
||||
export type PostDataAction = "send_private_msg" | "send_group_msg" | "get_group_list"
|
||||
| "get_friend_list" | "delete_msg" | "get_login_info"
|
||||
|
||||
export type PostDataSendMsg = {
|
||||
action: "send_private_msg" | "send_group_msg" | "get_group_list" | "get_friend_list" | "delete_msg",
|
||||
action: PostDataAction
|
||||
message_type?: "private" | "group"
|
||||
params?: {
|
||||
user_id: string,
|
||||
|
21
src/global.d.ts
vendored
21
src/global.d.ts
vendored
@ -1,16 +1,25 @@
|
||||
import {Config, Group, GroupMemberInfo, MessageElement, Peer, PostDataSendMsg, SendMessage, User} from "./common/types";
|
||||
import {
|
||||
Config,
|
||||
Group,
|
||||
GroupMemberInfo,
|
||||
MessageElement,
|
||||
Peer,
|
||||
PostDataSendMsg,
|
||||
SelfInfo,
|
||||
SendMessage,
|
||||
User
|
||||
} from "./common/types";
|
||||
|
||||
|
||||
declare var LLAPI: {
|
||||
on(event: "new-messages" | "new-send-messages", callback: (data: MessageElement[]) => void): void;
|
||||
on(event: "context-msg-menu", callback: (event: any, target: any, msgIds:any) => void): void;
|
||||
getAccountInfo(): Promise<{
|
||||
uid: string // qq
|
||||
uin: string // 一串加密的字符串
|
||||
uid: string // 一串加密的字符串
|
||||
uin: string // qq
|
||||
}>
|
||||
|
||||
// uid是一串加密的字符串
|
||||
getUserInfo(uid: string): Promise<User>;
|
||||
getUserInfo(uid: string): Promise<User>; // uid是一串加密的字符串
|
||||
sendMessage(peer: Peer, message: SendMessage[]): Promise<any>;
|
||||
recallMessage(peer: Peer, msgIds: string[]): Promise<void>;
|
||||
getGroupsList(forced: boolean): Promise<Group[]>
|
||||
@ -33,6 +42,8 @@ declare var llonebot: {
|
||||
log(data: any): void,
|
||||
setConfig(config: Config):void;
|
||||
getConfig():Promise<Config>;
|
||||
setSelfInfo(selfInfo: SelfInfo):void;
|
||||
downloadFile(arg: {uri: string, localFilePath: string}):Promise<string>;
|
||||
};
|
||||
|
||||
declare global {
|
||||
|
@ -1,8 +1,8 @@
|
||||
import {sendIPCRecallQQMsg, sendIPCSendQQMsg} from "./IPCSend";
|
||||
|
||||
const express = require("express");
|
||||
import {PostDataSendMsg} from "../common/types";
|
||||
import {friends, groups} from "./data";
|
||||
import {OnebotGroupMemberRole, PostDataAction, PostDataSendMsg} from "../common/types";
|
||||
import {friends, groups, selfInfo} from "./data";
|
||||
|
||||
function handlePost(jsonData: any) {
|
||||
let resData = {
|
||||
@ -11,13 +11,16 @@ function handlePost(jsonData: any) {
|
||||
data: {},
|
||||
message: ''
|
||||
}
|
||||
if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
|
||||
if (jsonData.action == "get_login_info") {
|
||||
resData["data"] = selfInfo
|
||||
} else if (jsonData.action == "send_private_msg" || jsonData.action == "send_group_msg") {
|
||||
sendIPCSendQQMsg(jsonData);
|
||||
} else if (jsonData.action == "get_group_list") {
|
||||
resData["data"] = groups.map(group => {
|
||||
return {
|
||||
group_id: group.uid,
|
||||
group_name: group.name,
|
||||
member_count: group.members.length,
|
||||
group_members: group.members.map(member => {
|
||||
return {
|
||||
user_id: member.uin,
|
||||
@ -27,33 +30,39 @@ function handlePost(jsonData: any) {
|
||||
})
|
||||
}
|
||||
})
|
||||
} else if (jsonData.action == "get_group_member_list") {
|
||||
}
|
||||
else if (jsonData.action == "get_group_info") {
|
||||
let group = groups.find(group => group.uid == jsonData.params.group_id)
|
||||
if (group) {
|
||||
resData["data"] = {
|
||||
group_id: group.uid,
|
||||
group_name: group.name,
|
||||
member_count: group.members.length,
|
||||
}
|
||||
}
|
||||
}
|
||||
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"] ={
|
||||
user_id: member.uin,
|
||||
user_name: member.nick,
|
||||
user_display_name: member.cardName || member.nick,
|
||||
nickname: member.nick,
|
||||
card: member.cardName,
|
||||
role: OnebotGroupMemberRole[member.role],
|
||||
}
|
||||
}
|
||||
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 => {
|
||||
let role = "member"
|
||||
switch (member.role) {
|
||||
case 4: {
|
||||
role = "owner"
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
role = "admin"
|
||||
break
|
||||
}
|
||||
case 2: {
|
||||
role = "member"
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
return {
|
||||
user_id: member.uin,
|
||||
user_name: member.nick,
|
||||
user_display_name: member.cardName || member.nick,
|
||||
nickname: member.nick,
|
||||
card: member.cardName,
|
||||
role
|
||||
role: OnebotGroupMemberRole[member.role],
|
||||
}
|
||||
|
||||
}) || []
|
||||
@ -76,11 +85,25 @@ function handlePost(jsonData: any) {
|
||||
|
||||
export function startExpress(port: number) {
|
||||
const app = express();
|
||||
|
||||
// 中间件,用于解析POST请求的请求体
|
||||
app.use(express.urlencoded({extended: true}));
|
||||
app.use(express.json());
|
||||
|
||||
function parseToOnebot12(action: PostDataAction) {
|
||||
app.post('/' + action, (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
jsonData.action = action
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
});
|
||||
}
|
||||
const actionList = ["get_login_info", "send_private_msg", "send_group_msg",
|
||||
"get_group_list", "get_friend_list", "delete_msg"]
|
||||
|
||||
for (const action of actionList) {
|
||||
parseToOnebot12(action as PostDataAction)
|
||||
}
|
||||
|
||||
app.get('/', (req: any, res: any) => {
|
||||
res.send('llonebot已启动');
|
||||
})
|
||||
@ -92,18 +115,6 @@ export function startExpress(port: number) {
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
});
|
||||
app.post('/send_private_msg', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
jsonData.action = "send_private_msg"
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
})
|
||||
app.post('/send_group_msg', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
jsonData.action = "send_group_msg"
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
})
|
||||
app.post('/send_msg', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
if (jsonData.message_type == "private") {
|
||||
@ -120,12 +131,7 @@ export function startExpress(port: number) {
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
})
|
||||
app.post('/delete_msg', (req: any, res: any) => {
|
||||
let jsonData: PostDataSendMsg = req.body;
|
||||
jsonData.action = "delete_msg"
|
||||
let resData = handlePost(jsonData)
|
||||
res.send(resData)
|
||||
})
|
||||
|
||||
app.listen(port, "0.0.0.0", () => {
|
||||
console.log(`服务器已启动,监听端口 ${port}`);
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {webContents} from 'electron';
|
||||
import {ipcMain, webContents} from 'electron';
|
||||
import {PostDataSendMsg} from "../common/types";
|
||||
import {CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG} from "../common/IPCChannel";
|
||||
|
||||
|
@ -1,4 +1,9 @@
|
||||
import {Group, User} from "../common/types";
|
||||
import {Group, SelfInfo, User} from "../common/types";
|
||||
|
||||
export let groups: Group[] = []
|
||||
export let friends: User[] = []
|
||||
|
||||
export let selfInfo: SelfInfo = {
|
||||
user_id: "",
|
||||
nickname: ""
|
||||
}
|
@ -5,9 +5,10 @@ import * as path from "path";
|
||||
const fs = require('fs');
|
||||
import {ipcMain} from 'electron';
|
||||
|
||||
import {Config, Group, User} from "../common/types";
|
||||
import {Config, Group, SelfInfo, User} from "../common/types";
|
||||
import {
|
||||
CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
|
||||
CHANNEL_DOWNLOAD_FILE,
|
||||
CHANNEL_GET_CONFIG, CHANNEL_GET_SELF_INFO, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
|
||||
CHANNEL_SET_CONFIG,
|
||||
CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS,
|
||||
CHANNEL_UPDATE_GROUPS
|
||||
@ -15,7 +16,7 @@ import {
|
||||
import {ConfigUtil} from "./config";
|
||||
import {startExpress} from "./HttpServer";
|
||||
import {log} from "./utils";
|
||||
import {friends, groups} from "./data";
|
||||
import {friends, groups, selfInfo} from "./data";
|
||||
|
||||
|
||||
|
||||
@ -31,6 +32,24 @@ function onLoad(plugin: any) {
|
||||
ipcMain.handle(CHANNEL_GET_CONFIG, (event: any, arg: any) => {
|
||||
return configUtil.getConfig()
|
||||
})
|
||||
ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: {uri: string, localFilePath: string}) => {
|
||||
let url = new URL(arg.uri);
|
||||
if (url.protocol == "base64:"){
|
||||
// base64转成文件
|
||||
let base64Data = arg.uri.split("base64://")[1]
|
||||
const buffer = Buffer.from(base64Data, 'base64');
|
||||
|
||||
fs.writeFileSync(arg.localFilePath, buffer);
|
||||
}
|
||||
else if (url.protocol == "http:" || url.protocol == "https:") {
|
||||
// 下载文件
|
||||
let res = await fetch(url)
|
||||
let blob = await res.blob();
|
||||
let buffer = await blob.arrayBuffer();
|
||||
fs.writeFileSync(arg.localFilePath, Buffer.from(buffer));
|
||||
}
|
||||
return arg.localFilePath;
|
||||
})
|
||||
ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => {
|
||||
fs.writeFileSync(configFilePath, JSON.stringify(arg, null, 2), "utf-8")
|
||||
})
|
||||
@ -74,7 +93,8 @@ function onLoad(plugin: any) {
|
||||
fetch(configUtil.getConfig().host, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
"x-self-id": selfInfo.user_id
|
||||
},
|
||||
body: JSON.stringify(arg)
|
||||
}).then((res: any) => {
|
||||
@ -91,7 +111,10 @@ function onLoad(plugin: any) {
|
||||
log(arg)
|
||||
})
|
||||
|
||||
|
||||
ipcMain.on(CHANNEL_GET_SELF_INFO, (event: any, arg: SelfInfo) => {
|
||||
selfInfo.user_id = arg.user_id;
|
||||
selfInfo.nickname = arg.nickname;
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
// Electron 主进程 与 渲染进程 交互的桥梁
|
||||
|
||||
import {Config, Group, PostDataSendMsg, User} from "./common/types";
|
||||
import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types";
|
||||
import {
|
||||
CHANNEL_GET_CONFIG, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA,
|
||||
CHANNEL_DOWNLOAD_FILE,
|
||||
CHANNEL_GET_CONFIG, CHANNEL_GET_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
|
||||
@ -45,6 +46,12 @@ contextBridge.exposeInMainWorld("llonebot", {
|
||||
},
|
||||
getConfig: async () => {
|
||||
return ipcRenderer.invoke(CHANNEL_GET_CONFIG);
|
||||
}
|
||||
},
|
||||
setSelfInfo(selfInfo: SelfInfo){
|
||||
ipcRenderer.send(CHANNEL_GET_SELF_INFO, selfInfo)
|
||||
},
|
||||
downloadFile: async (arg: {uri: string, localFilePath: string}) => {
|
||||
return ipcRenderer.invoke(CHANNEL_DOWNLOAD_FILE, arg);
|
||||
},
|
||||
// startExpress,
|
||||
});
|
@ -2,7 +2,7 @@
|
||||
|
||||
// import express from "express";
|
||||
// const { ipcRenderer } = require('electron');
|
||||
import {AtType, Group, MessageElement, Peer, PostDataSendMsg, User} from "./common/types";
|
||||
import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types";
|
||||
|
||||
let self_qq: string = ""
|
||||
let groups: Group[] = []
|
||||
@ -99,16 +99,26 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
sub_type: "",
|
||||
message: []
|
||||
}
|
||||
// let group: Group
|
||||
if (message.peer.chatType == "group") {
|
||||
let group_id = message.peer.uid
|
||||
let group = (await getGroup(group_id))!
|
||||
onebot_message_data["group_id"] = message.peer.uid
|
||||
let groupMember = await getGroupMember(group_id, message.sender.uid)
|
||||
onebot_message_data["user_id"] = groupMember!.uin
|
||||
onebot_message_data.sender = {
|
||||
user_id: groupMember!.uin,
|
||||
nickname: groupMember!.nick,
|
||||
card: groupMember!.cardName,
|
||||
role: OnebotGroupMemberRole[groupMember!.role]
|
||||
}
|
||||
console.log("收到群消息", onebot_message_data)
|
||||
} else if (message.peer.chatType == "private") {
|
||||
onebot_message_data["user_id"] = message.peer.uid
|
||||
let friend = getFriend(message.sender.uid)
|
||||
onebot_message_data.sender = {
|
||||
user_id: friend!.uin,
|
||||
nickname: friend!.nickName
|
||||
}
|
||||
}
|
||||
for (let element of message.raw.elements) {
|
||||
let message_data: any = {
|
||||
@ -132,7 +142,11 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
message_data["type"] = "image"
|
||||
message_data["data"]["file_id"] = element.picElement.fileUuid
|
||||
message_data["data"]["path"] = element.picElement.sourcePath
|
||||
message_data["data"]["file"] = element.picElement.sourcePath
|
||||
let startS = "file://"
|
||||
if (!element.picElement.sourcePath.startsWith("/")) {
|
||||
startS += "/"
|
||||
}
|
||||
message_data["data"]["file"] = startS + element.picElement.sourcePath
|
||||
} else if (element.replyElement) {
|
||||
message_data["type"] = "reply"
|
||||
message_data["data"]["id"] = msgHistory.find(msg => msg.raw.msgSeq == element.replyElement.replayMsgSeq)?.raw.msgId
|
||||
@ -148,7 +162,7 @@ async function handleNewMessage(messages: MessageElement[]) {
|
||||
async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
if (postData.action == "send_private_msg" || postData.action == "send_group_msg") {
|
||||
let peer: Peer | null = null;
|
||||
if (!postData.params){
|
||||
if (!postData.params) {
|
||||
postData.params = {
|
||||
message: postData.message,
|
||||
user_id: postData.user_id,
|
||||
@ -178,8 +192,8 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
}
|
||||
}
|
||||
if (peer) {
|
||||
for (let message of postData.params.message){
|
||||
if (message.type == "at"){
|
||||
for (let message of postData.params.message) {
|
||||
if (message.type == "at") {
|
||||
// @ts-ignore
|
||||
message.type = "text"
|
||||
message.atType = AtType.atUser
|
||||
@ -189,20 +203,35 @@ async function listenSendMessage(postData: PostDataSendMsg) {
|
||||
message.atNtUid = atMember.uid
|
||||
message.atUid = atUid
|
||||
message.content = `@${atMember.cardName || atMember.nick}`
|
||||
}
|
||||
else if (message.type == "text"){
|
||||
} else if (message.type == "text") {
|
||||
message.content = message.data?.text || message.content
|
||||
}
|
||||
else if (message.type == "image" || message.type == "voice"){
|
||||
message.file = message.data?.file || message.file
|
||||
}
|
||||
else if (message.type == "reply"){
|
||||
} else if (message.type == "image" || message.type == "voice") {
|
||||
// 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") {
|
||||
ext = ".png"
|
||||
}
|
||||
if (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})
|
||||
}
|
||||
message.file = localFilePath
|
||||
} else if (message.type == "reply") {
|
||||
let msgId = message.data?.id || message.msgId
|
||||
let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId)
|
||||
message.msgId = msgId
|
||||
message.msgSeq = replyMessage?.raw.msgSeq || ""
|
||||
}
|
||||
}
|
||||
// 发送完之后要删除下载的文件
|
||||
console.log("发送消息", postData)
|
||||
window.LLAPI.sendMessage(peer, postData.params.message).then(res => console.log("消息发送成功:", res),
|
||||
err => console.log("消息发送失败", postData, err))
|
||||
@ -240,8 +269,7 @@ function onLoad() {
|
||||
setTimeout(() => {
|
||||
getGroupsMembers(failedGroups).then()
|
||||
}, 1000)
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
console.log("全部群成员获取完毕", groups)
|
||||
}
|
||||
}
|
||||
@ -260,20 +288,29 @@ function onLoad() {
|
||||
console.log("chatListEle", chatListEle)
|
||||
}
|
||||
|
||||
getGroups().then(()=>{
|
||||
getGroupsMembers(groups).then(()=>{
|
||||
getGroups().then(() => {
|
||||
getGroupsMembers(groups).then(() => {
|
||||
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.LLAPI.add_qmenu((qContextMenu: Node) => {
|
||||
let btn = document.createElement("a")
|
||||
btn.className = "q-context-menu-item q-context-menu-item--normal vue-component"
|
||||
btn.setAttribute("aria-disabled", "false")
|
||||
btn.setAttribute("role", "menuitem")
|
||||
btn.setAttribute("tabindex", "-1")
|
||||
btn.onclick = ()=>{
|
||||
btn.onclick = () => {
|
||||
// window.LLAPI.getPeer().then(peer => {
|
||||
// // console.log("current peer", peer)
|
||||
// if (peer && peer.chatType == "group") {
|
||||
@ -288,7 +325,8 @@ function onLoad() {
|
||||
await getGroupMembers(group.uid, true)
|
||||
}
|
||||
}
|
||||
func().then(()=> {
|
||||
|
||||
func().then(() => {
|
||||
console.log("获取群成员列表结果", groups);
|
||||
// 找到members数量为空的群
|
||||
groups.map(group => {
|
||||
@ -326,7 +364,7 @@ function onLoad() {
|
||||
// 获得当前聊天窗口
|
||||
window.LLAPI.getPeer().then(peer => {
|
||||
// console.log("current peer", peer)
|
||||
if (peer && peer.chatType == "group"){
|
||||
if (peer && peer.chatType == "group") {
|
||||
getGroupMembers(peer.uid, false).then()
|
||||
}
|
||||
})
|
||||
@ -339,7 +377,7 @@ function onLoad() {
|
||||
// 传入目标节点和观察选项
|
||||
observer.observe(targetNode, config);
|
||||
|
||||
}catch (e) {
|
||||
} catch (e) {
|
||||
window.llonebot.log(e)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user