Search code examples
botframework

Bot Framework save user data in the database by UserID


I want to save data that the user has entered in my database.

I need an ID that will be unique for all conversations with the same user rather than for a single conversation.

According to the documentation, I need to use the From.ID field from the Channel Account data.

But when I use Bot Framework Emulator and click Restart with same User ID, the ID of the user changes and what remains is the bot ID.

What field should I use to identify a user across different calls (in the same channel, of course)?


In this example, I see that the the Recipient.id identifier that the bot sends does not change.

Is this the user's unique identifier? Why is it be different from the From.ID?

Conversation 1:

Message from the bot:

{
  "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"
}

Message from the user:

{
  "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"
}

Conversation 2 (After Restart with same user ID):

Message from the bot:

{
  "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"
}

Message from the user:

{
  "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"
}

Solution

  • This was a bug in the Emulator, and fixed with https://github.com/Microsoft/BotFramework-Emulator/pull/1348

    Please make sure you are on version >= 4.3.3:

    enter image description here

    Also, the Recipient.Id is the bot id if the message is coming from the Emulator.