【问题标题】:Adding directline voice to waterfall dialog bot using Microsoft bot framework使用 Microsoft bot 框架将直线语音添加到瀑布对话 bot
【发布时间】:2020-11-17 07:50:49
【问题描述】:

我正在尝试在我的对话机器人中添加使用直线语音通道的功能。我正在阅读微软关于如何做到这一点的教程,但他们只是使用回声机器人。我希望能够使用对话机器人并返回声音。我已经在 azure 中创建了语音资源,并在 azure 上的 bot 资源中实现了直线语音通道。有没有人成功地向对话机器人添加语音?我读到会有语音提示选项,但我在 PromptOptions 对象中找不到该属性。

【问题讨论】:

  • 澄清一下,您对直线语音或认知服务语音感兴趣吗?两者的实现略有不同。
  • 我实际上对两者都感兴趣。我启用了直线语音通道,但无法在我的 azure 资源组中的网络聊天中获取麦克风。最终目标是找到一种方法让用户能够使用他们的麦克风。他们可以使用的可能方法是通过 Microsoft Teams。 Cortana,或通过直接线路。

标签: botframework chatbot speech-to-text direct-line-botframework


【解决方案1】:

语音的配置方式取决于您要使用的类型,这也意味着可能会更新您的机器人以及您正在使用的客户端。

关于客户端(即频道)的快速说明 - 频道决定了是否支持语音。例如:

  • 除了设计呼叫机器人外,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' )
);

其他资源:


希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2019-10-11
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多