如何更新从WebChat组件触发的会话更新事件的channelData



我正在尝试使用Angular客户端连接到bot服务。我可以连接到Bot服务,但一旦我发布消息"活动",WebChat客户端就会向Bot发送一个conversationUpdate事件,因此我会从Bot收到一张登录卡。(如果我从客户端向Bot服务发送第一条消息的channelData中的userToken,我将无法获得登录卡(。

我正试图在channelData内发送一个带有userToken的活动,但conversationUpdate事件在我的消息活动之前到达Bot Service,我收到了一张登录卡。

我需要发送自定义channelData和从客户端发送的conversationUpdate事件。

使用反向通道机制,我可以发送自定义的channelData来发布活动,但这个conversationUpdate事件是从websocket内部触发的,我需要拦截这个事件触发器。

以下是代码详细信息:

Index.html

<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<script src="https://unpkg.com/simple-update-in/dist/simple-update-in.production.min.js"></script>

应用程序组件.ts


// after response from https://directline.botframework.com/v3/directline/conversations
createDirectLine(response: any) {
this.directLine =  window.WebChat.createDirectLine({
token: response.token,
webSocket: true,
streamUrl: response.streamUrl,
conversationId: response.conversationId
});
}
this.store = window.WebChat.createStore({},
({dispatch}) => next => action => {
if (action.type === 'DIRECT_LINE/POST_ACTIVITY') {
// add custom channelData to every postActivity
action = window.simpleUpdateIn(action, ['payload', 'activity', 'channelData', 'UserToken'], function () { return this.userToken; });
}
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
// Send event to bot with custom data
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
activity: {
type : 'conversationUpdate',
channelData : { 'UserToken : this.userToken}
}
}
})
}
return next(action);
});
renderWebChat() {
window.WebChat.renderWebChat(
{
directLine: this.directLine,
store: this.store
},
this.botWindowElement.nativeElement
);
}

p.S.这不是完整的代码,我只添加了一些片段

您不应该试图使用Web Chat直接操纵Direct Line引发的对话更新活动。在创建对话时,您可以通过提供用户ID来影响这些活动,但其余的都不在您的掌控范围内。尝试将令牌嵌入用户ID中可能不是一个好主意,但您可以将令牌托管在机器人可以使用用户ID查找的某个地方。

我认为你真正想做的是向机器人发送一个表示连接成功的事件活动,然后使用它来代替对话更新。您似乎已经走上了正确的轨道,因为您正在尝试响应中间件中的DIRECT_LINE/CONNECT_FULFILLED操作。请尝试以下示例:https://github.com/microsoft/BotFramework-WebChat/tree/master/samples/04.api/a.welcome-event

最新更新