【问题标题】:Azure bot framework - Connection open and closeAzure 机器人框架 - 连接打开和关闭
【发布时间】:2018-07-21 02:08:33
【问题描述】:

我是 Azure 机器人服务的新手。我创建了一个机器人并在本地托管。我想知道用户和机器人之间的连接何时关闭。 在连接关闭之前,我实际上也需要它来将聊天记录存储到数据库中。 我正在使用 Directline API。 bot连接会保持很长时间吗? 连接关闭之前是否有任何事件触发? 使用和机器人之间的实际连接是如何创建和关闭的?

【问题讨论】:

  • I actually need this to store chat history to DB that too before connection getting closed. 您能否详细说明您的要求?您想获取和跟踪指示用户加入/离开特定对话的时间戳/事件吗?
  • 是的。我想知道用户何时关闭/退出机器人。

标签: azure botframework azure-bot-service


【解决方案1】:

如果您在自己的网站中使用直通渠道,则以下方法应该有效。本质上,当用户关闭与您的机器人聊天时,您只需向您的机器人发送backchannel message。

确保按如下方式定义您的直达线路,以便连接可用于反向通道消息

botConnection = new BotChat.DirectLine({
    secret: "<secret>"
  });

你会想在你的 html body 或 div 上注册一个“onunload”方法,如下所示:

 <body onunload="closeBotChat();">

然后在此方法中,您只需从您的网站向您的机器人发送一个事件,表明对话结束

 function closeBotChat(){
  botConnection
  .postActivity({
    from: { id: '<user>' },
    value: "chat <conversationId> closed", //send whatever information you need about the conversation here
    type: 'event',
    name: "ConversationUpdate"
  })
  .subscribe(id=> console.log("closed" + id)) // this will likely not be shown unless the botchat is in a modal within the page somewhere
}

以下代码在您的机器人中使用时将能够听到并处理此事件:

bot.on("event", function(e){
    console.log(util.inspect(e)); // do whatever with the conversation end event here
});

请注意,您不必为机器人网络聊天的“打开连接”事件添加任何内容,因为 ConversationUpdate 事件会在新对话开始时自动发送。

【讨论】:

  • 非常感谢。它的工作
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-17
  • 2022-11-14
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
相关资源
最近更新 更多