Yoctol / bottender

⚡️ A framework for building conversational user interfaces.
https://bottender.js.org
MIT License
4.21k stars 333 forks source link

[Proposal] Add an event.timestamp property #813

Open chentsulin opened 4 years ago

chentsulin commented 4 years ago

Is your feature request related to a problem? Please describe.

It's so frustrated when we just want to get a timestamp of a specific event. The ways those platforms provide time information of events are very different, for example:

Messenger

In timestamp:

{
  sender: { id: '1423587017700273' },
  recipient: { id: '404217156637689' },
  timestamp: 1491796363181,
  message: {
    mid: 'mid.$cAAE1UUyiiwthh0NPrVbVf4HFNDGl',
    text: 'Sharp tools make good work.',
  },
};

Facebook

In value.createdTime:

{
  field: 'feed',
  value: {
    from: {
      id: 'xx',
      name: 'user',
    },
    item: 'comment',
    commentId: 'xx',
    postId: 'xx',
    verb: 'add',
    parentId: 'xx',
    createdTime: 1511951015,
    message: 'Good',
  },
};

And sometimes it doesn't have any time information on the event:

{
  value: {
    item: 'like',
    verb: 'add',
  },
  field: 'feed',
};

so we need to use the time information on the entry:

{
  body: {
    object: 'page',
    entry: [
      {
        id: '<PAGE_ID>',
        time: 1458692752478,
        changes: [
        ]
      }
    ]
  }
}

LINE

In timestamp:

{
  "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
  "type": "message",
  "mode": "active",
  "timestamp": 1462629479859,
  "source": {
    "type": "user",
    "userId": "U4af4980629..."
  },
  "message": {
    "id": "325708",
    "type": "text",
    "text": "Hello, world! (love)",
    "emojis": [
      {
        "index": 14,
        "length": 6,
        "productId": "5ac1bfd5040ab15980c9b435",
        "emojiId": "001"
      }
    ]
  }
}

Telegram

In message.date:

{
  message: {
    messageId: 666,
    from: {
      id: 427770117,
      isBot: false,
      firstName: 'first',
      lastName: 'last',
      languageCode: 'en',
    },
    chat: {
      id: 427770117,
      firstName: 'first',
      lastName: 'last',
      type: 'private',
    },
    date: 1499402829,
    text: 'text',
  },
}

Note: what if the event is not a message?

Viber

In timestamp:

{
  event: 'message',
  timestamp: 1457764197627,
  messageToken: 4912661846655238145,
  sender: {
    id: '01234567890A=',
    name: 'John McClane',
    avatar: 'http://avatar.example.com',
    country: 'UK',
    language: 'en',
    apiVersion: 1,
  },
  message: {
    type: 'text',
    text: 'a message to the service',
    trackingData: 'tracking data',
  },
}

Slack

In ts, eventTs, messageTs.. a lot of very different keys

Describe the solution you'd like

So we should provide this timestamp thing in the same way as possible and fallback it to a Date.now value if it can't find one.