Botbuilder,多个网站与directline网络控制.如何管理



我们希望有多个网站可以放置聊天机器人,但我们当然希望使用1个机器人中间件,但在内部我们希望跟踪来源,即呼叫来自哪个网站。我认为配置js站点的僵尸连接是很容易的:

var bot = {
id:  'mysite1',
name: 'mysite1'
};
BotChat.App({
botConnection: botConnection,
user: user,
bot: bot
}, document.getElementById("BotChatGoesHere"));

但我在中间件流中的任何地方都找不到"mysite1",似乎机器人服务无论如何都会将其翻译成一些指南,所以我明确地将这个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来实现。

BotFramework WebChat实现已经重新设计,并且正在从BotChat.app((中移除。虽然它仍然可以使用,但最佳实践是使用WebChat.renderWebChat方法向机器人发送/从机器人接收。

要将网站位置发送到机器人程序,请将以下内容作为脚本添加到index.html页面。请注意,您不应将Direct Line机密存储在浏览器或客户端应用程序中。此处包含它只是为了简单起见。

简而言之,您首先通过将Direct Line机密传递给生成令牌API来创建令牌。一旦接收到令牌,它就用于建立Direct Line连接。然后,"window.WebChat.renderWebChat"方法在页面上呈现聊天。位置值是在页面首次加载时获得的,然后在"channelData.location"下的活动对象中传递到商店的有效负载中。在任何"POST_activity"时,活动对象都会传递到机器人。您还可以选择其他一些选项,如"DIRECT_LINE/CONNECT",它将在DIRECT LINE首次建立连接时发布位置。

<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>

在机器人的活动对象中,我们看到位置值在channelData中传递,正如我们指定的那样。

channelData: { clientActivityID: '15469789226730.86rx9n6ssex', location: 'http://localhost:3000/' }

希望得到帮助!

最新更新