Bot Framework通过UserID将用户数据保存在数据库中

时间:2019-03-22 10:10:40

标签: botframework

我要保存用户输入到数据库中的数据。

我需要一个ID,该ID对于与同一用户的所有对话(而不是单个对话)是唯一的。

根据documentation,我需要使用渠道帐户数据中的From.ID字段。

但是当我使用 Bot Framework Emulator 并单击使用相同的用户ID重新启动时,用户的ID会更改,剩下的就是机器人ID。

我应该使用哪个字段来识别不同呼叫之间的用户(当然是在同一频道中)?


在此示例中,我看到机器人发送的Recipient.id标识符没有更改。

这是用户的唯一标识符吗?为什么与From.ID不同?

会话1:

机器人发出的消息:

{
  "channelId": "emulator",
  "conversation": {
    "id": "202d2d60-4c7f-11e9-b1fa-8b3537dcca45|livechat"
  },
  "from": {
    "id": "2",
    "name": "Bot",
    "role": "bot"
  },
  "id": "208f2380-4c7f-11e9-98ea-9595460a8f6e",
  "inputHint": "acceptingInput",
  "localTimestamp": "2019-03-22T10:47:30+02:00",
  "locale": "",
  "recipient": {
    "id": "d4d1b5a6-1797-4d2a-b78e-257de71d3a69",
    "role": "user"
  },
  "replyToId": "20559cf0-4c7f-11e9-98ea-9595460a8f6e",
  "serviceUrl": "http://localhost:53634",
  "showInInspector": true,
  "text": "conversationUpdate event detected",
  "timestamp": "2019-03-22T08:47:30.232Z",
  "type": "message"
}

来自用户的消息:

{
  "channelData": {
    "clientActivityID": "15532445742330.iqwrgb646rq",
    "state": "sent"
  },
  "channelId": "emulator",
  "conversation": {
    "id": "202d2d60-4c7f-11e9-b1fa-8b3537dcca45|livechat"
  },
  "entities": [
    {
      "requiresBotState": true,
      "supportsListening": true,
      "supportsTts": true,
      "type": "ClientCapabilities"
    }
  ],
  "from": {
    "id": "r_wg30czmqjt",
    "name": "User",
    "role": "user"
  },
  "id": "6a791af0-4c7f-11e9-98ea-9595460a8f6e",
  "localTimestamp": "2019-03-22T10:49:34+02:00",
  "locale": "",
  "recipient": {
    "id": "2",
    "name": "Bot",
    "role": "bot"
  },
  "serviceUrl": "http://localhost:53634",
  "showInInspector": true,
  "text": "hi",
  "textFormat": "plain",
  "timestamp": "2019-03-22T08:49:34.239Z",
  "type": "message"
}

会话2(在使用相同的用户ID重新启动之后):

机器人发出的消息:

{
  "channelId": "emulator",
  "conversation": {
    "id": "a10fba20-4c83-11e9-b1fa-8b3537dcca45|livechat"
  },
  "from": {
    "id": "2",
    "name": "Bot",
    "role": "bot"
  },
  "id": "a15611f0-4c83-11e9-98ea-9595460a8f6e",
  "inputHint": "acceptingInput",
  "localTimestamp": "2019-03-22T11:19:44+02:00",
  "locale": "",
  "recipient": {
    "id": "d4d1b5a6-1797-4d2a-b78e-257de71d3a69",
    "role": "user"
  },
  "replyToId": "a116e610-4c83-11e9-98ea-9595460a8f6e",
  "serviceUrl": "http://localhost:53634",
  "showInInspector": true,
  "text": "conversationUpdate event detected",
  "timestamp": "2019-03-22T09:19:44.271Z",
  "type": "message"
}

来自用户的消息:

{
  "channelData": {
    "clientActivityID": "15532464069120.36lccv6nsg3",
    "state": "sent"
  },
  "channelId": "emulator",
  "conversation": {
    "id": "a10fba20-4c83-11e9-b1fa-8b3537dcca45|livechat"
  },
  "entities": [
    {
      "requiresBotState": true,
      "supportsListening": true,
      "supportsTts": true,
      "type": "ClientCapabilities"
    }
  ],
  "from": {
    "id": "r_xl9pb24o5o",
    "name": "User",
    "role": "user"
  },
  "id": "aed62f90-4c83-11e9-98ea-9595460a8f6e",
  "localTimestamp": "2019-03-22T11:20:06+02:00",
  "locale": "",
  "recipient": {
    "id": "2",
    "name": "Bot",
    "role": "bot"
  },
  "serviceUrl": "http://localhost:53634",
  "showInInspector": true,
  "text": "hi",
  "textFormat": "plain",
  "timestamp": "2019-03-22T09:20:06.921Z",
  "type": "message"
}

2 个答案:

答案 0 :(得分:1)

由于漫游器不会跟踪或识别用户,因此这部分取决于您。

您将需要:

  1. 创建一个捕获用户身份验证的“反向通道”。使漫游器可以访问正在交互或对话的用户的用户名/用户名。
  2. 当用户登录并验证应用程序时,您识别出用户-对其进行检索。
  3. 当用户与漫游器进行交互时,您会获得会话ID-您已经拥有该ID。
  4. 将它们绑定到JSON对象中,然后存储回云存储,应用存储或SQL数据库。

答案 1 :(得分:1)

这是模拟器中的错误,已通过https://github.com/Microsoft/BotFramework-Emulator/pull/1348

修复

请确保您使用的版本> = 4.3.3:

enter image description here

此外,如果消息来自仿真器,则Recipient.Id是机器人ID。