语音的配置方式取决于您要使用的类型,这也意味着可能会更新您的机器人以及您正在使用的客户端。
关于客户端(即频道)的快速说明 - 频道决定了是否支持语音。例如:
- 除了设计呼叫机器人外,Teams 不支持 STT(语音转文本)。
- Cortana 不再是公开发售,现在只能通过企业帐户访问。
- 建立在 Direct Line 之上并集成了 DL 语音的网络聊天,同时支持 CS 和 DL 语音
- 请务必注意,DL Speech 是独立于 Direct Line 的自己的通道,因此它需要在您的机器人中添加额外的代码。位于BotBuilder-Samples 的示例默认包含此代码。
- 使用 DL 或 DL Speech 的自定义客户端将要求您内置允许 Speech 工作的功能。
- 其他频道,例如 Telegram、Slack 等,是特定于频道的,当然,不依赖 CS 或 DL Speech 来启用。定制的第三方频道支持演讲需要您查阅该频道的文档以进行实施。
关于 DL Speech,您需要添加/更新您的机器人的 index.js 代码以包含以下内容:
[...]
// Catch-all for errors.
const onTurnErrorHandler = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights. See https://aka.ms/bottelemetry for telemetry
// configuration instructions.
console.error(`\n [onTurnError] unhandled error: ${ error }`);
// Send a trace activity, which will be displayed in Bot Framework Emulator
await context.sendTraceActivity(
'OnTurnError Trace',
`${ error }`,
'https://www.botframework.com/schemas/error',
'TurnError'
);
// Send a message to the user
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue to run this bot, please fix the bot source code.');
};
// Set the onTurnError for the singleton BotFrameworkAdapter.
adapter.onTurnError = onTurnErrorHandler;
[...]
// Listen for Upgrade requests for Streaming.
server.on('upgrade', (req, socket, head) => {
// Create an adapter scoped to this WebSocket connection to allow storing session data.
const streamingAdapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
// Set onTurnError for the BotFrameworkAdapter created for each connection.
streamingAdapter.onTurnError = onTurnErrorHandler;
streamingAdapter.useWebSocket(req, socket, head, async (context) => {
// After connecting via WebSocket, run this logic for every request sent over
// the WebSocket connection.
await myBot.run(context);
});
});
然后,在网络聊天中,您将传递以下内容。 (您可以在此 DL Speech sample 中引用以下代码。另外,请注意,您需要将“获取”地址更新为您自己的 API 以生成令牌。):
[...]
const fetchCredentials = async () => {
const res = await fetch('https://webchat-mockbot-streaming.azurewebsites.net/speechservices/token', {
method: 'POST'
});
if (!res.ok) {
throw new Error('Failed to fetch authorization token and region.');
}
const { region, token: authorizationToken } = await res.json();
return { authorizationToken, region };
};
// Create a set of adapters for Web Chat to use with Direct Line Speech channel.
const adapters = await window.WebChat.createDirectLineSpeechAdapters({
fetchCredentials
});
// Pass the set of adapters to Web Chat.
window.WebChat.renderWebChat(
{
...adapters
},
document.getElementById('webchat')
);
[...]
以下是一些附加资源,可帮助您更好地理解 DL Speech:
关于 CS Speech,您需要有一个活跃的Cognitive Services subscription。在 Azure 中设置好语音服务后,您可以使用订阅密钥生成用于启用 CS Speech 的令牌(您也可以参考此 Web Chat sample。启用时无需更改您的机器人。(同样,您将需要设置一个 API 来生成令牌,因为最佳做法是不在 HTML 中包含任何键。这是我在此示例中为获取 DL 令牌所做的操作):
let authorizationToken;
let region = '<<SPEECH SERVICES REGION>>';
const response = await fetch( `https://${ region }.api.cognitive.microsoft.com/sts/v1.0/issueToken`, {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': '<<SUBSCRIPTION KEY>>'
}
} );
if ( response.status === 200 ) {
authorizationToken = await response.text(),
region
} else {
console.log( 'error' )
}
const webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory( {
authorizationToken,
region
} );
const res = await fetch( 'http://localhost:3500/directline/token', { method: 'POST' } );
const { token } = await res.json();
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine( {
token: token
} ),
webSpeechPonyfillFactory: webSpeechPonyfillFactory,
},
document.getElementById( 'webchat' )
);
其他资源:
希望有帮助!