【发布时间】:2020-02-20 07:06:31
【问题描述】:
我们正在尝试在用户退出聊天机器人窗口(或网站)(或欢迎消息)时发布一些事件/消息,但到目前为止这些事件并未触发。
我可以在 Inspector 工具中看到:
屏幕截图 2020-02-18 下午 3 15 39
创建了各种活动/对话,聊天机器人工作,但没有触发欢迎/退出事件。
我们使用的代码几乎与此处的文档代码相同:https://github.com/microsoft/BotFramework-WebChat/blob/master/docs/WELCOME_MESSAGE.md
这里:How to handle user leaving conversation
我有一个在窗口关闭时触发的函数,如下:
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
return next( action );});
window.addEventListener( 'sendEventActivity', ( { data } ) => {
store.dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'user_event',
value: {
name: 'end_conversation',
value: 'user ended conversation'
},
text: 'The user has left the conversation.'
}
})
});
function exitEvent(){
const eventSendActivity = new Event( 'sendEventActivity' );
eventSendActivity.data = 'User left conversation';
window.dispatchEvent( eventSendActivity );
console.log('Exit Event Submitted (hopefully)');
}
exitEvent();
我尝试过其他变体,在前面定义商店,在渲染聊天上方,在渲染聊天之后,从不同位置和不同时间发送欢迎消息,但似乎无法发送。
我们正在使用https://cdn.botframework.com/botframework-webchat/latest/webchat.js
知道问题可能是什么吗?不确定我们哪里出错了,或者为什么它没有触发 - 将理论上已知有效的代码直接复制到我们的代码中似乎并不能解决问题。
提前致谢,如果我没有包含任何必要的详细信息,请告诉我 - 聊天机器人的新手,不要在 github 上发布太多内容。非常感谢,
编辑:
我能够将上述代码和这里的代码结合起来:https://github.com/microsoft/BotFramework-WebChat/issues/2120#issuecomment-516056614 以实现我想要的。我会在下面发布,以防它帮助其他人......
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join'
}
});
}
return next(action);
});
window.addEventListener( 'sendEventActivity', ( { data } ) => {
store.dispatch( {
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/exit'
}
} );
} );
document.getElementById("action_menu_btn").addEventListener( 'click', function() {
const eventSendActivity = new Event( 'sendEventActivity' );
eventSendActivity.data = 'User left conversation';
window.dispatchEvent( eventSendActivity );
console.log('End Converstaion Event Fired');
});
干杯!
【问题讨论】:
-
接受/投票支持更大的 Stack Overflow 社区和任何有类似问题的人。如果您觉得我的回答足够,请“接受”并点赞。如果没有,请告诉我我还能提供哪些帮助!
标签: botframework