我写了一个html页面,用户登录(数据文件在json中),我可以保存他/她的名字和性别。我将它们作为参数传递给机器人居住的请求网址中。
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中,我希望该机器人用名字和性别来问候该用户,该机器人是男性还是女性。网址如何调用app.js发送和接收消息?如何从网址访问这些参数?
编辑:使用反向通道,该脚本正确吗?
<!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>
答案 0 :(得分:1)
根据您的要求,最好利用botframework-webchat库的The Backchannel功能,因为Bot服务的嵌入网络聊天很难通过url传递值。
利用网络聊天js库,您可以将值设置为:
botConnection .postActivity({ type: "event", from: user, name: "customeEvt", data:{fitstname:'Gary',gender:'Male'} }) .subscribe(activity => console.log(activity));
在机器人应用程序中,您可以通过event
触发器检索变量:
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
}
})