mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
aea67db27c | ||
![]() |
c4b45f8298 | ||
![]() |
1a77abfc62 | ||
![]() |
eb32ecb79b | ||
![]() |
ccf91f4a94 |
10
README.md
10
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
|
||||
@@ -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.4",
|
||||
"version": "3.0.6",
|
||||
"thumbnail": "./icon.png",
|
||||
"authors": [{
|
||||
"name": "linyuchen",
|
||||
|
@@ -8,19 +8,20 @@ export let msgHistory: Record<string, RawMessage> = {} // msgId: RawMessage
|
||||
|
||||
let globalMsgId = Date.now()
|
||||
|
||||
export function addHistoryMsg(msg: RawMessage){
|
||||
export function addHistoryMsg(msg: RawMessage): boolean{
|
||||
let existMsg = msgHistory[msg.msgId]
|
||||
if (existMsg){
|
||||
Object.assign(existMsg, msg)
|
||||
msg.msgShortId = existMsg.msgShortId;
|
||||
return
|
||||
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))
|
||||
// log("getHistoryMsgByShortId", shortId, Object.values(msgHistory).map(m=>m.msgShortId))
|
||||
return Object.values(msgHistory).find(msg => msg.msgShortId.toString() == shortId.toString())
|
||||
}
|
||||
|
||||
|
@@ -12,7 +12,7 @@ import {
|
||||
} from "../common/channels";
|
||||
import { postMsg, startExpress } from "../onebot11/server";
|
||||
import { CONFIG_DIR, getConfigUtil, log } from "../common/utils";
|
||||
import { addHistoryMsg, 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 +48,10 @@ function onLoad() {
|
||||
function postRawMsg(msgList: RawMessage[]) {
|
||||
const {debug, reportSelfMessage} = getConfigUtil().getConfig();
|
||||
for (let message of msgList) {
|
||||
addHistoryMsg(message)
|
||||
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) {
|
||||
@@ -86,7 +92,8 @@ function onLoad() {
|
||||
NTQQApi.getGroups(true).then()
|
||||
startExpress(getConfigUtil().getConfig().port)
|
||||
}
|
||||
const initLoop = setInterval(async ()=>{
|
||||
|
||||
const init = async () => {
|
||||
try {
|
||||
const _ = await NTQQApi.getSelfInfo()
|
||||
Object.assign(selfInfo, _)
|
||||
@@ -102,20 +109,19 @@ function onLoad() {
|
||||
if (userInfo) {
|
||||
selfInfo.nick = userInfo.nick
|
||||
} else {
|
||||
return
|
||||
return setTimeout(init, 1000)
|
||||
}
|
||||
} catch (e) {
|
||||
return log("get self nickname failed", e.toString())
|
||||
log("get self nickname failed", e.toString())
|
||||
return setTimeout(init, 1000)
|
||||
}
|
||||
clearInterval(initLoop);
|
||||
start();
|
||||
}
|
||||
}, 1000)
|
||||
async function getSelfInfo() {
|
||||
|
||||
else{
|
||||
setTimeout(init, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
getSelfInfo().then()
|
||||
setTimeout(init, 1000)
|
||||
}
|
||||
|
||||
|
||||
|
@@ -137,12 +137,8 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payl
|
||||
|
||||
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload) => {
|
||||
for (const message of payload.msgList) {
|
||||
log("收到新消息,push到历史记录", message)
|
||||
if (!msgHistory[message.msgId]) {
|
||||
addHistoryMsg(message)
|
||||
} else {
|
||||
Object.assign(msgHistory[message.msgId], message)
|
||||
}
|
||||
// log("收到新消息,push到历史记录", message)
|
||||
addHistoryMsg(message)
|
||||
}
|
||||
const msgIds = Object.keys(msgHistory);
|
||||
if (msgIds.length > 30000) {
|
||||
|
@@ -5,7 +5,6 @@ import {
|
||||
getGroup,
|
||||
getHistoryMsgByShortId,
|
||||
getStrangerByUin,
|
||||
msgHistory
|
||||
} from "../../common/data";
|
||||
import { OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types';
|
||||
import { NTQQApi } from "../../ntqqapi/ntcall";
|
||||
@@ -99,7 +98,7 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
||||
replyMsgId = replyMsgId.toString()
|
||||
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;
|
||||
|
@@ -1,12 +1,21 @@
|
||||
import {OB11MessageDataType, OB11GroupMemberRole, OB11Message, OB11MessageData, OB11Group, OB11GroupMember, OB11User} from "./types";
|
||||
import {
|
||||
OB11MessageDataType,
|
||||
OB11GroupMemberRole,
|
||||
OB11Message,
|
||||
OB11MessageData,
|
||||
OB11Group,
|
||||
OB11GroupMember,
|
||||
OB11User
|
||||
} from "./types";
|
||||
import { AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User } from '../ntqqapi/types';
|
||||
import { addHistoryMsg, getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data';
|
||||
import {file2base64, getConfigUtil, log} from "../common/utils";
|
||||
import { addHistoryMsg, 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 = {
|
||||
@@ -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,7 +87,7 @@ 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) {
|
||||
@@ -86,8 +95,7 @@ export class OB11Constructor {
|
||||
const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq)
|
||||
if (replyMsg) {
|
||||
message_data["data"]["id"] = replyMsg.msgShortId
|
||||
}
|
||||
else{
|
||||
} 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user