Skip to content

An open-source, faster Devin alternative

TypeScript

api-edge: send Slack Connect invites as a person (user token), not a bot

utpal nadiger via Computer1e5c9f2666e6
diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.ts
index 267caf64..20ab4d5c 100644
--- a/cloudflare-workers/api-edge/src/index.ts
+++ b/cloudflare-workers/api-edge/src/index.ts
@@ -185,9 +185,10 @@ export interface Env extends DashboardEnv, AgentFeedbackEnv, BillingEventsEnv {
// as the API key so /v3 sandboxes are owned by + billed to the customer org.
// Unset → the feature is inert (JWT keys rejected). See dashboard.ts.
OC_PROVISION_SECRET?: string;
- // Slack Connect per signup (slack_connect.ts): dormant unless the bot token
+ // Slack Connect per signup (slack_connect.ts), sent as a person via a user
+ // token: dormant unless the token
// and SINCE (unix seconds) are set.
- SLACK_CONNECT_BOT_TOKEN?: string;
+ SLACK_CONNECT_TOKEN?: string;
SLACK_CONNECT_SINCE?: string;
SLACK_CONNECT_TEAM_USER_IDS?: string;
SLACK_CONNECT_BATCH?: string;
@@ -6378,7 +6379,7 @@ export default {
runPoolKeepWarm(env).catch((err) => console.error("pool-keepwarm: run failed", err)),
);
// Slack Connect invite per signup, regardless of billing config. Dormant
- // unless SLACK_CONNECT_BOT_TOKEN is set. See slack_connect.ts.
+ // unless SLACK_CONNECT_TOKEN is set. See slack_connect.ts.
ctx.waitUntil(
runSlackConnectInvites(env, Date.now()).catch((err) => console.error("slack-connect: run failed", err)),
);
diff --git a/cloudflare-workers/api-edge/src/slack_connect.test.ts b/cloudflare-workers/api-edge/src/slack_connect.test.ts
index 250934e6..2af84814 100644
--- a/cloudflare-workers/api-edge/src/slack_connect.test.ts
+++ b/cloudflare-workers/api-edge/src/slack_connect.test.ts
@@ -83,6 +83,7 @@ function fakeSlack(errors: Record<string, string | string[]> = {}, taken: string
taken.push(params.name);
return Response.json({ ok: true, channel: { id: `C${++n}`, name: params.name } });
}
+ if (method === "auth.test") return Response.json({ ok: true, user_id: "U1" });
if (method === "conversations.inviteShared") return Response.json({ ok: true, invite_id: `I${n}` });
return Response.json({ ok: true });
}) as unknown as typeof fetch;
@@ -96,7 +97,7 @@ function setup(users: [string, string, number?][]) {
}
const env: SlackConnectEnv = {
OPENCOMPUTER_DB: d1(db),
- SLACK_CONNECT_BOT_TOKEN: "xoxb-test",
+ SLACK_CONNECT_TOKEN: "xoxp-test",
SLACK_CONNECT_SINCE: String(SINCE),
SLACK_CONNECT_TEAM_USER_IDS: "U1,U2,U3",
};
@@ -126,7 +127,7 @@ describe("runSlackConnectInvites", () => {
it("does nothing without a token or a start time", async () => {
const { env, rows } = setup([["u1", "[email protected]"]]);
const slack = fakeSlack();
- await runSlackConnectInvites({ ...env, SLACK_CONNECT_BOT_TOKEN: undefined }, NOW, slack.fetchImpl);
+ await runSlackConnectInvites({ ...env, SLACK_CONNECT_TOKEN: undefined }, NOW, slack.fetchImpl);
await runSlackConnectInvites({ ...env, SLACK_CONNECT_SINCE: undefined }, NOW, slack.fetchImpl);
expect(slack.calls).toEqual([]);
expect(rows()).toEqual([]);
@@ -138,13 +139,15 @@ describe("runSlackConnectInvites", () => {
expect(await runSlackConnectInvites(env, NOW, slack.fetchImpl)).toMatchObject({ invited: 1 });
expect(slack.calls).toEqual([
{ method: "conversations.create", params: { name: "opencomputer-acme", is_private: "false" } },
- { method: "conversations.invite", params: { channel: "C1", users: "U1,U2,U3", force: "true" } },
+ { method: "auth.test", params: {} },
+ // U1 is the token's own user, already in the channel it created.
+ { method: "conversations.invite", params: { channel: "C1", users: "U2,U3", force: "true" } },
{ method: "conversations.inviteShared", params: { channel: "C1", emails: "[email protected]" } },
]);
expect(rows()).toMatchObject([{ user_id: "u1", channel_id: "C1", status: "invited" }]);
await runSlackConnectInvites(env, NOW + 300_000, slack.fetchImpl);
- expect(slack.calls).toHaveLength(3);
+ expect(slack.calls).toHaveLength(4);
});
it("puts colleagues in one channel and free-email users in their own", async () => {
@@ -186,7 +189,7 @@ describe("runSlackConnectInvites", () => {
expect(rows()).toMatchObject([{ status: "pending", attempts: 1, channel_id: "C1", last_error: "internal_error" }]);
await runSlackConnectInvites(env, NOW, slack.fetchImpl);
expect(slack.methods()).toEqual([
- "conversations.create", "conversations.invite", "conversations.inviteShared", "conversations.inviteShared",
+ "conversations.create", "auth.test", "conversations.invite", "conversations.inviteShared", "conversations.inviteShared",
]);
expect(rows()).toMatchObject([{ status: "invited", last_error: null }]);
});
diff --git a/cloudflare-workers/api-edge/src/slack_connect.ts b/cloudflare-workers/api-edge/src/slack_connect.ts
index 5a893064..9ff04e4f 100644
--- a/cloudflare-workers/api-edge/src/slack_connect.ts
+++ b/cloudflare-workers/api-edge/src/slack_connect.ts
@@ -6,12 +6,16 @@
// Runs on the */5 cron, never in the login path. A row in slack_connect_invites
// is the claim that a user is handled, so a re-run does nothing; a Slack error
// leaves the row pending and the next tick retries, up to MAX_ATTEMPTS.
-// Dormant unless SLACK_CONNECT_BOT_TOKEN and SLACK_CONNECT_SINCE are set; SINCE
+// SLACK_CONNECT_TOKEN is a user token (xoxp-, scopes channels:write,
+// channels:read, conversations.connect:write), so channels and invites come from
+// that person rather than a bot. The token's own user is the channel's creator
+// and is left out of the team invite.
+// Dormant unless SLACK_CONNECT_TOKEN and SLACK_CONNECT_SINCE are set; SINCE
// keeps the first deploy from inviting every user who signed up before it.
export interface SlackConnectEnv {
OPENCOMPUTER_DB: D1Database;
- SLACK_CONNECT_BOT_TOKEN?: string;
+ SLACK_CONNECT_TOKEN?: string;
SLACK_CONNECT_SINCE?: string; // unix seconds; only users created at or after it
SLACK_CONNECT_TEAM_USER_IDS?: string; // comma-separated Slack user ids added to every channel
SLACK_CONNECT_BATCH?: string; // users per tick, default 10
@@ -91,7 +95,7 @@ async function slackCall(
const resp = await fetchImpl(`https://slack.com/api/${method}`, {
method: "POST",
headers: {
- authorization: `Bearer ${env.SLACK_CONNECT_BOT_TOKEN}`,
+ authorization: `Bearer ${env.SLACK_CONNECT_TOKEN}`,
"content-type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(params).toString(),
@@ -117,7 +121,7 @@ export async function runSlackConnectInvites(
): Promise<{ invited: number; skipped: number; failed: number; pending: number }> {
const counts = { invited: 0, skipped: 0, failed: 0, pending: 0 };
const since = Number(env.SLACK_CONNECT_SINCE);
- if (!env.SLACK_CONNECT_BOT_TOKEN || !Number.isFinite(since) || since <= 0) return counts;
+ if (!env.SLACK_CONNECT_TOKEN || !Number.isFinite(since) || since <= 0) return counts;
const batch = Number(env.SLACK_CONNECT_BATCH) || DEFAULT_BATCH;
const nowSec = Math.floor(nowMs / 1000);
@@ -215,7 +219,11 @@ async function inviteOne(
.bind(c.user_id, channelID, name)
.run();
- const team = (env.SLACK_CONNECT_TEAM_USER_IDS ?? "").split(",").map((s) => s.trim()).filter(Boolean);
+ const self = await slackCall(env, fetchImpl, "auth.test", {});
+ const team = (env.SLACK_CONNECT_TEAM_USER_IDS ?? "")
+ .split(",")
+ .map((s) => s.trim())
+ .filter((id) => id && id !== self.user_id);
if (team.length > 0) {
// Not fatal: the invite still goes out, and the team can join by hand.
const r = await slackCall(env, fetchImpl, "conversations.invite", {