使用 Bot Connector API 將語音新增至訊息Add speech to messages with the Bot Connector API

如果您要為具備語音功能的通道 (例如 Cortana) 建置 Bot,您可以建構訊息,其中指定要由 Bot 讀出的文字。If you are building a bot for a speech-enabled channel such as Cortana, you can construct messages that specify the text to be spoken by your bot. 您也可以指定輸入提示,藉由指出您的 Bot 要接受、需要或忽略使用者輸入,來嘗試影響用戶端的麥克風狀態。You can also attempt to influence the state of the client's microphone by specifying an input hint to indicate whether your bot is accepting, expecting, or ignoring user input.

您可以設定 Bot,允許用戶端應用程式透過 Direct Line Speech 通道與其通訊。You can configure your bot to allow client applications to communicate with it through Direct Line Speech channel.

指定要由 Bot 讀出的文字Specify text to be spoken by your bot

若要指定將由 Bot 在啟用語音通道上讀出的文字,請在代表您訊息的 Activity 物件中設定 speak 屬性。To specify text to be spoken by your bot on a speech-enabled channel, set the speak property within the Activity object that represents your message. 您可以將 speak 屬性設定為純文字字串或格式化為語音合成標記語言 (SSML) 的字串,SSML 是一種以 XML 為基礎的標記語言,可讓您控制 Bot 語音的各種特性,例如聲音、速率、音量、發音、音調等等。You can set the speak property to either a plain text string or a string that is formatted as Speech Synthesis Markup Language (SSML), an XML-based markup language that enables you to control various characteristics of your bot's speech such as voice, rate, volume, pronunciation, pitch, and more. 如果通道不支援,訊息會以文字傳遞。If the channel does not support, the message is delivered as text.

下列要求會傳送一則訊息,其中指定要顯示的文字和要讀出的文字,並指出 Bot 需要使用者輸入The following request sends a message that specifies text to be displayed and text to be spoken and indicates that the bot is expecting user input. 其會使用 SSML 格式來指定 speak 屬性,表示應以適度強調的方式讀出 "sure" 這個字。It specifies the speak property using SSML format to indicate that the word "sure" should be spoken with a moderate amount of emphasis. 在此範例要求中,Direct Line 表示基底 URI,您的 Bot 所發出要求的基底 URI 可能和這個不同。In this example request, Direct Line represents the base URI; the base URI for requests that your bot issues may be different. 如需設定基底 URI 的詳細資料,請參閱 API 參考For details about setting the base URI, see API Reference.

POST https://smba.trafficmanager.net/apis/v3/conversations/abcd1234/activities/5d5cdc723
Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json
{
    "type": "message",
    "from": {
        "id": "12345678",
        "name": "sender's name"
    },
    "conversation": {
        "id": "abcd1234",
        "name": "conversation's name"
   },
   "recipient": {
        "id": "1234abcd",
        "name": "recipient's name"
    },
    "text": "Are you sure that you want to cancel this transaction?",
    "speak": "<speak version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"en-US\">Are you <emphasis level=\"moderate\">sure</emphasis> that you want to cancel this transaction?</speak>",
    "inputHint": "expectingInput",
    "replyToId": "5d5cdc723"
}

輸入提示Input hints

當您在啟用語音功能的通道上傳送訊息時,您也可以提供輸入提示,指出 bot 是否接受、預期或忽略使用者輸入,藉以表示用戶端麥克風的預期狀態。When you send a message on a speech-enabled channel, you can express the intended state of the client's microphone by also including an input hint to indicate whether your bot is accepting, expecting, or ignoring user input. 如需詳細資訊,請參閱將輸入提示新增至訊息For more information, see Add input hints to messages.

其他資源Additional resources