mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a7d75f84cb | ||
![]() |
9bb69058c2 | ||
![]() |
89971dd2e4 | ||
![]() |
aea67db27c | ||
![]() |
c4b45f8298 | ||
![]() |
1a77abfc62 | ||
![]() |
eb32ecb79b | ||
![]() |
ccf91f4a94 | ||
![]() |
282b2a0da0 | ||
![]() |
b28b812396 |
12
README.md
12
README.md
@@ -1,9 +1,11 @@
|
||||
# LLOneBot API
|
||||
|
||||
将NTQQLiteLoaderAPI封装成OneBot11标准的API
|
||||
LiteLoaderQQNT的OneBot11协议插件
|
||||
|
||||
*注意:本文档对应的是 LiteLoader 1.0.0及以上版本,如果你使用的是旧版本请切换到本项目v1分支查看文档*
|
||||
|
||||
*V3之后不再需要LLAPI*
|
||||
|
||||
## 安装方法
|
||||
|
||||
1.安装[LiteLoaderQQNT](https://liteloaderqqnt.github.io/guide/install.html)
|
||||
@@ -16,7 +18,7 @@
|
||||
|
||||
## 支持的API
|
||||
|
||||
目前只支持http协议POST方法,不支持websocket,事件上报也是http协议
|
||||
目前只支持http协议,不支持websocket,事件上报也是http协议
|
||||
|
||||
主要功能:
|
||||
- [x] 发送好友消息
|
||||
@@ -46,6 +48,7 @@
|
||||
- [x] send_private_msg
|
||||
- [x] delete_msg
|
||||
- [x] get_group_list
|
||||
- [x] get_group_info
|
||||
- [x] get_group_member_list
|
||||
- [x] get_group_member_info
|
||||
- [x] get_friend_list
|
||||
@@ -67,7 +70,7 @@
|
||||
<details>
|
||||
<summary>调用接口报404</summary>
|
||||
<br/>
|
||||
目前没有支持全部的onebot规范接口,请检查是否调用了不支持的接口,并且所有接口都只支持POST方法,调用GET方法会报404
|
||||
目前没有支持全部的onebot规范接口,请检查是否调用了不支持的接口
|
||||
</details>
|
||||
<br/>
|
||||
|
||||
@@ -94,11 +97,10 @@
|
||||
|
||||
|
||||
## TODO
|
||||
|
||||
- [x] 重构摆脱LLAPI,目前调用LLAPI只能在renderer进程调用,需重构成在main进程调用
|
||||
- [ ] 转发消息记录
|
||||
- [ ] 好友点赞api
|
||||
- [ ] 支持websocket,等个有缘人提PR实现
|
||||
- [x] 重构摆脱LLAPI,目前调用LLAPI只能在renderer进程调用,需重构成在main进程调用
|
||||
|
||||
## onebot11文档
|
||||
<https://11.onebot.dev/>
|
||||
|
@@ -4,7 +4,7 @@
|
||||
"name": "LLOneBot",
|
||||
"slug": "LLOneBot",
|
||||
"description": "LiteLoaderQQNT的OneBotApi",
|
||||
"version": "3.0.2",
|
||||
"version": "3.0.8",
|
||||
"thumbnail": "./icon.png",
|
||||
"authors": [{
|
||||
"name": "linyuchen",
|
||||
|
@@ -1,10 +1,31 @@
|
||||
import { NTQQApi } from '../ntqqapi/ntcall';
|
||||
import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types";
|
||||
import { log } from "./utils";
|
||||
|
||||
export let groups: Group[] = []
|
||||
export let friends: Friend[] = []
|
||||
export let msgHistory: Record<string, RawMessage> = {} // msgId: RawMessage
|
||||
|
||||
let globalMsgId = Date.now()
|
||||
|
||||
export function addHistoryMsg(msg: RawMessage): boolean{
|
||||
let existMsg = msgHistory[msg.msgId]
|
||||
if (existMsg){
|
||||
Object.assign(existMsg, msg)
|
||||
msg.msgShortId = existMsg.msgShortId;
|
||||
return false
|
||||
}
|
||||
msg.msgShortId = ++globalMsgId
|
||||
msgHistory[msg.msgId] = msg
|
||||
return true
|
||||
}
|
||||
|
||||
export function getHistoryMsgByShortId(shortId: number | string){
|
||||
// log("getHistoryMsgByShortId", shortId, Object.values(msgHistory).map(m=>m.msgShortId))
|
||||
return Object.values(msgHistory).find(msg => msg.msgShortId.toString() == shortId.toString())
|
||||
}
|
||||
|
||||
|
||||
export async function getFriend(qq: string): Promise<Friend | undefined> {
|
||||
let friend = friends.find(friend => friend.uin === qq)
|
||||
// if (!friend){
|
||||
|
@@ -10,10 +10,9 @@ import {
|
||||
CHANNEL_LOG,
|
||||
CHANNEL_SET_CONFIG,
|
||||
} from "../common/channels";
|
||||
import { ConfigUtil } from "../common/config";
|
||||
import { postMsg, startExpress } from "../onebot11/server";
|
||||
import { CONFIG_DIR, getConfigUtil, log } from "../common/utils";
|
||||
import { friends, groups, msgHistory, selfInfo } from "../common/data";
|
||||
import { addHistoryMsg, msgHistory, selfInfo } from "../common/data";
|
||||
import { hookNTQQApiReceive, ReceiveCmd, registerReceiveHook } from "../ntqqapi/hook";
|
||||
import { OB11Constructor } from "../onebot11/constructor";
|
||||
import { NTQQApi } from "../ntqqapi/ntcall";
|
||||
@@ -48,7 +47,11 @@ function onLoad() {
|
||||
|
||||
function postRawMsg(msgList: RawMessage[]) {
|
||||
const {debug, reportSelfMessage} = getConfigUtil().getConfig();
|
||||
for (const message of msgList) {
|
||||
for (let message of msgList) {
|
||||
message.msgShortId = msgHistory[message.msgId]?.msgShortId
|
||||
if (!message.msgShortId) {
|
||||
addHistoryMsg(message)
|
||||
}
|
||||
OB11Constructor.message(message).then((msg) => {
|
||||
if (debug) {
|
||||
msg.raw = message;
|
||||
@@ -57,14 +60,17 @@ function onLoad() {
|
||||
return
|
||||
}
|
||||
postMsg(msg);
|
||||
// log("post msg", msg)
|
||||
}).catch(e => log("constructMessage error: ", e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function start() {
|
||||
log("llonebot start")
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||
try {
|
||||
// log("received msg length", payload.msgList.length);
|
||||
postRawMsg(payload.msgList);
|
||||
} catch (e) {
|
||||
log("report message error: ", e.toString())
|
||||
@@ -76,7 +82,7 @@ function onLoad() {
|
||||
if (!reportSelfMessage) {
|
||||
return
|
||||
}
|
||||
log("reportSelfMessage", payload)
|
||||
// log("reportSelfMessage", payload)
|
||||
try {
|
||||
postRawMsg([payload.msgRecord]);
|
||||
} catch (e) {
|
||||
@@ -87,7 +93,7 @@ function onLoad() {
|
||||
startExpress(getConfigUtil().getConfig().port)
|
||||
}
|
||||
|
||||
async function getSelfInfo() {
|
||||
const init = async () => {
|
||||
try {
|
||||
const _ = await NTQQApi.getSelfInfo()
|
||||
Object.assign(selfInfo, _)
|
||||
@@ -95,7 +101,6 @@ function onLoad() {
|
||||
log("get self simple info", _)
|
||||
} catch (e) {
|
||||
log("retry get self info")
|
||||
|
||||
}
|
||||
if (selfInfo.uin) {
|
||||
try {
|
||||
@@ -104,25 +109,19 @@ function onLoad() {
|
||||
if (userInfo) {
|
||||
selfInfo.nick = userInfo.nick
|
||||
} else {
|
||||
return setTimeout(() => {
|
||||
getSelfInfo().then()
|
||||
}, 100)
|
||||
return setTimeout(init, 1000)
|
||||
}
|
||||
} catch (e) {
|
||||
log("get self nickname failed", e.toString())
|
||||
return setTimeout(() => {
|
||||
getSelfInfo().then()
|
||||
}, 100)
|
||||
return setTimeout(init, 1000)
|
||||
}
|
||||
start();
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
getSelfInfo().then()
|
||||
}, 100)
|
||||
}
|
||||
else{
|
||||
setTimeout(init, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
getSelfInfo().then()
|
||||
setTimeout(init, 1000)
|
||||
}
|
||||
|
||||
|
||||
|
@@ -3,7 +3,7 @@ import { getConfigUtil, log } from "../common/utils";
|
||||
import { NTQQApi, NTQQApiClass, sendMessagePool } from "./ntcall";
|
||||
import { Group, User } from "./types";
|
||||
import { RawMessage } from "./types";
|
||||
import { friends, groups, msgHistory } from "../common/data";
|
||||
import { addHistoryMsg, friends, groups, msgHistory } from "../common/data";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
|
||||
@@ -131,21 +131,17 @@ registerReceiveHook<{
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
msgHistory[message.msgId] = message;
|
||||
addHistoryMsg(message)
|
||||
}
|
||||
})
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
log("收到新消息,push到历史记录", message)
|
||||
if (!msgHistory[message.msgId]) {
|
||||
msgHistory[message.msgId] = message
|
||||
} else {
|
||||
Object.assign(msgHistory[message.msgId], message)
|
||||
}
|
||||
// log("收到新消息,push到历史记录", message)
|
||||
addHistoryMsg(message)
|
||||
}
|
||||
const msgIds = Object.keys(msgHistory);
|
||||
if (msgIds.length > 3000) {
|
||||
if (msgIds.length > 30000) {
|
||||
delete msgHistory[msgIds.sort()[0]]
|
||||
}
|
||||
})
|
||||
|
@@ -78,8 +78,7 @@ function callNTQQApi<ReturnType>(channel: NTQQApiChannel, className: NTQQApiClas
|
||||
success = true
|
||||
resolve(r)
|
||||
};
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// 这里的callback比较特殊,QQ后端先返回是否调用成功,再返回一条结果数据
|
||||
hookApiCallbacks[uuid] = (result: GeneralCallResult) => {
|
||||
log(`${methodName} callback`, result)
|
||||
@@ -90,8 +89,7 @@ function callNTQQApi<ReturnType>(channel: NTQQApiChannel, className: NTQQApiClas
|
||||
success = true
|
||||
resolve(payload);
|
||||
})
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
success = true
|
||||
reject(`ntqq api call failed, ${result.errMsg}`);
|
||||
}
|
||||
@@ -108,7 +106,7 @@ function callNTQQApi<ReturnType>(channel: NTQQApiChannel, className: NTQQApiClas
|
||||
ipcMain.emit(
|
||||
channel,
|
||||
{},
|
||||
{ type: 'request', callbackId: uuid, eventName: className + "-" + channel[channel.length - 1] },
|
||||
{type: 'request', callbackId: uuid, eventName: className + "-" + channel[channel.length - 1]},
|
||||
[methodName, ...args],
|
||||
)
|
||||
})
|
||||
@@ -142,13 +140,17 @@ export class NTQQApi {
|
||||
}
|
||||
|
||||
static async getUserInfo(uid: string) {
|
||||
const result = await callNTQQApi<{ profiles: Map<string, User> }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
||||
[{ force: true, uids: [uid] }, undefined], ReceiveCmd.USER_INFO)
|
||||
const result = await callNTQQApi<{
|
||||
profiles: Map<string, User>
|
||||
}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.USER_INFO,
|
||||
[{force: true, uids: [uid]}, undefined], ReceiveCmd.USER_INFO)
|
||||
return result.profiles.get(uid)
|
||||
}
|
||||
|
||||
static async getFriends(forced = false) {
|
||||
const data = await callNTQQApi<{ data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: Friend[] }[] }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.FRIENDS, [{ force_update: forced }, undefined], ReceiveCmd.FRIENDS)
|
||||
const data = await callNTQQApi<{
|
||||
data: { categoryId: number, categroyName: string, categroyMbCount: number, buddyList: Friend[] }[]
|
||||
}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.FRIENDS, [{force_update: forced}, undefined], ReceiveCmd.FRIENDS)
|
||||
let _friends: Friend[] = [];
|
||||
for (const fData of data.data) {
|
||||
_friends.push(...fData.buddyList)
|
||||
@@ -161,7 +163,10 @@ export class NTQQApi {
|
||||
if (process.platform != "win32") {
|
||||
cbCmd = ReceiveCmd.GROUPS_UNIX
|
||||
}
|
||||
const result = await callNTQQApi<{ updateType: number, groupList: Group[] }>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUPS, [{ force_update: forced }, undefined], cbCmd)
|
||||
const result = await callNTQQApi<{
|
||||
updateType: number,
|
||||
groupList: Group[]
|
||||
}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUPS, [{force_update: forced}, undefined], cbCmd)
|
||||
return result.groupList
|
||||
}
|
||||
|
||||
@@ -172,7 +177,9 @@ export class NTQQApi {
|
||||
}])
|
||||
// log("get group member sceneId", sceneId);
|
||||
try {
|
||||
const result = await callNTQQApi<{result:{infos: any}}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBERS,
|
||||
const result = await callNTQQApi<{
|
||||
result: { infos: any }
|
||||
}>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.GROUP_MEMBERS,
|
||||
[{
|
||||
sceneId: sceneId,
|
||||
num: num
|
||||
@@ -182,7 +189,7 @@ export class NTQQApi {
|
||||
// log("members info", typeof result.result.infos, Object.keys(result.result.infos))
|
||||
let values = result.result.infos.values()
|
||||
|
||||
values = Array.from(values) as GroupMember[]
|
||||
values = Array.from(values) as GroupMember[]
|
||||
// log("members info", values);
|
||||
return values
|
||||
} catch (e) {
|
||||
@@ -192,7 +199,6 @@ export class NTQQApi {
|
||||
}
|
||||
|
||||
|
||||
|
||||
static getFileType(filePath: string) {
|
||||
return callNTQQApi<{
|
||||
ext: string
|
||||
@@ -204,7 +210,10 @@ export class NTQQApi {
|
||||
}
|
||||
|
||||
static copyFile(filePath: string, destPath: string) {
|
||||
return callNTQQApi<string>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.FS_API, NTQQApiMethod.FILE_COPY, [{ fromPath: filePath, toPath: destPath }])
|
||||
return callNTQQApi<string>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.FS_API, NTQQApiMethod.FILE_COPY, [{
|
||||
fromPath: filePath,
|
||||
toPath: destPath
|
||||
}])
|
||||
}
|
||||
|
||||
static getImageSize(filePath: string) {
|
||||
@@ -221,7 +230,14 @@ export class NTQQApi {
|
||||
// 上传文件到QQ的文件夹
|
||||
static async uploadFile(filePath: string) {
|
||||
const md5 = await NTQQApi.getFileMd5(filePath);
|
||||
const fileName = `${md5}.${(await NTQQApi.getFileType(filePath)).ext}`;
|
||||
let ext = (await NTQQApi.getFileType(filePath))?.ext
|
||||
if (ext) {
|
||||
ext = "." + ext
|
||||
}
|
||||
else{
|
||||
ext = ""
|
||||
}
|
||||
const fileName = `${md5}${ext}`;
|
||||
const mediaPath = await callNTQQApi<string>(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.MEDIA_FILE_PATH, [{
|
||||
path_info: {
|
||||
md5HexStr: md5,
|
||||
@@ -245,9 +261,9 @@ export class NTQQApi {
|
||||
}
|
||||
}
|
||||
|
||||
static async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string){
|
||||
static async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string) {
|
||||
// 用于下载收到的消息中的图片等
|
||||
if (fs.existsSync(sourcePath)){
|
||||
if (fs.existsSync(sourcePath)) {
|
||||
return sourcePath
|
||||
}
|
||||
const apiParams = [
|
||||
@@ -267,8 +283,12 @@ export class NTQQApi {
|
||||
await callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.DOWNLOAD_MEDIA, apiParams)
|
||||
return sourcePath
|
||||
}
|
||||
|
||||
static recallMsg(peer: Peer, msgIds: string[]) {
|
||||
return callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.RECALL_MSG, [{ peer, msgIds }, null])
|
||||
return callNTQQApi(NTQQApiChannel.IPC_UP_2, NTQQApiClass.NT_API, NTQQApiMethod.RECALL_MSG, [{
|
||||
peer,
|
||||
msgIds
|
||||
}, null])
|
||||
}
|
||||
|
||||
static sendMsg(peer: Peer, msgElements: SendMessageElement[]) {
|
||||
@@ -297,8 +317,7 @@ export class NTQQApi {
|
||||
// log("有正在发送的消息,等待中...")
|
||||
usingTime += 100;
|
||||
setTimeout(checkLastSend, 100);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
log("可以进行发送消息,设置发送成功回调", sendMessagePool)
|
||||
sendMessagePool[peerUid] = (rawMessage: RawMessage) => {
|
||||
success = true;
|
||||
|
@@ -182,6 +182,7 @@ export interface PicElement {
|
||||
|
||||
export interface RawMessage {
|
||||
msgId: string;
|
||||
msgShortId?: number; // 自己维护的消息id
|
||||
msgTime: string;
|
||||
msgSeq: string;
|
||||
senderUin: string; // 发送者QQ号
|
||||
|
@@ -1,21 +1,21 @@
|
||||
import { ActionName } from "./types";
|
||||
import BaseAction from "./BaseAction";
|
||||
import { NTQQApi } from "../../ntqqapi/ntcall";
|
||||
import { msgHistory } from "../../common/data";
|
||||
import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
|
||||
|
||||
interface Payload {
|
||||
message_id: string
|
||||
message_id: number
|
||||
}
|
||||
|
||||
class DeleteMsg extends BaseAction<Payload, void> {
|
||||
actionName = ActionName.DeleteMsg
|
||||
|
||||
protected async _handle(payload:Payload){
|
||||
let msg = msgHistory[payload.message_id]
|
||||
let msg = getHistoryMsgByShortId(payload.message_id)
|
||||
await NTQQApi.recallMsg({
|
||||
chatType: msg.chatType,
|
||||
peerUid: msg.peerUid
|
||||
}, [payload.message_id])
|
||||
}, [msg.msgId])
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { msgHistory } from "../../common/data";
|
||||
import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
|
||||
import { OB11Message } from '../types';
|
||||
import { OB11Constructor } from "../constructor";
|
||||
import { log } from "../../common/utils";
|
||||
@@ -7,7 +7,7 @@ import { ActionName } from "./types";
|
||||
|
||||
|
||||
export interface PayloadType {
|
||||
message_id: string
|
||||
message_id: number
|
||||
}
|
||||
|
||||
export type ReturnDataType = OB11Message
|
||||
@@ -17,7 +17,7 @@ class GetMsg extends BaseAction<PayloadType, OB11Message> {
|
||||
|
||||
protected async _handle(payload: PayloadType){
|
||||
// log("history msg ids", Object.keys(msgHistory));
|
||||
const msg = msgHistory[payload.message_id.toString()]
|
||||
const msg = getHistoryMsgByShortId(payload.message_id)
|
||||
if (msg) {
|
||||
const msgData = await OB11Constructor.message(msg);
|
||||
return msgData
|
||||
|
@@ -1,5 +1,11 @@
|
||||
import { AtType, ChatType, Group } from "../../ntqqapi/types";
|
||||
import { friends, getGroup, getStrangerByUin, msgHistory } from "../../common/data";
|
||||
import {
|
||||
addHistoryMsg,
|
||||
friends,
|
||||
getGroup,
|
||||
getHistoryMsgByShortId,
|
||||
getStrangerByUin,
|
||||
} from "../../common/data";
|
||||
import { OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types';
|
||||
import { NTQQApi } from "../../ntqqapi/ntcall";
|
||||
import { Peer } from "../../ntqqapi/ntcall";
|
||||
@@ -13,7 +19,7 @@ import { ActionName } from "./types";
|
||||
import * as fs from "fs";
|
||||
|
||||
export interface ReturnDataType {
|
||||
message_id: string
|
||||
message_id: number
|
||||
}
|
||||
|
||||
class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
@@ -90,9 +96,9 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
let replyMsgId = sendMsg.data.id;
|
||||
if (replyMsgId) {
|
||||
replyMsgId = replyMsgId.toString()
|
||||
const replyMsg = msgHistory[replyMsgId]
|
||||
const replyMsg = getHistoryMsgByShortId(replyMsgId)
|
||||
if (replyMsg) {
|
||||
sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsgId, replyMsg.senderUin, replyMsg.senderUin))
|
||||
sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsg.msgId, replyMsg.senderUin, replyMsg.senderUin))
|
||||
}
|
||||
}
|
||||
} break;
|
||||
@@ -119,8 +125,9 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
// log("send msg:", peer, sendElements)
|
||||
try {
|
||||
const returnMsg = await NTQQApi.sendMsg(peer, sendElements)
|
||||
addHistoryMsg(returnMsg)
|
||||
deleteAfterSentFiles.map(f=>fs.unlink(f, ()=>{}))
|
||||
return { message_id: returnMsg.msgId }
|
||||
return { message_id: returnMsg.msgShortId }
|
||||
} catch (e) {
|
||||
throw(e.toString())
|
||||
}
|
||||
|
@@ -1,19 +1,27 @@
|
||||
import {OB11MessageDataType, OB11GroupMemberRole, OB11Message, OB11MessageData, OB11Group, OB11GroupMember, OB11User} from "./types";
|
||||
import {
|
||||
OB11MessageDataType,
|
||||
OB11GroupMemberRole,
|
||||
OB11Message,
|
||||
OB11Group,
|
||||
OB11GroupMember,
|
||||
OB11User
|
||||
} from "./types";
|
||||
import { AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User } from '../ntqqapi/types';
|
||||
import { getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data';
|
||||
import {file2base64, getConfigUtil, log} from "../common/utils";
|
||||
import { getFriend, getGroupMember, getHistoryMsgBySeq, msgHistory, selfInfo } from '../common/data';
|
||||
import { file2base64, getConfigUtil, log } from "../common/utils";
|
||||
import { NTQQApi } from "../ntqqapi/ntcall";
|
||||
|
||||
|
||||
export class OB11Constructor {
|
||||
static async message(msg: RawMessage): Promise<OB11Message> {
|
||||
|
||||
const {enableBase64} = getConfigUtil().getConfig()
|
||||
const message_type = msg.chatType == ChatType.group ? "group" : "private";
|
||||
const resMsg: OB11Message = {
|
||||
self_id: selfInfo.uin,
|
||||
user_id: msg.senderUin,
|
||||
time: parseInt(msg.msgTime) || 0,
|
||||
message_id: msg.msgId,
|
||||
message_id: msg.msgShortId,
|
||||
real_id: msg.msgId,
|
||||
message_type: msg.chatType == ChatType.group ? "group" : "private",
|
||||
sender: {
|
||||
@@ -28,6 +36,7 @@ export class OB11Constructor {
|
||||
post_type: "message",
|
||||
}
|
||||
if (msg.chatType == ChatType.group) {
|
||||
resMsg.sub_type = "normal"
|
||||
resMsg.group_id = msg.peerUin
|
||||
const member = await getGroupMember(msg.peerUin, msg.senderUin);
|
||||
if (member) {
|
||||
@@ -56,13 +65,13 @@ export class OB11Constructor {
|
||||
} else {
|
||||
let atUid = element.textElement.atNtUid
|
||||
let atQQ = element.textElement.atUid
|
||||
if (!atQQ || atQQ === "0"){
|
||||
if (!atQQ || atQQ === "0") {
|
||||
const atMember = await getGroupMember(msg.peerUin, null, atUid)
|
||||
if (atMember){
|
||||
if (atMember) {
|
||||
atQQ = atMember.uin
|
||||
}
|
||||
}
|
||||
if (atQQ){
|
||||
if (atQQ) {
|
||||
message_data["data"]["mention"] = atQQ
|
||||
message_data["data"]["qq"] = atQQ
|
||||
}
|
||||
@@ -78,16 +87,15 @@ export class OB11Constructor {
|
||||
try {
|
||||
await NTQQApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid,
|
||||
element.elementId, element.picElement.thumbPath.get(0), element.picElement.sourcePath)
|
||||
}catch (e) {
|
||||
} catch (e) {
|
||||
message_data["data"]["http_file"] = IMAGE_HTTP_HOST + element.picElement.originImageUrl
|
||||
}
|
||||
} else if (element.replyElement) {
|
||||
message_data["type"] = "reply"
|
||||
const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq)
|
||||
if (replyMsg) {
|
||||
message_data["data"]["id"] = replyMsg.msgId
|
||||
}
|
||||
else{
|
||||
message_data["data"]["id"] = replyMsg.msgShortId
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if (element.pttElement) {
|
||||
@@ -104,10 +112,9 @@ export class OB11Constructor {
|
||||
message_data["type"] = OB11MessageDataType.json;
|
||||
message_data["data"]["data"] = element.arkElement.bytesData;
|
||||
}
|
||||
if (message_data.data.http_file){
|
||||
if (message_data.data.http_file) {
|
||||
message_data.data.file = message_data.data.http_file
|
||||
}
|
||||
else if (message_data.data.file) {
|
||||
} else if (message_data.data.file) {
|
||||
let filePath: string = message_data.data.file;
|
||||
message_data.data.file = "file://" + filePath
|
||||
if (enableBase64) {
|
||||
@@ -125,24 +132,24 @@ export class OB11Constructor {
|
||||
}
|
||||
return resMsg;
|
||||
}
|
||||
|
||||
static friend(friend: User): OB11User{
|
||||
|
||||
static friend(friend: User): OB11User {
|
||||
return {
|
||||
user_id: friend.uin,
|
||||
nickname: friend.nick,
|
||||
remark: friend.remark
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static selfInfo(selfInfo: SelfInfo): OB11User{
|
||||
static selfInfo(selfInfo: SelfInfo): OB11User {
|
||||
return {
|
||||
user_id: selfInfo.uin,
|
||||
nickname: selfInfo.nick
|
||||
}
|
||||
}
|
||||
|
||||
static friends(friends: User[]): OB11User[]{
|
||||
static friends(friends: User[]): OB11User[] {
|
||||
return friends.map(OB11Constructor.friend)
|
||||
}
|
||||
|
||||
@@ -154,7 +161,7 @@ export class OB11Constructor {
|
||||
}[role]
|
||||
}
|
||||
|
||||
static groupMember(group_id: string, member: GroupMember): OB11GroupMember{
|
||||
static groupMember(group_id: string, member: GroupMember): OB11GroupMember {
|
||||
return {
|
||||
group_id,
|
||||
user_id: member.uin,
|
||||
@@ -163,19 +170,19 @@ export class OB11Constructor {
|
||||
}
|
||||
}
|
||||
|
||||
static groupMembers(group: Group): OB11GroupMember[]{
|
||||
static groupMembers(group: Group): OB11GroupMember[] {
|
||||
log("construct ob11 group members", group)
|
||||
return group.members.map(m=>OB11Constructor.groupMember(group.groupCode, m))
|
||||
return group.members.map(m => OB11Constructor.groupMember(group.groupCode, m))
|
||||
}
|
||||
|
||||
static group(group: Group): OB11Group{
|
||||
static group(group: Group): OB11Group {
|
||||
return {
|
||||
group_id: group.groupCode,
|
||||
group_name: group.groupName
|
||||
}
|
||||
}
|
||||
|
||||
static groups(groups: Group[]): OB11Group[]{
|
||||
static groups(groups: Group[]): OB11Group[] {
|
||||
return groups.map(OB11Constructor.group)
|
||||
}
|
||||
}
|
@@ -58,12 +58,12 @@ export enum OB11MessageType {
|
||||
export interface OB11Message {
|
||||
self_id?: string,
|
||||
time: number,
|
||||
message_id: string,
|
||||
message_id: number,
|
||||
real_id: string,
|
||||
user_id: string,
|
||||
group_id?: string,
|
||||
message_type: "private" | "group",
|
||||
sub_type?: "friend" | "group" | "other",
|
||||
sub_type?: "friend" | "group" | "normal",
|
||||
sender: OB11Sender,
|
||||
message: OB11MessageData[],
|
||||
raw_message: string,
|
||||
|
@@ -39,11 +39,12 @@ export async function uri2local(fileName: string, uri: string){
|
||||
}
|
||||
} else if (url.protocol === "file:"){
|
||||
// await fs.copyFile(url.pathname, filePath);
|
||||
let pathname = decodeURIComponent(url.pathname)
|
||||
if (process.platform === "win32"){
|
||||
filePath = url.pathname.slice(1)
|
||||
filePath = pathname.slice(1)
|
||||
}
|
||||
else{
|
||||
filePath = url.pathname
|
||||
filePath = pathname
|
||||
}
|
||||
res.isLocal = true
|
||||
}
|
||||
|
Reference in New Issue
Block a user