mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:42:43 +00:00
fix(slack): drop mismatched Socket Mode events (#889)
Filter Slack Socket Mode events by api_app_id/team_id. Refs: #828 Contributor: @roshanasingh4 Co-authored-by: Roshan Singh <roshanasingh4@users.noreply.github.com>
This commit is contained in:
@@ -3,65 +3,37 @@ import type { SlackEventMiddlewareArgs } from "@slack/bolt";
|
||||
import { danger } from "../../../globals.js";
|
||||
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
||||
|
||||
import { normalizeSlackSlug } from "../allow-list.js";
|
||||
import {
|
||||
resolveSlackChannelConfig,
|
||||
shouldEmitSlackReactionNotification,
|
||||
} from "../channel-config.js";
|
||||
import { resolveSlackChannelLabel } from "../channel-config.js";
|
||||
import type { SlackMonitorContext } from "../context.js";
|
||||
import type { SlackReactionEvent } from "../types.js";
|
||||
import type { SlackMessageEvent, SlackReactionEvent } from "../types.js";
|
||||
|
||||
export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext }) {
|
||||
const { ctx } = params;
|
||||
|
||||
const handleReactionEvent = async (event: SlackReactionEvent, action: "added" | "removed") => {
|
||||
const handleReactionEvent = async (event: SlackReactionEvent, action: string) => {
|
||||
try {
|
||||
const item = event.item;
|
||||
if (!event.user) return;
|
||||
if (!item?.channel || !item?.ts) return;
|
||||
if (item.type && item.type !== "message") return;
|
||||
if (ctx.botUserId && event.user === ctx.botUserId) return;
|
||||
|
||||
const channelInfo = await ctx.resolveChannelName(item.channel);
|
||||
const channelType = channelInfo?.type;
|
||||
const channelName = channelInfo?.name;
|
||||
if (!item || item.type !== "message") return;
|
||||
|
||||
const channelInfo = item.channel ? await ctx.resolveChannelName(item.channel) : {};
|
||||
const channelType = channelInfo?.type as SlackMessageEvent["channel_type"];
|
||||
if (
|
||||
!ctx.isChannelAllowed({
|
||||
channelId: item.channel,
|
||||
channelName,
|
||||
channelName: channelInfo?.name,
|
||||
channelType,
|
||||
})
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isRoom = channelType === "channel" || channelType === "group";
|
||||
if (isRoom) {
|
||||
const channelConfig = resolveSlackChannelConfig({
|
||||
channelId: item.channel,
|
||||
channelName,
|
||||
channels: ctx.channelsConfig,
|
||||
});
|
||||
if (channelConfig?.allowed === false) return;
|
||||
}
|
||||
|
||||
const actor = await ctx.resolveUserName(event.user);
|
||||
const shouldNotify = shouldEmitSlackReactionNotification({
|
||||
mode: ctx.reactionMode,
|
||||
botId: ctx.botUserId,
|
||||
messageAuthorId: event.item_user ?? undefined,
|
||||
userId: event.user,
|
||||
userName: actor?.name ?? undefined,
|
||||
allowlist: ctx.reactionAllowlist,
|
||||
const channelLabel = resolveSlackChannelLabel({
|
||||
channelId: item.channel,
|
||||
channelName: channelInfo?.name,
|
||||
});
|
||||
if (!shouldNotify) return;
|
||||
|
||||
const actorInfo = event.user ? await ctx.resolveUserName(event.user) : undefined;
|
||||
const actorLabel = actorInfo?.name ?? event.user;
|
||||
const emojiLabel = event.reaction ?? "emoji";
|
||||
const actorLabel = actor?.name ?? event.user;
|
||||
const channelLabel = channelName
|
||||
? `#${normalizeSlackSlug(channelName) || channelName}`
|
||||
: `#${item.channel}`;
|
||||
const authorInfo = event.item_user ? await ctx.resolveUserName(event.item_user) : undefined;
|
||||
const authorLabel = authorInfo?.name ?? event.item_user;
|
||||
const baseText = `Slack reaction ${action}: :${emojiLabel}: by ${actorLabel} in ${channelLabel} msg ${item.ts}`;
|
||||
@@ -79,13 +51,18 @@ export function registerSlackReactionEvents(params: { ctx: SlackMonitorContext }
|
||||
}
|
||||
};
|
||||
|
||||
ctx.app.event("reaction_added", async ({ event }: SlackEventMiddlewareArgs<"reaction_added">) => {
|
||||
await handleReactionEvent(event as SlackReactionEvent, "added");
|
||||
});
|
||||
ctx.app.event(
|
||||
"reaction_added",
|
||||
async ({ event, body }: SlackEventMiddlewareArgs<"reaction_added">) => {
|
||||
if (ctx.shouldDropMismatchedSlackEvent(body)) return;
|
||||
await handleReactionEvent(event as SlackReactionEvent, "added");
|
||||
},
|
||||
);
|
||||
|
||||
ctx.app.event(
|
||||
"reaction_removed",
|
||||
async ({ event }: SlackEventMiddlewareArgs<"reaction_removed">) => {
|
||||
async ({ event, body }: SlackEventMiddlewareArgs<"reaction_removed">) => {
|
||||
if (ctx.shouldDropMismatchedSlackEvent(body)) return;
|
||||
await handleReactionEvent(event as SlackReactionEvent, "removed");
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user