这将是特定于频道的,因为它依赖于提供在用户离开对话时发送更新的功能的频道。任何其他渠道,您都需要研究。
对于 Facebook,我无法找到涵盖此类操作的范围。这些是可用的范围,您可以更仔细地参考here:
- 消息
- message_deliveries
- message_echoes
- message_reads
- messaging_account_linking
- messaging_checkout_updates(测试版)
- messaging_game_plays
- messaging_handovers
- messaging_optins
- messaging_payments(测试版)
- messaging_policy_enforcement
- messaging_postbacks
- messaging_pre_checkouts(测试版)
- messaging_referrals
- 待机
网络聊天作为一项功能,也不包含此功能。但是,鉴于这是一个网页,您可以利用 onbeforeunload() 窗口函数来调度事件。事件侦听器将利用网络聊天的存储向机器人发送消息或事件。为了清楚起见,我通过SEND_MESSAGE 和SEND_EVENT 发送不同类型的数据。
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
return next( action );
};
window.addEventListener( 'sendEventActivity', ( { data } ) => {
store.dispatch({
type: 'WEB_CHAT/SEND_MESSAGE',
payload: {
text: 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.'
}
} )
} );
window.onbeforeunload = function() {
const eventSendActivity = new Event( 'sendEventActivity' );
eventSendActivity.data = 'User left conversation';
window.dispatchEvent( eventSendActivity );
}
{ type: 'message',
id: '4uPdpZhlTFfBMziBE7EmEI-f|0000004',
timestamp: 2020-01-10T18:21:26.767Z,
serviceUrl: 'https://directline.botframework.com/',
channelId: 'directline',
from: { id: 'dl_123', name: 'johndoe', role: 'user' },
conversation: { id: '4uPdpZhlTFfBMziBE7EmEI-f' },
recipient: { id: 'botberg@QaeuoeEamLg', name: 'Dungeon Runner' },
textFormat: 'plain',
locale: 'en-US',
text: 'User left conversation',
channelData:
{ clientActivityID: '15786804807910gegwkp2kai',
clientTimestamp: '2020-01-10T18:21:20.792Z' }
}
{ type: 'event',
id: '4uPdpZhlTFfBMziBE7EmEI-f|0000005',
timestamp: 2020-01-10T18:21:26.780Z,
serviceUrl: 'https://directline.botframework.com/',
channelId: 'directline',
from: { id: 'dl_123', name: 'johndoe', role: 'user' },
conversation: { id: '4uPdpZhlTFfBMziBE7EmEI-f' },
recipient: { id: 'botberg@QaeuoeEamLg', name: 'Dungeon Runner' },
locale: 'en-US',
channelData:
{ clientActivityID: '1578680480821h7kgfm9cyz',
clientTimestamp: '2020-01-10T18:21:20.821Z' },
value:
{ name: 'end_conversation', value: 'user ended conversation' },
name: 'user_event'
}
希望有帮助!
更新(2021 年 8 月 6 日):
作为 Chrome 和其他浏览器,在 onbeforeunload() 期间不允许阻塞/延迟关闭窗口,使用事件处理程序或侦听器发送事件通常是不可靠的,充其量。在最坏的情况下,它根本不起作用。
但是,通过使用“window.navigator.sendBeacon()”(如果它受支持),确实可以使用另一种方法。
sendBeacon 是 fetch 的低级简化版本,它“通过 HTTP 将少量数据异步发送到 Web 服务器”。它仅作为 POST 发送,仅将 URL 或 URL + 数据作为属性,并且不等待响应。正如文档所述:
在测试中,我已将它与我的机器人中的 proactive messaging 端点结合起来,它似乎工作得很好。 (下面我的代码示例是用 JS 编写的,从一个项目中提取 - 作为参考,这里是主动消息传递 C# sample,也有其他语言版本。
当我导航到另一个页面或关闭浏览器时,sendBeacon 将消息发布到创建主动消息的端点,而后者又将活动发送到机器人。当我返回 Web 聊天页面时,该消息现在在聊天窗口中可见。请记住,我还在网络聊天中设置了persistence,允许我返回之前开始的对话。
在下图中,我演示了加载网络聊天,导航到浏览器的主页,然后返回 - 主动消息现在在聊天中可见。
(网络聊天)托管页面:
window.onbeforeunload = () => {
let body = { user: { userName: 'McUser', userId: 'abc123' } };
const headers = { type: 'application/json', 'Access-Control-Allow-Origin': '*' };
const blob = new Blob( [ JSON.stringify( body ) ], headers )
navigator.sendBeacon( 'http://localhost:3978/api/notify', blob )
}
Bot 的主动消息传递端点:
server.post('/api/notify', async (req, res) => {
const userName = req.body.user.userName;
const userId = req.body.user.userId;
for (const conversationReference of Object.values(conversationReferences)) {
await adapter.continueConversation(conversationReference, async (turnContext) => {
await turnContext.sendActivity(`${ userName } (userId: ${ userId }) exited chat.`);
});
}
});