如何在直线渠道中创建Be语言机器人

时间:2018-09-25 12:53:52

标签: c# html botframework speech-recognition direct-line-botframework

我正在使用bot框架在直接渠道中创建一个bot。  已经创建了2个bot,一个是英文的,一个是法语的,但是我的想法是仅创建一个bot,并根据用户的需要调用对话框englishluis和frenchluis。

我的机器人也在使用语音识别。

这是我的法国直线代码:

<!DOCTYPE html>
 <html>
<head>
<link href="https://cdn.botframework.com/botframework- 
  webchat/latest/botchat.css" rel="stylesheet" />
   </head>
  <body>
<div id="bot" />
<script src="https://cdn.botframework.com/botframework- 
  webchat/latest/botchat.js"></script>
<script src="https://cdn.botframework.com/botframework- 
    webchat/latest/CognitiveServices.js"></script>
<script>
var speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer(
        { locale: 'fr-fr', subscriptionKey: 
    'xxxxxxx' }),

    speechSynthesizer: new CognitiveServices.SpeechSynthesizer(
        {
            subscriptionKey: 'xxxxxxxxxxx',
            gender: CognitiveServices.SynthesisGender.Female,
            voiceName: 'Microsoft Server Speech Text to Speech Voice (fr-FR, 
         HortenseRUS)'
        })
}

var botConnection = new BotChat.DirectLine({
    // secret: 
         "yF0xxxxxxxxxxxxxxM",
    secret: "yBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    webSocket: true ,

});


  BotChat.App({
        speechOptions: speechOptions,
        botConnection: botConnection,
        user: {
            id: 'userid',
            name: 'User',
        },
        bot: { id: 'botid' },
        resize: 'detect',
        locale: 'fr-FR'

    },
  document.getElementById("bot"));

   function postHelp() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'postHelp',
            type: 'message',
            text: 'help'
        })
        .subscribe(function(id) {
            console.log('"postHelp" sent');
        });
};

function welcome() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'welcome',
            type: 'event',
            value: 'help'
        })
        .subscribe(function (id) {
            console.log('"welcome" sent');
        });
  }
    </script>
   </body>
    </html>

如何创建一个功能来检测来自messagecontroller.cs的语言,或者如果您有其他任何想法,请帮助我。

提前谢谢

directline.html的新代码

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.botframework.com/botframework- 
 webchat/latest/botchat.css" rel="stylesheet" />
 </head>
 <div id="bot" />
  <script src="https://cdn.botframework.com/botframework- 
 webchat/latest/botchat.js"></script>
  <script src="https://cdn.botframework.com/botframework- 
  webchat/latest/CognitiveServices.js"></script>
  <script>
   var speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer(
        { locale: String.localrec, subscriptionKey: 
   'xxxxxxxxxxxxx' }),

    speechSynthesizer: new CognitiveServices.SpeechSynthesizer(
        {
            subscriptionKey: 'xxxxxxxxxxxxxxxx',
            gender: CognitiveServices.SynthesisGender.Female,
           // voiceName: 'Microsoft Server Speech Text to Speech Voice (fr- 
         FR, HortenseRUS)'
            voiceName: String.localevoice

        })
}

var botConnection = new BotChat.DirectLine({
    // secret: "yxxxxxxxxxxxxxxxM",
    secret: "yxxxxxxxxxxxxxxxxxxx",
    webSocket: true ,

  });


      BotChat.App({
        speechOptions: speechOptions,
        botConnection: botConnection,
        user: {
            id: 'userid',
            name: 'User',
        },
        bot: { id: 'botid' },
        resize: 'detect',
        locale: String.localed

    },
document.getElementById("bot"));

function postHelp() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'postHelp',
            type: 'message',
            text: 'help'
        })
        .subscribe(function(id) {
            console.log('"postHelp" sent');
        });
};

