Html 在Microsoft Bot框架中,如何从聊天室的url提取参数?

Html 在Microsoft Bot框架中,如何从聊天室的url提取参数?,html,node.js,botframework,Html,Node.js,Botframework,我编写了一个html页面,用户在其中登录(数据文件是json格式的),我可以保存他/她的名字和性别。我将它们作为参数传递到机器人所在的请求url中 var xhr = new XMLHttpRequest(); function processRequest(e) { var url; if (xhr.readyState == 4 && xhr.status == 200) { var response = JSON.p

我编写了一个html页面,用户在其中登录(数据文件是json格式的),我可以保存他/她的名字和性别。我将它们作为参数传递到机器人所在的请求url中

   var xhr = new XMLHttpRequest();

    function processRequest(e) {
      var url;
      if (xhr.readyState == 4  && xhr.status == 200) {
        var response = JSON.parse(xhr.responseText);

        url = "https://webchat.botframework.com/embed/nodejsbot98?firstname="+firstname+"&gender="+gender+"&t="+response;
        document.getElementById("chat").src= url;
      }
    }
现在在app.js中,我想让机器人用名字和性别来问候那个用户,机器人是男性还是女性。url如何调用app.js来发送和接收消息?如何从url访问这些参数

编辑:使用反向通道,脚本正确吗

    <!DOCTYPE html>
<html>
  <head>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>

  </head>
  <body>
    <div id="bot"/>
        <script>
      var botConnection = new BotChat.DirectLine({
        secret: 'TGDGNQY7JK0.cwA.XiQ.I1LDLE5qI3Jsx6q7dlnMMrJtEoLcbTdOE-QIZ4AA_1Y',
      });

var user = {
  id:  'userid',
  name: 'username'
};

var bot = {
  id:  'botid',
  name: 'botname'
};

BotChat.App({
  botConnection: botConnection,
  user: user,
  bot: bot,
}, document.getElementById("bot"));

botConnection
                    .postActivity({type: "event", value: "", from: {id: "me" }, name: "greeting", data:{firstname:'Alain', gender:'male'}})
                    .subscribe(id => console.log("success"));    
   </script>
  </body>
</html>

var botConnection=new BotChat.DirectLine({
秘密:“TGDGNQY7JK0.cwA.XiQ.I1LDLE5qI3Jsx6q7dlnMMrJtEoLcbTdOE-QIZ4AA_1Y”,
});
变量用户={
id:'用户id',
名称:“用户名”
};
var bot={
id:“botid”,
名称:'botname'
};
BotChat.App({
botConnection:botConnection,
用户:用户,,
机器人:机器人,
},document.getElementById(“bot”);
僵尸连接
.postActivity({type:“event”,value:,from:{id:“me”},name:“greeting”,数据:{firstname:'Alain',性别:'male'})
.subscribe(id=>console.log(“成功”));

根据您的需求,最好利用botframework webchat库的功能,因为Bot服务的嵌入式web聊天很难从url传递值

利用Web chat js库,您可以设置如下值:

botConnection .postActivity({ type: "event", from: user, name: "customeEvt", data:{fitstname:'Gary',gender:'Male'} }) .subscribe(activity => console.log(activity));
在bot应用程序中,您可以通过
事件
触发器检索变量:

bot.on('event',(event) => {
  if (event.name === 'customeEvt') {
      console.log(event)// use the data as **event.data** as you set the variable as 'data'
      bot.beginDialog(event.address, '<any dialog>', event.data);//pass your value to dialog
  }
})
bot.on('event',(event)=>{
如果(event.name=='customeEvt'){
console.log(event)//将变量设置为“data”时,将数据用作**event.data**
bot.beginDialog(event.address',event.data);//将值传递给对话框
}
})

谢谢!请检查我的编辑以确保我在htm文件中正确使用了反向通道,好吗?我是否需要在文件或bot应用程序中添加其他内容?另外,我是否使用event.data[0]和[1]访问用户的数据(用户名+性别)?是的,您需要在bot应用程序中将您的事件注册为我的第二个代码段。而
postActivity
对象中的
数据
参数由您自己定义,您可以设置任何其他参数名称,例如,如果您设置
,则在bot应用程序中使用
事件.value
。当您将一个对象设置为
数据
时,您将在bot中获得一个对象,而不是数组。所以
event.data
shuldbe
Object{fitstname:'Gary',gender:'Male'}
这里不是数组。当然,您也可以通过反向通道将阵列发送给bot。感谢您的解释。我是否需要以某种方式启动customeEvt或postbuttonmessage,还是在连接机器人后自动启动?通过上面的代码,我得到了一个控制台错误,即BotChat未定义,如果我试图从按钮调用postButtonMessage,则在声明postButtonMessage之前有一个用法。语法“const postButtonMessage=()=>{”很奇怪是的,一旦机器人webchat连接,它就会自动运行。对于您的问题,您可能错过了html页面中包含的js库。请仔细查看我使用的