我们希望有多个网站来放置我们的聊天机器人,但我们想要使用一个bot中间件确定,但在内部,我们想要跟踪的起源,从哪个网站的调用是来自。我认为这一点也不像配置js站点的僵尸连接那样简单:
var bot = {
id: 'mysite1',
name: 'mysite1'
};
BotChat.App({
botConnection: botConnection,
user: user,
bot: bot
}, document.getElementById("BotChatGoesHere"));但是我在中间件流中的任何地方都找不到" mysite1“,似乎bot服务无论如何都会将它转换成一些guids,所以我将这个mysite1显式地添加到pageLoad事件中,当用户加载页面时,我也会使用该事件来启动聊天机器人交互:
(function sendEvent() {
botConnection
.postActivity({ type: "event", value: "mysite1", from: { id: localStorage.getItem("guidJD"), name: localStorage.getItem("guidJD") }, name: "userLoadPage" })
.subscribe(id => console.log("success"));
})();它可以工作,但不稳定,有时页面事件会在组件实际加载和对话框启动后以某种方式触发,因此在对话框开始时,我不需要获得原始信息。你有什么办法解决这个问题吗?或者我做错了。还是应该在频道中添加新站点?最好是通过调用api来实现。
发布于 2019-01-08 21:31:39
BotFramework-网络聊天实现已经重新设计,并且正在远离BotChat.app()。虽然它仍然可以使用,但最佳实践是使用WebChat.renderWebChat方法从bot发送/接收。
若要将站点位置发送到机器人,请将以下内容作为脚本添加到index.html页面。请注意,您不应该将您的直接线路机密存储在浏览器或客户端应用程序。这里包括它只是为了简单。
简而言之,您首先要通过将Direct秘密传递给来创建令牌。一旦接收到令牌,它将用于建立直接线路连接。然后,"window.WebChat.renderWebChat“方法将聊天呈现在页面上。当页面第一次加载并在"channelData.location“下传递到活动对象中的存储的有效负载时,将获得位置值。在任何"POST_ACTIVITY“上,活动对象都被传递给机器人。您还可以选择其他一些选项,例如“Direct /CONNECT”,它将在Direct第一次连接时发布位置。
<script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>
<script src='https://cdn.botframework.com/botframework-webchat/master/webchat.js'></script>
<script src='https://unpkg.com/simple-update-in/dist/simple-update-in.production.min.js'></script>
<script>
(async function () {
// To talk to your bot, you should use the token exchanged using your Direct Line secret.
// You should never put the Direct Line secret in the browser or client app.
const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + secret
},
json: true
});
const { token } = await res.json();
cosnt location = window.location.href;
let store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
// simple-update-in is used to update the "action"
action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'location'], () => location);
}
return next(action);
}
);
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
store,
styleOptions: {
botAvatarInitials: 'BF',
userAvatarInitials: 'WC'
}
}, document.getElementById('webchat'));
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));;
</script>在bot的activity中,我们看到位置值在channelData中传递,就像我们指定的那样。
channelData: { clientActivityID: '15469789226730.86rx9n6ssex', location: 'http://localhost:3000/' }希望得到帮助!
https://stackoverflow.com/questions/53695917
复制相似问题