feat: ocr image

This commit is contained in:
手瓜一十雪 2024-05-25 13:38:19 +08:00
parent db93a8eed2
commit f2f1f893d8
5 changed files with 58 additions and 3 deletions

View File

@ -6,6 +6,7 @@ QQ Version: Windows 9.9.10-23873 / Linux 3.2.7-23361
## 新增与调整
* 支持空间Cookies获取
* 新增API /get_online_clients
* 支持获取在线设备 API /get_online_clients
* 支持图片OCR API /.ocr_image /ocr_image
新增的 API 详细见[API文档](https://napneko.github.io/zh-CN/develop/extends_api)

@ -1 +1 @@
Subproject commit f8896164300375d1251a3cf71d308293dd5c24de
Subproject commit d7677362d108fe4d942a0da8a87dce1e6f9ed5ee

View File

@ -0,0 +1,47 @@
import { DeviceList } from '@/onebot11/main';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { checkFileReceived, uri2local } from '@/common/utils/file';
import { NTQQSystemApi } from '@/core';
import fs from 'fs';
const SchemaData = {
type: 'object',
properties: {
image: { type: 'string' },
},
required: ['image']
} as const satisfies JSONSchema;
type Payload = FromSchema<typeof SchemaData>;
export class OCRImage extends BaseAction<Payload, any> {
actionName = ActionName.OCRImage;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const { path, isLocal, errMsg } = (await uri2local(payload.image));
if (errMsg) {
throw `头像${payload.file}设置失败,file字段可能格式不正确`;
}
if (path) {
await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃需要提前判断
const ret = await NTQQSystemApi.ORCImage(path);
if (!isLocal) {
fs.unlink(path, () => { });
}
if (!ret) {
throw `OCR ${payload.file}失败`;
}
// log(`头像设置返回:${JSON.stringify(ret)}`)
return ret.result;
}
if (!isLocal) {
fs.unlink(path, () => { });
}
throw `OCR ${payload.file}失败,文件可能不存在`;
}
}
export class IOCRImage extends OCRImage {
actionName = ActionName.IOCRImage;
}

View File

@ -58,6 +58,8 @@ import { Reboot, RebootNormol } from './system/Reboot';
import { GetGroupHonorInfo } from './go-cqhttp/GetGroupHonorInfo';
import { GoCQHTTHandleQuickAction } from './go-cqhttp/QuickAction';
import { GetGroupSystemMsg } from './group/GetGroupSystemMsg';
import { GetOnlineClient } from './go-cqhttp/GetOnlineClient';
import { IOCRImage, OCRImage } from './go-cqhttp/OCRImage';
export const actionHandlers = [
new RebootNormol(),
@ -104,6 +106,9 @@ export const actionHandlers = [
new GetRobotUinRange(),
new GetFriendWithCategory(),
//以下为go-cqhttp api
new GetOnlineClient(),
new OCRImage(),
new IOCRImage(),
new GetGroupHonorInfo(),
new SendGroupNotice(),
new GetGroupNotice(),

View File

@ -79,5 +79,7 @@ export enum ActionName {
GoCQHTTP_GetForwardMsg = 'get_forward_msg',
GetFriendMsgHistory = 'get_friend_msg_history',
GetGroupSystemMsg = 'get_group_system_msg',
GetOnlineClient = "get_online_clients"
GetOnlineClient = "get_online_clients",
OCRImage = "ocr_image",
IOCRImage = ".ocr_image"
}