import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getSlackApi } from "../lib/api" import { SLACK_TEAM_READ_SCOPE } from "../lib/scopes" import { SLACK_WORKSPACE_IDENTITY_SCHEMA } from "@automate.ax/integration-contracts/slack" const AUTH_TEST_RESPONSE_SCHEMA = z.looseObject({ bot_id: z.string().optional(), enterprise_id: z.string().optional(), ok: z.literal(true), team: z.string(), team_id: z.string(), url: z.string().optional(), user: z.string().optional(), user_id: z.string(), }) const PROVIDER_WORKSPACE_ICON_SCHEMA = z.looseObject({ image_34: z.string().optional(), image_44: z.string().optional(), image_68: z.string().optional(), image_88: z.string().optional(), image_102: z.string().optional(), image_132: z.string().optional(), image_default: z.boolean().optional(), }) const PROVIDER_WORKSPACE_SCHEMA = z.looseObject({ domain: z.string(), email_domain: z.string(), enterprise_id: z.string().optional(), enterprise_name: z.string().optional(), icon: PROVIDER_WORKSPACE_ICON_SCHEMA, id: z.string(), name: z.string(), }) const WORKSPACE_INFO_RESPONSE_SCHEMA = z.looseObject({ ok: z.literal(true), team: PROVIDER_WORKSPACE_SCHEMA, }) const WORKSPACE_SCHEMA = z.object({ /** Slack workspace domain. */ domain: z.string(), /** Email domain configured for joining the workspace. */ emailDomain: z.string(), /** Owning Enterprise Grid organization ID, when applicable. */ enterpriseId: z.string().optional(), /** Owning Enterprise Grid organization name, when applicable. */ enterpriseName: z.string().optional(), /** Workspace icon URLs keyed by pixel size. */ icon: z.object({ image102: z.string().optional(), image132: z.string().optional(), image34: z.string().optional(), image44: z.string().optional(), image68: z.string().optional(), image88: z.string().optional(), isDefault: z.boolean().optional(), }), /** Stable Slack workspace ID. */ id: z.string(), /** Human-readable workspace name. */ name: z.string(), }) /** Returns the workspace and bot identity represented by the Slack account. */ export const getSlackWorkspaceIdentity = defineAction( "Get Slack workspace identity", ) .describe("Gets the authenticated Slack bot and workspace identity.") .account("slack") .output(SLACK_WORKSPACE_IDENTITY_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => { const result = await getSlackApi(account.secret).call("auth.test", { httpMethod: "POST", responseSchema: AUTH_TEST_RESPONSE_SCHEMA, }) return { botId: result.bot_id, enterpriseId: result.enterprise_id, url: result.url, user: result.user, userId: result.user_id, workspace: result.team, workspaceId: result.team_id, } }) /** Gets metadata for the current Slack workspace. */ export const getSlackWorkspace = defineAction("Get Slack workspace") .describe("Gets metadata and icon URLs for the current Slack workspace.") .account("slack", SLACK_TEAM_READ_SCOPE) .output(WORKSPACE_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account }) => { const result = await getSlackApi(account.secret).call("team.info", { responseSchema: WORKSPACE_INFO_RESPONSE_SCHEMA, }) return { domain: result.team.domain, emailDomain: result.team.email_domain, enterpriseId: result.team.enterprise_id, enterpriseName: result.team.enterprise_name, icon: { image102: result.team.icon.image_102, image132: result.team.icon.image_132, image34: result.team.icon.image_34, image44: result.team.icon.image_44, image68: result.team.icon.image_68, image88: result.team.icon.image_88, isDefault: result.team.icon.image_default, }, id: result.team.id, name: result.team.name, } })