diff --git a/docs/changelogs/CHANGELOG.v1.4.0.md b/docs/changelogs/CHANGELOG.v1.4.0.md index 80c1b02c..8aa6e97e 100644 --- a/docs/changelogs/CHANGELOG.v1.4.0.md +++ b/docs/changelogs/CHANGELOG.v1.4.0.md @@ -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) diff --git a/src/core b/src/core index f8896164..d7677362 160000 --- a/src/core +++ b/src/core @@ -1 +1 @@ -Subproject commit f8896164300375d1251a3cf71d308293dd5c24de +Subproject commit d7677362d108fe4d942a0da8a87dce1e6f9ed5ee diff --git a/src/onebot11/action/extends/OCRImage.ts b/src/onebot11/action/extends/OCRImage.ts new file mode 100644 index 00000000..a02cac6c --- /dev/null +++ b/src/onebot11/action/extends/OCRImage.ts @@ -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; + +export class OCRImage extends BaseAction { + 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; +} diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index 1a9d677a..3b5ed219 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -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(), diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index 16a03abe..af736f97 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -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" }