@juzi/wechaty 1.0.149 → 1.0.155
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/mods/impls.d.ts +3 -3
- package/dist/cjs/src/mods/impls.d.ts.map +1 -1
- package/dist/cjs/src/mods/impls.js +2 -1
- package/dist/cjs/src/mods/impls.js.map +1 -1
- package/dist/cjs/src/mods/users.d.ts +1 -1
- package/dist/cjs/src/mods/users.d.ts.map +1 -1
- package/dist/cjs/src/package-json.js +4 -4
- package/dist/cjs/src/user-modules/message.d.ts +15 -0
- package/dist/cjs/src/user-modules/message.d.ts.map +1 -1
- package/dist/cjs/src/user-modules/message.js +20 -0
- package/dist/cjs/src/user-modules/message.js.map +1 -1
- package/dist/cjs/src/user-modules/mod.d.ts +4 -3
- package/dist/cjs/src/user-modules/mod.d.ts.map +1 -1
- package/dist/cjs/src/user-modules/mod.js +3 -1
- package/dist/cjs/src/user-modules/mod.js.map +1 -1
- package/dist/cjs/src/user-modules/voice.d.ts +72 -0
- package/dist/cjs/src/user-modules/voice.d.ts.map +1 -0
- package/dist/cjs/src/user-modules/voice.js +116 -0
- package/dist/cjs/src/user-modules/voice.js.map +1 -0
- package/dist/cjs/src/user-modules/voice.spec.d.ts +3 -0
- package/dist/cjs/src/user-modules/voice.spec.d.ts.map +1 -0
- package/dist/cjs/src/user-modules/voice.spec.js +155 -0
- package/dist/cjs/src/user-modules/voice.spec.js.map +1 -0
- package/dist/cjs/src/wechaty/wechaty-base.d.ts +10 -0
- package/dist/cjs/src/wechaty/wechaty-base.d.ts.map +1 -1
- package/dist/cjs/src/wechaty/wechaty-impl.spec.js +25 -23
- package/dist/cjs/src/wechaty/wechaty-impl.spec.js.map +1 -1
- package/dist/cjs/src/wechaty-mixins/login-mixin.d.ts +12 -0
- package/dist/cjs/src/wechaty-mixins/login-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/misc-mixin.d.ts +6 -0
- package/dist/cjs/src/wechaty-mixins/misc-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/plugin-mixin.d.ts +8 -12
- package/dist/cjs/src/wechaty-mixins/plugin-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts +4 -0
- package/dist/cjs/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +4 -2
- package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.d.ts.map +1 -1
- package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.js +3 -0
- package/dist/cjs/src/wechaty-mixins/wechatify-user-module-mixin.js.map +1 -1
- package/dist/esm/src/mods/impls.d.ts +3 -3
- package/dist/esm/src/mods/impls.d.ts.map +1 -1
- package/dist/esm/src/mods/impls.js +1 -1
- package/dist/esm/src/mods/impls.js.map +1 -1
- package/dist/esm/src/mods/users.d.ts +1 -1
- package/dist/esm/src/mods/users.d.ts.map +1 -1
- package/dist/esm/src/package-json.js +4 -4
- package/dist/esm/src/user-modules/message.d.ts +15 -0
- package/dist/esm/src/user-modules/message.d.ts.map +1 -1
- package/dist/esm/src/user-modules/message.js +20 -0
- package/dist/esm/src/user-modules/message.js.map +1 -1
- package/dist/esm/src/user-modules/mod.d.ts +4 -3
- package/dist/esm/src/user-modules/mod.d.ts.map +1 -1
- package/dist/esm/src/user-modules/mod.js +2 -1
- package/dist/esm/src/user-modules/mod.js.map +1 -1
- package/dist/esm/src/user-modules/voice.d.ts +72 -0
- package/dist/esm/src/user-modules/voice.d.ts.map +1 -0
- package/dist/esm/src/user-modules/voice.js +113 -0
- package/dist/esm/src/user-modules/voice.js.map +1 -0
- package/dist/esm/src/user-modules/voice.spec.d.ts +3 -0
- package/dist/esm/src/user-modules/voice.spec.d.ts.map +1 -0
- package/dist/esm/src/user-modules/voice.spec.js +153 -0
- package/dist/esm/src/user-modules/voice.spec.js.map +1 -0
- package/dist/esm/src/wechaty/wechaty-base.d.ts +10 -0
- package/dist/esm/src/wechaty/wechaty-base.d.ts.map +1 -1
- package/dist/esm/src/wechaty/wechaty-impl.spec.js +25 -23
- package/dist/esm/src/wechaty/wechaty-impl.spec.js.map +1 -1
- package/dist/esm/src/wechaty-mixins/login-mixin.d.ts +12 -0
- package/dist/esm/src/wechaty-mixins/login-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/misc-mixin.d.ts +6 -0
- package/dist/esm/src/wechaty-mixins/misc-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/plugin-mixin.d.ts +8 -12
- package/dist/esm/src/wechaty-mixins/plugin-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts +4 -0
- package/dist/esm/src/wechaty-mixins/puppet-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.d.ts +4 -2
- package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.d.ts.map +1 -1
- package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.js +4 -1
- package/dist/esm/src/wechaty-mixins/wechatify-user-module-mixin.js.map +1 -1
- package/package.json +3 -3
- package/src/mods/impls.ts +3 -0
- package/src/mods/users.ts +1 -0
- package/src/package-json.ts +4 -4
- package/src/user-modules/message.ts +24 -0
- package/src/user-modules/mod.ts +8 -0
- package/src/user-modules/voice.spec.ts +187 -0
- package/src/user-modules/voice.ts +159 -0
- package/src/wechaty/wechaty-impl.spec.ts +3 -0
- package/src/wechaty-mixins/wechatify-user-module-mixin.ts +6 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
|
|
2
|
+
/**
|
|
3
|
+
* Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
|
|
4
|
+
*
|
|
5
|
+
* @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
|
|
6
|
+
* Wechaty Contributors <https://github.com/wechaty>.
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
import {
|
|
22
|
+
test,
|
|
23
|
+
sinon,
|
|
24
|
+
} from 'tstest'
|
|
25
|
+
|
|
26
|
+
import { FileBox } from 'file-box'
|
|
27
|
+
import type * as PUPPET from '@juzi/wechaty-puppet'
|
|
28
|
+
import { PuppetMock } from '@juzi/wechaty-puppet-mock'
|
|
29
|
+
import { WechatyBuilder } from '../wechaty-builder.js'
|
|
30
|
+
|
|
31
|
+
const MESSAGE_ID = 'message-id-voice'
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The fallback must trigger ONLY when the puppet does not implement the new
|
|
35
|
+
* RPC, never on transient failures: otherwise `text()` silently drops the
|
|
36
|
+
* `noSpeech` discriminator and makes the bot re-run its own paid ASR.
|
|
37
|
+
*
|
|
38
|
+
* The puppet is built & started once and reused across cases (`wechaty.Voice`
|
|
39
|
+
* is unavailable before `start()`). `messageVoice` / `messageVoiceText` are
|
|
40
|
+
* assigned directly (rather than via `sinon.stub(puppet, ...)`) because the
|
|
41
|
+
* installed wechaty-puppet-mock predates those methods, so stubbing a
|
|
42
|
+
* non-existent property would throw.
|
|
43
|
+
*/
|
|
44
|
+
const puppet = new PuppetMock() as any
|
|
45
|
+
const wechaty = WechatyBuilder.build({ puppet })
|
|
46
|
+
|
|
47
|
+
let started = false
|
|
48
|
+
async function ensureStarted () {
|
|
49
|
+
if (!started) {
|
|
50
|
+
await wechaty.start()
|
|
51
|
+
started = true
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const voice = () => wechaty.Voice.create(MESSAGE_ID)
|
|
56
|
+
|
|
57
|
+
const UNSUPPORTED = new Error('Wechaty Puppet Unsupported API Error.')
|
|
58
|
+
|
|
59
|
+
test('Voice.file() returns the dedicated messageVoice result when supported', async t => {
|
|
60
|
+
await ensureStarted()
|
|
61
|
+
puppet.messageVoice = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('voice'), 'voice.silk'))
|
|
62
|
+
puppet.messageFile = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('file'), 'file.bin'))
|
|
63
|
+
|
|
64
|
+
const fileBox = await voice().file()
|
|
65
|
+
t.equal(fileBox.name, 'voice.silk', 'should return the messageVoice FileBox')
|
|
66
|
+
t.ok(puppet.messageVoice.calledOnceWith(MESSAGE_ID), 'should call messageVoice')
|
|
67
|
+
t.notOk(puppet.messageFile.called, 'should NOT fall back to messageFile when supported')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('Voice.file() falls back to messageFile on unsupported error', async t => {
|
|
71
|
+
await ensureStarted()
|
|
72
|
+
puppet.messageVoice = sinon.stub().rejects(UNSUPPORTED)
|
|
73
|
+
puppet.messageFile = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('file'), 'file.bin'))
|
|
74
|
+
|
|
75
|
+
const fileBox = await voice().file()
|
|
76
|
+
t.equal(fileBox.name, 'file.bin', 'should fall back to messageFile')
|
|
77
|
+
t.ok(puppet.messageFile.calledOnceWith(MESSAGE_ID), 'should call messageFile on fallback')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('Voice.file() falls back to messageFile when messageVoice is absent (old puppet)', async t => {
|
|
81
|
+
await ensureStarted()
|
|
82
|
+
puppet.messageVoice = undefined // old puppet built without the method at all
|
|
83
|
+
puppet.messageFile = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('file'), 'file.bin'))
|
|
84
|
+
|
|
85
|
+
const fileBox = await voice().file()
|
|
86
|
+
t.equal(fileBox.name, 'file.bin', 'should fall back to messageFile via the typeof guard')
|
|
87
|
+
t.ok(puppet.messageFile.calledOnceWith(MESSAGE_ID), 'should call messageFile on fallback')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('Voice.file() rethrows a transient error instead of falling back', async t => {
|
|
91
|
+
await ensureStarted()
|
|
92
|
+
puppet.messageVoice = sinon.stub().rejects(new Error('socket hang up'))
|
|
93
|
+
puppet.messageFile = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('file'), 'file.bin'))
|
|
94
|
+
|
|
95
|
+
await t.rejects(() => voice().file(), /socket hang up/, 'should rethrow the transient error')
|
|
96
|
+
t.notOk(puppet.messageFile.called, 'should NOT fall back on a transient error')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('Voice.file() rethrows a genuine "is not a function" TypeError from a working messageVoice', async t => {
|
|
100
|
+
await ensureStarted()
|
|
101
|
+
// method exists but its implementation throws internally — must NOT be
|
|
102
|
+
// misclassified as unsupported just because the message says "is not a function"
|
|
103
|
+
puppet.messageVoice = sinon.stub().rejects(new TypeError('innerHelper.process is not a function'))
|
|
104
|
+
puppet.messageFile = sinon.stub().resolves(FileBox.fromBuffer(Buffer.from('file'), 'file.bin'))
|
|
105
|
+
|
|
106
|
+
await t.rejects(() => voice().file(), /is not a function/, 'should rethrow the internal TypeError')
|
|
107
|
+
t.notOk(puppet.messageFile.called, 'should NOT mask a real implementation bug')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('Voice.text() returns the dedicated messageVoiceText payload when supported', async t => {
|
|
111
|
+
await ensureStarted()
|
|
112
|
+
puppet.messageVoiceText = sinon.stub().resolves({ text: 'hello', noSpeech: false })
|
|
113
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy' } as PUPPET.payloads.Message)
|
|
114
|
+
|
|
115
|
+
const result = await voice().text()
|
|
116
|
+
t.same(result, { text: 'hello', noSpeech: false }, 'should return the messageVoiceText payload')
|
|
117
|
+
t.ok(puppet.messageVoiceText.calledOnceWith(MESSAGE_ID), 'should call messageVoiceText')
|
|
118
|
+
t.notOk(puppet.messagePayload.called, 'should NOT fall back to messagePayload when supported')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('Voice.text() preserves noSpeech=true from the puppet (skip paid ASR)', async t => {
|
|
122
|
+
await ensureStarted()
|
|
123
|
+
puppet.messageVoiceText = sinon.stub().resolves({ text: '', noSpeech: true })
|
|
124
|
+
puppet.messagePayload = sinon.stub().resolves({ text: '' } as PUPPET.payloads.Message)
|
|
125
|
+
|
|
126
|
+
const result = await voice().text()
|
|
127
|
+
t.same(result, { text: '', noSpeech: true }, 'should return the puppet payload verbatim')
|
|
128
|
+
t.equal(result.noSpeech, true, 'should preserve noSpeech=true so the bot can skip its own ASR')
|
|
129
|
+
t.ok(puppet.messageVoiceText.calledOnceWith(MESSAGE_ID), 'should call messageVoiceText')
|
|
130
|
+
t.notOk(puppet.messagePayload.called, 'should NOT fall back when supported')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('Voice.text() falls back to messagePayload().text on unsupported error', async t => {
|
|
134
|
+
await ensureStarted()
|
|
135
|
+
puppet.messageVoiceText = sinon.stub().rejects(UNSUPPORTED)
|
|
136
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy asr' } as PUPPET.payloads.Message)
|
|
137
|
+
|
|
138
|
+
const result = await voice().text()
|
|
139
|
+
t.same(result, { text: 'legacy asr', noSpeech: false }, 'should fall back to legacy payload.text with noSpeech=false')
|
|
140
|
+
t.ok(puppet.messagePayload.calledOnceWith(MESSAGE_ID), 'should call messagePayload on fallback')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('Voice.text() falls back on gRPC UNIMPLEMENTED (code 12)', async t => {
|
|
144
|
+
await ensureStarted()
|
|
145
|
+
const unimplemented = Object.assign(new Error('12 UNIMPLEMENTED'), { code: 12 })
|
|
146
|
+
puppet.messageVoiceText = sinon.stub().rejects(unimplemented)
|
|
147
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy asr' } as PUPPET.payloads.Message)
|
|
148
|
+
|
|
149
|
+
const result = await voice().text()
|
|
150
|
+
t.same(result, { text: 'legacy asr', noSpeech: false }, 'should fall back when the server returns UNIMPLEMENTED')
|
|
151
|
+
t.ok(puppet.messagePayload.calledOnceWith(MESSAGE_ID), 'should call messagePayload on fallback')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('Voice.text() falls back to messagePayload when messageVoiceText is absent (old puppet)', async t => {
|
|
155
|
+
await ensureStarted()
|
|
156
|
+
puppet.messageVoiceText = undefined // old puppet built without the method at all
|
|
157
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy asr' } as PUPPET.payloads.Message)
|
|
158
|
+
|
|
159
|
+
const result = await voice().text()
|
|
160
|
+
t.same(result, { text: 'legacy asr', noSpeech: false }, 'should fall back via the typeof guard with noSpeech=false')
|
|
161
|
+
t.ok(puppet.messagePayload.calledOnceWith(MESSAGE_ID), 'should call messagePayload on fallback')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('Voice.text() rethrows a transient error instead of falling back', async t => {
|
|
165
|
+
await ensureStarted()
|
|
166
|
+
puppet.messageVoiceText = sinon.stub().rejects(new Error('ETIMEDOUT'))
|
|
167
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy asr' } as PUPPET.payloads.Message)
|
|
168
|
+
|
|
169
|
+
await t.rejects(() => voice().text(), /ETIMEDOUT/, 'should rethrow the transient error')
|
|
170
|
+
t.notOk(puppet.messagePayload.called, 'should NOT fall back on a transient error (would drop noSpeech)')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('Voice.text() rethrows a genuine "is not a function" TypeError from a working messageVoiceText', async t => {
|
|
174
|
+
await ensureStarted()
|
|
175
|
+
// regression: a real internal TypeError must NOT be downgraded — that would
|
|
176
|
+
// drop noSpeech and make the bot re-run its own paid ASR
|
|
177
|
+
puppet.messageVoiceText = sinon.stub().rejects(new TypeError('innerHelper.process is not a function'))
|
|
178
|
+
puppet.messagePayload = sinon.stub().resolves({ text: 'legacy asr' } as PUPPET.payloads.Message)
|
|
179
|
+
|
|
180
|
+
await t.rejects(() => voice().text(), /is not a function/, 'should rethrow the internal TypeError')
|
|
181
|
+
t.notOk(puppet.messagePayload.called, 'should NOT mask a real implementation bug')
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
test('teardown: stop the shared wechaty', async t => {
|
|
185
|
+
await wechaty.stop()
|
|
186
|
+
t.pass('stopped')
|
|
187
|
+
})
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
|
|
3
|
+
*
|
|
4
|
+
* @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
|
|
5
|
+
* Wechaty Contributors <https://github.com/wechaty>.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
import type {
|
|
21
|
+
FileBoxInterface,
|
|
22
|
+
} from 'file-box'
|
|
23
|
+
import type { Constructor } from 'clone-class'
|
|
24
|
+
import type * as PUPPET from '@juzi/wechaty-puppet'
|
|
25
|
+
import { validationMixin } from '../user-mixins/validation.js'
|
|
26
|
+
import { log } from '../config.js'
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
wechatifyMixinBase,
|
|
30
|
+
} from '../user-mixins/wechatify.js'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* gRPC status code for an RPC the server does not implement.
|
|
34
|
+
* @see https://grpc.github.io/grpc/core/md_doc_statuscodes.html
|
|
35
|
+
*/
|
|
36
|
+
const GRPC_STATUS_UNIMPLEMENTED = 12
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Whether `e` means the puppet / server does not implement the new voice RPC,
|
|
40
|
+
* i.e. it is safe to fall back to the legacy path.
|
|
41
|
+
*
|
|
42
|
+
* Transient errors (network / timeout / file expired / other gRPC codes) must
|
|
43
|
+
* NOT match here: otherwise the fallback silently masks real failures and — for
|
|
44
|
+
* `text()` — discards the `noSpeech` discriminator, making the bot re-run its
|
|
45
|
+
* own paid ASR on voices the puppet already confirmed as empty.
|
|
46
|
+
*
|
|
47
|
+
* Note: the "method does not exist at all" case (an old puppet built against a
|
|
48
|
+
* wechaty-puppet without the method) is detected up-front via a `typeof` guard
|
|
49
|
+
* at the call site, NOT here — matching the `'is not a function'` text would
|
|
50
|
+
* also swallow a genuine `TypeError` raised inside a working implementation.
|
|
51
|
+
*/
|
|
52
|
+
function isUnsupportedError (e: unknown): boolean {
|
|
53
|
+
const err = e as { code?: number | string; message?: string } | undefined
|
|
54
|
+
// old wechaty-puppet-service server without the new RPC → gRPC UNIMPLEMENTED
|
|
55
|
+
if (err?.code === GRPC_STATUS_UNIMPLEMENTED) {
|
|
56
|
+
return true
|
|
57
|
+
}
|
|
58
|
+
// puppet implements the abstract method via throwUnsupportedError()
|
|
59
|
+
return (err?.message || '').includes('Unsupported API')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class VoiceMixin extends wechatifyMixinBase() {
|
|
63
|
+
|
|
64
|
+
static create (id: string): VoiceInterface {
|
|
65
|
+
log.verbose('Voice', 'static create(%s)', id)
|
|
66
|
+
|
|
67
|
+
const voice = new this(id)
|
|
68
|
+
return voice
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
constructor (
|
|
72
|
+
public id: string,
|
|
73
|
+
) {
|
|
74
|
+
super()
|
|
75
|
+
log.verbose('Voice', 'constructor(%s)', id)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get the voice file (FileBox) via the puppet's dedicated `messageVoice` RPC.
|
|
80
|
+
*
|
|
81
|
+
* Falls back to the generic `messageFile` when the puppet does not implement
|
|
82
|
+
* `messageVoice` — either the method is absent (old puppet built without it,
|
|
83
|
+
* caught by the `typeof` guard) or it rejects with an unsupported-API error.
|
|
84
|
+
* Keeps the behaviour compatible with the legacy "voice file via messageFile"
|
|
85
|
+
* path. A genuine runtime error from a working `messageVoice` is rethrown.
|
|
86
|
+
*/
|
|
87
|
+
async file (): Promise<FileBoxInterface> {
|
|
88
|
+
log.verbose('Voice', 'file() for id: "%s"', this.id)
|
|
89
|
+
const puppet = this.wechaty.puppet
|
|
90
|
+
// puppet built against an old wechaty-puppet without the method at all
|
|
91
|
+
if (typeof (puppet as { messageVoice?: unknown }).messageVoice !== 'function') {
|
|
92
|
+
log.verbose('Voice', 'file() messageVoice() absent, fallback to messageFile()')
|
|
93
|
+
return puppet.messageFile(this.id)
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const fileBox = await puppet.messageVoice(this.id)
|
|
97
|
+
return fileBox
|
|
98
|
+
} catch (e) {
|
|
99
|
+
if (!isUnsupportedError(e)) {
|
|
100
|
+
throw e
|
|
101
|
+
}
|
|
102
|
+
log.verbose('Voice', 'file() messageVoice() unsupported, fallback to messageFile(): %s', (e as Error).message)
|
|
103
|
+
const fileBox = await puppet.messageFile(this.id)
|
|
104
|
+
return fileBox
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the voice-to-text (ASR) result via the puppet's dedicated
|
|
110
|
+
* `messageVoiceText` RPC. Returns a discriminated result so the bot can tell
|
|
111
|
+
* "confirmed no speech" (`noSpeech=true`) from "normal transcription".
|
|
112
|
+
*
|
|
113
|
+
* Falls back to reading the legacy `messagePayload().text` when the puppet
|
|
114
|
+
* does not implement `messageVoiceText` — either the method is absent (old
|
|
115
|
+
* puppet built without it, caught by the `typeof` guard) or it rejects with
|
|
116
|
+
* an unsupported-API error. Old puppets put the ASR result into
|
|
117
|
+
* `payload.text`, so this keeps the behaviour compatible; `noSpeech` is
|
|
118
|
+
* `false` in the fallback. A genuine runtime error is rethrown (so the bot
|
|
119
|
+
* does not silently drop `noSpeech` and re-run its own paid ASR).
|
|
120
|
+
*/
|
|
121
|
+
async text (): Promise<PUPPET.payloads.VoiceText> {
|
|
122
|
+
log.verbose('Voice', 'text() for id: "%s"', this.id)
|
|
123
|
+
const puppet = this.wechaty.puppet
|
|
124
|
+
// puppet built against an old wechaty-puppet without the method at all
|
|
125
|
+
if (typeof (puppet as { messageVoiceText?: unknown }).messageVoiceText !== 'function') {
|
|
126
|
+
log.verbose('Voice', 'text() messageVoiceText() absent, fallback to messagePayload().text')
|
|
127
|
+
const payload = await puppet.messagePayload(this.id)
|
|
128
|
+
return { text: payload.text || '', noSpeech: false }
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const payload = await puppet.messageVoiceText(this.id)
|
|
132
|
+
return payload
|
|
133
|
+
} catch (e) {
|
|
134
|
+
if (!isUnsupportedError(e)) {
|
|
135
|
+
throw e
|
|
136
|
+
}
|
|
137
|
+
log.verbose('Voice', 'text() messageVoiceText() unsupported, fallback to messagePayload().text: %s', (e as Error).message)
|
|
138
|
+
const payload = await puppet.messagePayload(this.id)
|
|
139
|
+
return { text: payload.text || '', noSpeech: false }
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
class VoiceImpl extends validationMixin(VoiceMixin)<VoiceInterface>() {}
|
|
146
|
+
interface VoiceInterface extends VoiceImpl { }
|
|
147
|
+
|
|
148
|
+
type VoiceConstructor = Constructor<
|
|
149
|
+
VoiceInterface,
|
|
150
|
+
typeof VoiceImpl
|
|
151
|
+
>
|
|
152
|
+
|
|
153
|
+
export type {
|
|
154
|
+
VoiceConstructor,
|
|
155
|
+
VoiceInterface,
|
|
156
|
+
}
|
|
157
|
+
export {
|
|
158
|
+
VoiceImpl,
|
|
159
|
+
}
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
ContactConstructor,
|
|
10
10
|
FriendshipConstructor,
|
|
11
11
|
ImageConstructor,
|
|
12
|
+
VoiceConstructor,
|
|
12
13
|
LocationConstructor,
|
|
13
14
|
MessageConstructor,
|
|
14
15
|
MiniProgramConstructor,
|
|
@@ -51,6 +52,7 @@ test('Wechaty interface', async t => {
|
|
|
51
52
|
Delay : DelayConstructor
|
|
52
53
|
Friendship : FriendshipConstructor
|
|
53
54
|
Image : ImageConstructor
|
|
55
|
+
Voice : VoiceConstructor
|
|
54
56
|
Location : LocationConstructor
|
|
55
57
|
Message : MessageConstructor
|
|
56
58
|
MiniProgram : MiniProgramConstructor
|
|
@@ -82,6 +84,7 @@ test('Wechaty interface', async t => {
|
|
|
82
84
|
= this.Delay
|
|
83
85
|
= this.Friendship
|
|
84
86
|
= this.Image
|
|
87
|
+
= this.Voice
|
|
85
88
|
= this.Location
|
|
86
89
|
= this.Message
|
|
87
90
|
= this.MiniProgram
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
DelayImpl,
|
|
8
8
|
FriendshipImpl,
|
|
9
9
|
ImageImpl,
|
|
10
|
+
VoiceImpl,
|
|
10
11
|
LocationImpl,
|
|
11
12
|
MessageImpl,
|
|
12
13
|
MiniProgramImpl,
|
|
@@ -35,6 +36,7 @@ import {
|
|
|
35
36
|
DelayConstructor,
|
|
36
37
|
FriendshipConstructor,
|
|
37
38
|
ImageConstructor,
|
|
39
|
+
VoiceConstructor,
|
|
38
40
|
LocationConstructor,
|
|
39
41
|
MessageConstructor,
|
|
40
42
|
MiniProgramConstructor,
|
|
@@ -79,6 +81,7 @@ const wechatifyUserModuleMixin = <MixinBase extends typeof WechatySkeleton> (mix
|
|
|
79
81
|
__wechatifiedDelay? : DelayConstructor
|
|
80
82
|
__wechatifiedFriendship? : FriendshipConstructor
|
|
81
83
|
__wechatifiedImage? : ImageConstructor
|
|
84
|
+
__wechatifiedVoice? : VoiceConstructor
|
|
82
85
|
__wechatifiedLocation? : LocationConstructor
|
|
83
86
|
__wechatifiedMessage? : MessageConstructor
|
|
84
87
|
__wechatifiedMiniProgram? : MiniProgramConstructor
|
|
@@ -107,6 +110,7 @@ const wechatifyUserModuleMixin = <MixinBase extends typeof WechatySkeleton> (mix
|
|
|
107
110
|
get Delay () : DelayConstructor { return guardWechatify(this.__wechatifiedDelay) }
|
|
108
111
|
get Friendship () : FriendshipConstructor { return guardWechatify(this.__wechatifiedFriendship) }
|
|
109
112
|
get Image () : ImageConstructor { return guardWechatify(this.__wechatifiedImage) }
|
|
113
|
+
get Voice () : VoiceConstructor { return guardWechatify(this.__wechatifiedVoice) }
|
|
110
114
|
get Location () : LocationConstructor { return guardWechatify(this.__wechatifiedLocation) }
|
|
111
115
|
get Message () : MessageConstructor { return guardWechatify(this.__wechatifiedMessage) }
|
|
112
116
|
get MiniProgram () : MiniProgramConstructor { return guardWechatify(this.__wechatifiedMiniProgram) }
|
|
@@ -155,6 +159,7 @@ const wechatifyUserModuleMixin = <MixinBase extends typeof WechatySkeleton> (mix
|
|
|
155
159
|
this.__wechatifiedDelay = wechatifyUserModule(DelayImpl)(this as any)
|
|
156
160
|
this.__wechatifiedFriendship = wechatifyUserModule(FriendshipImpl)(this as any)
|
|
157
161
|
this.__wechatifiedImage = wechatifyUserModule(ImageImpl)(this as any)
|
|
162
|
+
this.__wechatifiedVoice = wechatifyUserModule(VoiceImpl)(this as any)
|
|
158
163
|
this.__wechatifiedLocation = wechatifyUserModule(LocationImpl)(this as any)
|
|
159
164
|
this.__wechatifiedMessage = wechatifyUserModule(MessageImpl)(this as any)
|
|
160
165
|
this.__wechatifiedMiniProgram = wechatifyUserModule(MiniProgramImpl)(this as any)
|
|
@@ -204,6 +209,7 @@ type ProtectedPropertyWechatifyUserModuleMixin =
|
|
|
204
209
|
| '__wechatifiedDelay'
|
|
205
210
|
| '__wechatifiedFriendship'
|
|
206
211
|
| '__wechatifiedImage'
|
|
212
|
+
| '__wechatifiedVoice'
|
|
207
213
|
| '__wechatifiedLocation'
|
|
208
214
|
| '__wechatifiedMessage'
|
|
209
215
|
| '__wechatifiedMiniProgram'
|