function welcome() {
    botConnection
        .postActivity({
            from: { id: 'userid', name: 'User' },
            name: 'welcome',
            type: 'event',
            value: 'help'
        })
        .subscribe(function (id) {
            console.log('"welcome" sent');
        });
}
     var getFirstBrowserLanguage = function () {
var nav = window.navigator,
browserLanguagePropertyKeys = ['language', 'browserLanguage', 
'systemLanguage', 'userLanguage'],
i,
language;

    // support for HTML 5.1 "navigator.languages"
 if (Array.isArray(nav.languages)) {
  for (i = 0; i < nav.languages.length; i++) {
    language = nav.languages[i];
    if (language && language.length) {
      return language;
    }
  }
    }

// support for other well known properties in browsers
for (i = 0; i < browserLanguagePropertyKeys.length; i++) {
  language = nav[browserLanguagePropertyKeys[i]];
  if (language && language.length) {
    return language;
    }
   }

    return null;
   };

  console.log(getFirstBrowserLanguage());

let localed = "";
if ((navigator.languages != undefined) || (navigator.languages == "fr") || 
 getFirstBrowserLanguage() =="fr")
String.localed = 'fr-fr';
  else
String.localed = 'en-US';

    let localrec = "";
  if ((navigator.languages != undefined) || (navigator.languages == "fr") || 
  getFirstBrowserLanguage() =="fr")
String.localrec = 'fr-fr';
else
String.localrec = null;


function getVoice(localevoice) {
if (locale.startsWith('fr'))
    return 'Microsoft Server Speech Text to Speech Voice (fr-FR, 
      HortenseRUS)';
else
    return 'Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)';
  }

let localevoice = "";
if ((navigator.languages != undefined) || (navigator.languages == "fr") || 
 (getFirstBrowserLanguage() =="fr")  || (navigator.languages[0] == "fr") || 
    (navigator.language == "fr") || (navigator.userLanguage == "fr" ))// IE 
            <= 10

    String.localevoice = 'Microsoft Server Speech Text to Speech Voice (fr- 
           FR, HortenseRUS)';
      else
    String.localevoice = 'Microsoft Server Speech Text to Speech Voice (en- 
                   US, 
         ZiraRUS)';



   </script>
      </body>
     </html>

现在的问题是,机器人可以理解bonjour是针对Frenchdialog的,而hello或英语中的其他单词称为englishdialog。但是当我开始讲话时,法语对话中的bot会读英语中的单词。 我在代码中找不到问题

1 个答案:

答案 0 :(得分:2)

您可以在消息控制器中检查活动的.Locale属性,并基于该属性加载其他对话框:

if (activity.Locale.StartsWith("fr"))
    await Conversation.SendAsync(activity, () => new FrenchLuisDialog());
else
    await Conversation.SendAsync(activity, () => new EnglishLuisDialog());

在托管网络聊天控件的页面中,从导航器中检索语言环境,并使用它来设置语音识别器,选择语音并为BotChat.App设置语言环境:

<script>

function getVoice(locale) {
    if (locale.startsWith('fr'))
        return 'Microsoft Server Speech Text to Speech Voice (fr-FR, HortenseRUS)';
    else
        return 'Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)';
}

let locale = "";
if (navigator.languages != undefined)
    locale = navigator.languages[0];
else
    locale = navigator.language;


var speechOptions = {
    speechRecognizer: new CognitiveServices.SpeechRecognizer(
        {locale: locale, subscriptionKey:'xxxxxxx' }),

    speechSynthesizer: new CognitiveServices.SpeechSynthesizer(
        {
            subscriptionKey: 'xxxxxxxxxxx',
            gender: CognitiveServices.SynthesisGender.Female,
            voiceName: getVoice(locale)
        })
}

var botConnection = new BotChat.DirectLine({
    secret: "yBxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    webSocket: true ,

});


  BotChat.App({
        speechOptions: speechOptions,
        botConnection: botConnection,
        user: {
            id: 'userid',
            name: 'User',
        },
        bot: { id: 'botid' },
        resize: 'detect',
      locale: locale

    },
  document.getElementById("bot"));

...