在不随时可用的邮件中添加语音 - Bot服务 - Azure

时间:2018-01-25 04:15:17

标签: azure text-to-speech speech-to-text chatbot

我使用Azure服务创建了一个bot。你可以在这里参考。 https://gin-botapp.azurewebsites.net/

我按照

中的说明逐步启用此机器人的语音文本

如何在网络聊天中启用语音:https://docs.microsoft.com/en-us/bot-framework/bot-service-channel-connect-webchat-speech

向消息添加语音(C#):https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-text-to-speech

我的问题是机器人会在用户说话时返回语音和文字。但是如果用户输入(不说话),机器人就不会回复语音。

我的代码JS

<script>
        const params = BotChat.queryParams(location.search);
        const user = {
            id: params['userid'] || 'userid',
            name: params['username'] || 'User'
        };
        const bot = {
            id: params['botid'] || 'botid',
            name: params['botname'] || 'botname'
        };
        window.botchatDebug = params['debug'] && params['debug'] === 'true';
        function getToken() {
            return fetch(
                'https://api.cognitive.microsoft.com/sts/v1.0/issueToken',
                {
                    headers: {
                        'Ocp-Apim-Subscription-Key': 'KEYXXXXXXXXXXXX'
                    },
                    method: 'POST'
                }
            ).then(res => res.text());
        }

        const speechOptions = {
            speechRecognizer: new CognitiveServices.SpeechRecognizer({
                fetchCallback: (authFetchEventId) => getToken(),
                fetchOnExpiryCallback: (authFetchEventId) => getToken()
            }),
            speechSynthesizer: new CognitiveServices.SpeechSynthesizer({
                gender: CognitiveServices.SynthesisGender.Female,
                subscriptionKey: 'KEYXXXXXXXXXXXX',
                voiceName: 'Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)'
            })
        };

        BotChat.App({
            bot: bot,
            locale: params['locale'],
            resize: 'detect',
            user: user,
            sendTyping: true,
            speechOptions: speechOptions,
            directLine: {
                domain: params['domain'],
                secret: "KEYYYYYYYYYYYYYYYYYY",
                token: "KEYYYYYYYYYYYYYYYYYY",
                webSocket: params['webSocket'] && params['webSocket'] === 'true'
            }
        }, document.getElementById('BotChatGoesHere'));
    </script>

和C#

else if (message.Type == ActivityTypes.ConversationUpdate)
            {
                IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
                if (iConversationUpdated != null)
                {
                    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

                    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                    {
                        // if the bot is added, then 
                        if (member.Id == iConversationUpdated.Recipient.Id)
                        {
                            var rpText = APIController.RequestAsync("hi", message.Conversation.Id);
                            var reply = ((Activity)iConversationUpdated).CreateReply(rpText.Result.Fulfillment.Speech);
                            reply.Speak = rpText.Result.Fulfillment.Speech;
                            reply.InputHint = InputHints.IgnoringInput;
                            reply.TextFormat = "plain";
                            reply.Locale = "en-Us";
                            reply.Text = rpText.Result.Fulfillment.Speech;

                            await connector.Conversations.ReplyToActivityAsync(reply);
                        }
                    }
                }
            }

我需要机器人总是返回文字和语音,请帮我解决这个问题。

0 个答案:

没有答案