add ptt to text (#1837)
This commit is contained in:
1 parent
2c8f9743da
commit
25db6f0835
5 files changed
+97
-1
No files matched your search
@@ -341,7 +341,7 @@ export interface NodeIKernelMsgService {
|
||||
|
||||
getFileThumbSavePath (arg1: string, arg2: number, arg3: boolean): unknown;
|
||||
|
||||
translatePtt2Text (msgId: string, peer: Peer, msgElement: MessageElement): unknown;
|
||||
translatePtt2Text (msgId: string, peer: Peer, msgElement: MessageElement): Promise<GeneralCallResult>;
|
||||
|
||||
setPttPlayedState (arg1: string, arg2: Peer, arg3: string): unknown;
|
||||
|
||||
|
||||
@@ -157,6 +157,7 @@ import { CancelOnlineFile } from './file/online/CancelOnlineFile';
|
||||
import { ReceiveOnlineFile } from './file/online/ReceiveOnlineFile';
|
||||
import { RefuseOnlineFile } from './file/online/RefuseOnlineFile';
|
||||
import { GetFilesetId } from './file/flash/GetFilesetIdByCode';
|
||||
import { FetchPttText } from '@/napcat-onebot/action/msg/FetchPttText';
|
||||
|
||||
export function getAllHandlers (obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||
const actionHandlers = [
|
||||
@@ -189,6 +190,7 @@ export function getAllHandlers (obContext: NapCatOneBot11Adapter, core: NapCatCo
|
||||
new GetGroupInfoEx(obContext, core),
|
||||
new FetchEmojiLike(obContext, core),
|
||||
new GetEmojiLikes(obContext, core),
|
||||
new FetchPttText(obContext, core),
|
||||
new GetFile(obContext, core),
|
||||
new SetQQProfile(obContext, core),
|
||||
new ShareGroupEx(obContext, core),
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { ActionName } from '@/napcat-onebot/action/router';
|
||||
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
||||
import { MessageUnique } from 'napcat-common/src/message-unique';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
import { PromiseTimer } from '@/napcat-common/src/helper';
|
||||
import { ElementType } from '@/napcat-core';
|
||||
import { MsgActionsExamples } from '@/napcat-onebot/action/msg/examples';
|
||||
|
||||
const PayloadSchema = Type.Object({
|
||||
message_id: Type.Union([Type.Number(), Type.String()], { description: '消息ID' }),
|
||||
});
|
||||
|
||||
type PayloadType = Static<typeof PayloadSchema>;
|
||||
|
||||
const ReturnSchema = Type.Object({ text: Type.String({ description: '得到的文本' }) });
|
||||
|
||||
type ReturnType = Static<typeof ReturnSchema>;
|
||||
|
||||
export class FetchPttText extends OneBotAction<PayloadType, ReturnType> {
|
||||
override actionName = ActionName.FetchPttText;
|
||||
override actionSummary = '获取语音转文字结果';
|
||||
override actionTags = ['消息扩展'];
|
||||
override payloadExample = MsgActionsExamples.FetchPttText.payload;
|
||||
override returnExample = MsgActionsExamples.FetchPttText.response;
|
||||
|
||||
override payloadSchema = PayloadSchema;
|
||||
override returnSchema = ReturnSchema;
|
||||
|
||||
async _handle (payload: PayloadType) {
|
||||
if (!payload.message_id) {
|
||||
throw Error('参数message_id不能为空');
|
||||
}
|
||||
const MsgShortId = MessageUnique.getShortIdByMsgId(payload.message_id.toString());
|
||||
const msgIdWithPeer = MessageUnique.getMsgIdAndPeerByShortId(MsgShortId ?? +payload.message_id);
|
||||
if (!msgIdWithPeer) {
|
||||
throw new Error('消息不存在');
|
||||
}
|
||||
const peer = { guildId: '', peerUid: msgIdWithPeer?.Peer.peerUid, chatType: msgIdWithPeer.Peer.chatType };
|
||||
const getMsgTimeout = this.obContext.configLoader.configData.timeout.baseTimeout;
|
||||
const msgRes = await PromiseTimer(
|
||||
this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgIdWithPeer?.MsgId || payload.message_id.toString()]),
|
||||
getMsgTimeout
|
||||
).catch((e: unknown) => {
|
||||
if (e instanceof Error && e.message.startsWith('PromiseTimer:')) {
|
||||
throw new Error(`消息不存在或已被撤回: ${e.message || String(e)}`);
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
const msg = msgRes.msgList[0];
|
||||
if (!msg) throw Error('消息不存在或已被撤回');
|
||||
const pttSegment = msg.elements.find((s) =>
|
||||
s.elementType === ElementType.PTT
|
||||
);
|
||||
if (!pttSegment) {
|
||||
throw Error('消息中不包含语音');
|
||||
}
|
||||
const getPttTextTimeout = this.obContext.configLoader.configData.timeout.baseTimeout;
|
||||
await PromiseTimer(
|
||||
this.core.context.session.getMsgService().translatePtt2Text(msgIdWithPeer.MsgId, msgIdWithPeer.Peer, pttSegment),
|
||||
getPttTextTimeout
|
||||
).catch((e: unknown) => {
|
||||
if (e instanceof Error && e.message.startsWith('PromiseTimer:')) {
|
||||
throw new Error(`获取语音转文字结果失败: ${e.message || String(e)}`);
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
const msgRes2 = await PromiseTimer(
|
||||
this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgIdWithPeer?.MsgId || payload.message_id.toString()]),
|
||||
getMsgTimeout
|
||||
).catch((e: unknown) => {
|
||||
if (e instanceof Error && e.message.startsWith('PromiseTimer:')) {
|
||||
throw new Error(`消息不存在或已被撤回: ${e.message || String(e)}`);
|
||||
}
|
||||
throw e;
|
||||
});
|
||||
const msg2 = msgRes2.msgList[0];
|
||||
if (!msg2) throw Error('消息不存在或已被撤回');
|
||||
for (const s of msg2.elements) {
|
||||
if (s.elementType === ElementType.PTT) {
|
||||
if (s.pttElement?.text) {
|
||||
return { text: s.pttElement?.text };
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error('获取语音转文字结果失败');
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,12 @@ export const MsgActionsExamples = {
|
||||
message: 'hello',
|
||||
},
|
||||
},
|
||||
FetchPttText: {
|
||||
payload: { message_id: 123456 },
|
||||
response: {
|
||||
text: 'hello',
|
||||
},
|
||||
},
|
||||
MarkMsgAsRead: {
|
||||
payload: { group_id: '123456' },
|
||||
response: {},
|
||||
|
||||
@@ -141,6 +141,7 @@ export const ActionName = {
|
||||
ForwardFriendSingleMsg: 'forward_friend_single_msg',
|
||||
ForwardGroupSingleMsg: 'forward_group_single_msg',
|
||||
TranslateEnWordToZn: 'translate_en2zh',
|
||||
FetchPttText: 'fetch_ptt_text',
|
||||
SetMsgEmojiLike: 'set_msg_emoji_like',
|
||||
GoCQHTTP_SendForwardMsg: 'send_forward_msg',
|
||||
MarkPrivateMsgAsRead: 'mark_private_msg_as_read',
|
||||
|
||||
Reference in new issue
Block a user