Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion ds-bot/src/host/im/platforms/qq.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const INTENTS = 1 << 25
const apiBase = env => String(env.config?.api ?? API).replace(/\/+$/, '')
const tokenBase = env => String(env.config?.token ?? TOKEN_API)

// A voice message carries no `content`: the transcript rides on the attachment
// as `asr_refer_text`, which the platform produced itself. Without this the
// message normalises to an empty text and the common IM entry drops it (#1824).
// A caption the sender typed alongside the voice wins over the transcript.
const textOf = d => {
const content = String(d.content ?? '').trim()
if (content !== '') return content
const attachments = Array.isArray(d.attachments) ? d.attachments : []
return attachments
.filter(item => item?.content_type === 'voice')
.map(item => String(item?.asr_refer_text ?? '').trim())
.filter(Boolean)
.join('\n')
}

const getToken = async (fields, env) => {
const response = await env.fetch(`${tokenBase(env)}/app/getAppAccessToken`, {
method: 'POST',
Expand Down Expand Up @@ -108,7 +123,7 @@ export default {
await handlers.onMessage({
chat: { key, kind: c2c ? 'direct' : 'group', title: '' },
sender: { id: String(sender ?? ''), name: String(sender ?? '') },
text: String(d.content ?? '').trim(),
text: textOf(d),
images: [], files: [],
mentioned: group, messageId: String(d.id ?? ''),
})
Expand Down
68 changes: 68 additions & 0 deletions ds-bot/test/im-qq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,71 @@ test('a socket that cannot be built at the handshake is retried (#1119)', async
assert.ok(ready !== undefined, 'the connection became ready after a new socket')
assert.equal(fake.sockets.length, 1)
})

test('a voice message arrives as the platform ASR text (#1824)', async (t) => {
const fake = await fakeQq()
const messages = []
const handle = qq.connect(FIELDS, env(fake), { onMessage: async msg => { messages.push(msg) }, onAction: () => {}, onStatus: () => {} })
t.after(() => { handle.stop(); fake.close() })
const ws = await until(() => fake.sockets[0])
await until(() => fake.packets.some(packet => packet.op === 2))
// A QQ voice message carries no content: only an attachment with the
// platform's own transcription.
fake.push(ws, {
op: 0,
t: 'C2C_MESSAGE_CREATE',
s: 2,
d: {
id: 'm1',
content: '',
author: { user_openid: 'u1' },
attachments: [
{ content_type: 'voice', url: 'https://multimedia.nt.qq.com.cn/x.amr', asr_refer_text: '明天下午三点开会', voice_wav_url: 'https://multimedia.nt.qq.com.cn/x.wav' },
],
},
})
await until(() => messages.length === 1)
assert.equal(messages.length, 1, 'the voice message reached the bot')
assert.equal(messages[0].text, '明天下午三点开会')
})

test('a voice attachment without ASR text leaves the text empty', async (t) => {
const fake = await fakeQq()
const messages = []
const handle = qq.connect(FIELDS, env(fake), { onMessage: async msg => { messages.push(msg) }, onAction: () => {}, onStatus: () => {} })
t.after(() => { handle.stop(); fake.close() })
const ws = await until(() => fake.sockets[0])
await until(() => fake.packets.some(packet => packet.op === 2))
fake.push(ws, {
op: 0,
t: 'C2C_MESSAGE_CREATE',
s: 2,
d: { id: 'm1', content: '', author: { user_openid: 'u1' }, attachments: [{ content_type: 'voice', url: 'https://x.amr' }] },
})
// The event still reaches the handler so the core can answer "no text":
// dropping it silently is what the bug report is about.
await until(() => messages.length === 1)
assert.equal(messages[0].text, '')
})

test('a caption on a voice message wins over the ASR text', async (t) => {
const fake = await fakeQq()
const messages = []
const handle = qq.connect(FIELDS, env(fake), { onMessage: async msg => { messages.push(msg) }, onAction: () => {}, onStatus: () => {} })
t.after(() => { handle.stop(); fake.close() })
const ws = await until(() => fake.sockets[0])
await until(() => fake.packets.some(packet => packet.op === 2))
fake.push(ws, {
op: 0,
t: 'C2C_MESSAGE_CREATE',
s: 2,
d: {
id: 'm1',
content: '听一下这个',
author: { user_openid: 'u1' },
attachments: [{ content_type: 'voice', url: 'https://x.amr', asr_refer_text: '明天下午三点开会' }],
},
})
await until(() => messages.length === 1)
assert.equal(messages[0].text, '听一下这个')
})
Loading