Node.js下Azure Bot Framework Web Chat文本转语音无法识别prompt问题
问题根因
Bot Framework 中使用 Hero Card 格式的 Choice Prompt 时,默认不会将卡片内容自动转换为语音播报文本,只有显式配置了speak字段的消息才会触发 Web Chat 的文本转语音逻辑,普通文本消息默认自带该字段所以可以正常播报。
修复步骤
1. 后端 Node.js 代码修改
在你现有 waterfall 步骤的 prompt 配置中,新增speak(首次提示的播报内容)和retrySpeak(输入错误重试时的播报内容)两个字段即可,修改后的代码示例如下:
async financeFirstStep(step) { console.log("Inside financeFirstStep::"); // 可根据实际使用语言替换播报文本,以下分别是中英文示例可选其一 // 中文播报文本 const speakText = "请选择与你查询相关的选项,可选内容为账户对账单、信用额度、信用期限、索赔状态、返回首页"; const retrySpeakText = "输入无效,请从上述可用选项中选择"; // 英文播报文本 // const speakText = "Please select an option related to your query: Account Statement, Credit Limit, Credit Term, Status of Claim, Home"; // const retrySpeakText = "Invalid input, please select from the available options listed above"; return await step.prompt(CHOICE_PROMPT, { prompt: 'Please select a option to which your query is related to-', size:'Medium', choices: ChoiceFactory.toChoices(['Account Statement', 'Credit Limit','Credit Term','Status of Claim','Home']), style: ListStyle.heroCard, retryPrompt: 'Invalid value entered. \r\nPlease select from the available options-', // 新增语音播报配置 speak: speakText, retrySpeak: retrySpeakText, // 可选配置:播报结束后自动等待用户语音输入 inputHint: 'expectingInput' }); }
2. 前端适配优化(可选)
如果修改后端后播报仍未生效,可在前端 Web Chat 渲染配置中添加发音人指定逻辑,避免语音引擎适配异常,修改后的前端核心代码如下:
window.WebChat.renderWebChat({ directLine: window.WebChat.createDirectLine({ secret: 'SECRET', watermark: 10 }), userID: "USERID", styleOptions, store, webSpeechPonyfillFactory: await createCognitiveServicesSpeechServicesPonyfillFactory({ credentials: { region: 'eastus', subscriptionKey: 'subscriptionKey' } }), // 新增发音人配置,lang参数可根据实际使用语言调整 selectVoice: (voices) => { // 中文发音人示例 return voices.find(({ lang }) => lang === 'zh-CN') || // 英文发音人示例 // return voices.find(({ lang }) => lang === 'en-US') || voices[0]; } }, document.getElementById('webchat'));
修改完成后重启后端服务、刷新前端页面即可生效,prompt 触发时会自动播报你配置的语音文本。
内容的提问来源于stack exchange,提问作者krati Agarwal
相关产品推荐
相关产品推荐

