Skip to content

Commit f39c569

Browse files
committed
feat: add VoiceMsg2Text action for converting voice messages to text
1 parent c9f11fe commit f39c569

5 files changed

Lines changed: 51 additions & 2 deletions

File tree

package-dist.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"name":"llonebot","version":"5.0.2","type":"module","description":"","main":"llonebot.js","author":"linyuchen"}
1+
{"name":"llonebot","version":"5.1.0","type":"module","description":"","main":"llonebot.js","author":"linyuchen"}

src/onebot11/action/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ import { SetGroupPortrait } from './go-cqhttp/SetGroupPortrait'
9191
import { MoveGroupFile } from './llonebot/MoveGroupFile'
9292
import { GetGroupShutList } from './llonebot/GetGroupShutList'
9393
import { RenameGroupFileFolder } from './llonebot/RenameGroupFileFolder'
94+
import { VoiceMsg2Text } from '@/onebot11/action/llonebot/VoiceMsg2Text'
9495

9596
export function initActionMap(adapter: Adapter) {
9697
const actionHandlers = [
98+
// llonebot
99+
new VoiceMsg2Text(adapter),
97100
new GetFile(adapter),
98101
new Debug(adapter),
99102
new GetConfigAction(adapter),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { BaseAction, Schema } from '../BaseAction'
2+
import { readFile } from 'node:fs/promises'
3+
import { ActionName } from '../types'
4+
import { Peer, ElementType } from '@/ntqqapi/types'
5+
import { Type } from 'minato'
6+
import Number = Type.Number
7+
import String = Type.String
8+
9+
export interface Payload {
10+
message_id: string | number
11+
}
12+
13+
export interface Response {
14+
text: string
15+
}
16+
17+
export class VoiceMsg2Text extends BaseAction<Payload, Response> {
18+
actionName = ActionName.VoiceMsg2Text
19+
20+
protected async _handle(payload: Payload): Promise<Response> {
21+
const msgInfo = await this.ctx.store.getMsgInfoByShortId(+payload.message_id)
22+
if (!msgInfo) {
23+
throw new Error('消息不存在')
24+
}
25+
let msg = this.ctx.store.getMsgCache(msgInfo.msgId)
26+
if (!msg) {
27+
const res = await this.ctx.ntMsgApi.getMsgsByMsgId(msgInfo.peer, [msgInfo.msgId])
28+
if (res.msgList.length === 0) {
29+
throw new Error('无法获取该消息')
30+
}
31+
msg = res.msgList[0]
32+
}
33+
const voiceElement = msg.elements.find(e => e.elementType === ElementType.Ptt)
34+
if (!voiceElement) {
35+
throw new Error('该消息不是语音消息')
36+
}
37+
const text = await this.ctx.ntMsgApi.translatePtt2Text(msgInfo.msgId, msgInfo.peer, voiceElement)
38+
if (!text) {
39+
throw new Error('无法转换语音消息为文本')
40+
}
41+
return { text }
42+
}
43+
44+
}
45+

src/onebot11/action/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface InvalidCheckResult {
1111

1212
export enum ActionName {
1313
// llonebot
14+
VoiceMsg2Text = 'voice_msg_to_text',
1415
SendPoke = 'send_poke',
1516
GetGroupIgnoreAddRequest = 'get_group_ignore_add_request',
1617
SetQQAvatar = 'set_qq_avatar',

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs'
22

3-
export const version = '5.0.2'
3+
export const version = '5.1.0'
44

55
export const writeVersion = ()=>{
66
const pkgJsonPath = './package-dist.json'

0 commit comments

Comments
 (0)