slack 从 conversation.history api 获取消息 return 的消息发件人姓名

slack get message sender name for messages return from conversation.history api

我正在调用 slack API(conversations.history) 来获取频道消息。我想在消息响应中检索发件人姓名。我怎样才能得到它?

以下是我的留言回复:

{  "client_msg_id": "0e19ca7d-1072-4f73-a932-9ec7fa9956de",
          "type": "message",
          "text": "Yeah good edge case, I would agree, just mentioning \" no data found\"",
          "user": "U01JW9D10PQ",
          "ts": "1642216920.001100",
          "team": "T01K38V9Q7M", blocks:[]}

您可以使用从 user 键返回的用户 ID 在收到该负载后调用 users.info

conversations.historyAPI没有return用户信息(只是用户id),你可能需要fetch each user from the channel history, 见示例代码:

Get Slack channel history users                                                                                 Run in Fusebit
const channelId = 'C02TSNCQ2Q2';
const channelHistory = await client.conversations.history({
  channel: channelId,
});

// Get the user ids from the history and populate an array with the user details
const userList = [];
for await (let message of channelHistory.messages) {
  const userAdded = userList.find(user => user.id === message.user);
  if (!userAdded) {
    const response = await slackClient.users.info({
      user: message.user,
    });

    if (response.ok) {
      const { id, name, real_name, profile, is_bot } = response.user;
      userList.push({ id, name, real_name, profile, is_bot });
    }
  }
}

console.log(`There are ${userList.length} users in the channel conversation:\n${userList.map(user => `${(user.is_bot ? '' : '')} ${user.name}`).join('\n')}`);