Skip to content

Commit

Permalink
Fix: handle failures in fetching sender name
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-osch committed Jan 17, 2025
1 parent dae7404 commit 7351059
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/slackbot/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,20 @@ export const convertSlackTimestamp = (slackTs: string): Date => {
};

export const fetchMessageSenderName = async (message: MessageElement) => {
if (message.user) {
const user = await web.users
.info({ user: message.user! })
.then((u) => u.user);
return user?.name ?? user?.real_name ?? "Unknown";
} else if (message.bot_id) {
const bot = await web.bots.info({ bot: message.bot_id! });
return bot.bot?.name ?? "Unknown";
try {
if (message.user) {
const user = await web.users
.info({ user: message.user! })
.then((u) => u.user);
return user?.name ?? user?.real_name ?? "Unknown";
} else if (message.bot_id) {
const bot = await web.bots.info({ bot: message.bot_id! });
return bot.bot?.name ?? "Unknown";
}
return "Unknown";
} catch (e) {
// Fetching message sender can fail if the permissions scope is missing
// Fallback to unique identifier in that case
return `Unknown/${message.user ?? message.bot_id}`;
}

return "Unknown";
};

0 comments on commit 7351059

Please sign in to comment.