Spaces:
Paused
Paused
Download slack-bot.js from jodaro01/claude-code: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/jodaro01/claude-code/resolve/main/slack-bot.js
- Command line
-
hf download hf://spaces/jodaro01/claude-code/slack-bot.js
-
curl -L -o slack-bot.js https://huggingface.co/spaces/jodaro01/claude-code/resolve/main/slack-bot.js
2.62 kB
| const { App } = require("@slack/bolt"); | |
| const { execFile } = require("child_process"); | |
| const required = ["SLACK_BOT_TOKEN", "SLACK_APP_TOKEN"]; | |
| const missing = required.filter((name) => !process.env[name]); | |
| if (missing.length > 0) { | |
| console.error(`Slack bot disabled. Missing: ${missing.join(", ")}`); | |
| process.exit(0); | |
| } | |
| const app = new App({ | |
| token: process.env.SLACK_BOT_TOKEN, | |
| appToken: process.env.SLACK_APP_TOKEN, | |
| socketMode: true, | |
| }); | |
| function buildClaudeEnv() { | |
| const env = { ...process.env }; | |
| const token = env.ANTHROPIC_AUTH_TOKEN || env.ANTHROPIC_API_KEY || env.ZAI_API_KEY || env.GLM_API_KEY; | |
| if (token) { | |
| env.ANTHROPIC_AUTH_TOKEN = token; | |
| env.ANTHROPIC_API_KEY = token; | |
| } | |
| env.ANTHROPIC_BASE_URL = env.ANTHROPIC_BASE_URL || "https://api.z.ai/api/anthropic"; | |
| env.API_TIMEOUT_MS = env.API_TIMEOUT_MS || "3000000"; | |
| env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC || "1"; | |
| env.ANTHROPIC_DEFAULT_HAIKU_MODEL = env.ANTHROPIC_DEFAULT_HAIKU_MODEL || "glm-4.5-air"; | |
| env.ANTHROPIC_DEFAULT_SONNET_MODEL = env.ANTHROPIC_DEFAULT_SONNET_MODEL || "glm-5-turbo"; | |
| env.ANTHROPIC_DEFAULT_OPUS_MODEL = env.ANTHROPIC_DEFAULT_OPUS_MODEL || "glm-5-turbo"; | |
| return env; | |
| } | |
| function stripAnsi(value) { | |
| return value.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ""); | |
| } | |
| app.event("app_mention", async ({ event, client, say }) => { | |
| const prompt = event.text.replace(/<@[A-Z0-9]+>/g, "").trim(); | |
| if (!prompt) { | |
| await say("Bitte schreibe eine Aufgabe nach der Erwaehnung."); | |
| return; | |
| } | |
| const ack = await say(`Claude Code arbeitet an: "${prompt}"`); | |
| execFile( | |
| "claude", | |
| ["-p", "--no-session-persistence", "--permission-mode", "bypassPermissions", prompt], | |
| { | |
| cwd: process.env.WORKSPACE_DIR || "/workspace", | |
| env: buildClaudeEnv(), | |
| timeout: Number(process.env.CLAUDE_SLACK_TIMEOUT_MS || 600000), | |
| maxBuffer: 1024 * 1024 * 10, | |
| }, | |
| async (error, stdout, stderr) => { | |
| let response = stdout || stderr || "Kein Output."; | |
| if (error) { | |
| response = `${error.message}\n\n${response}`; | |
| } | |
| response = stripAnsi(response).trim(); | |
| if (response.length > 3900) { | |
| response = `${response.slice(0, 3900)}\n\n...(Output gekuerzt)`; | |
| } | |
| await client.chat.update({ | |
| channel: event.channel, | |
| ts: ack.ts, | |
| text: `Fertig:\n\`\`\`\n${response}\n\`\`\``, | |
| }); | |
| }, | |
| ); | |
| }); | |
| (async () => { | |
| await app.start(); | |
| console.log("Claude Code Slack bot is running in Socket Mode."); | |
| })(); | |