Commit 0b0147cd by Lin Wang

feat: send message to slack channel

parent 2c07fbd0
No preview for this file type
......@@ -9,6 +9,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@slack/web-api": "^7.9.3",
"deep-diff": "^1.0.2",
"jsdom": "^26.1.0"
},
......@@ -24,6 +25,9 @@
"diff": "bun run src/detect_section_selector_masters/diffWithDeepDiff.ts",
"diff:ai": "USE_AI_SELECTORS=true bun run diff",
"task": "bun run extract && bun run diff",
"task:ai": "bun run extract:ai && bun run diff:ai"
"task:ai": "bun run extract:ai && bun run diff:ai",
"send-diff": "bun run src/detect_section_selector_masters/sendDiffToSlack.ts",
"task:slack": "bun run task && bun run send-diff",
"task:slack:ai": "bun run task:ai && bun run send-diff"
}
}
\ No newline at end of file
import { WebClient } from '@slack/web-api';
import { createReadStream } from 'fs';
import { basename } from 'path';
export async function sendSlackMessage(webhookUrl: string, params: {
issue: string,
displayName: string,
......@@ -22,6 +26,32 @@ export async function sendSlackMessage(webhookUrl: string, params: {
}
}
/**
* Upload a file to Slack.
* @param token Bot token (e.g., xoxb-...)
* @param channel Slack channel ID or name (e.g., #general)
* @param filePath Local path to the file to upload
* @param initialComment Optional comment to post with the file
*/
export async function sendSlackFile(
token: string,
channel: string,
filePath: string,
initialComment?: string
) {
const web = new WebClient(token);
const response = await web.files.uploadV2({
channels: channel,
file: createReadStream(filePath),
filename: basename(filePath),
initial_comment: initialComment,
});
if (!response.ok) {
throw new Error(`Slack 文件上传失败: ${response.error}`);
}
}
// 示例用法
// sendSlackMessage('https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX', {
// issue: 'Theme color missing',
......
import { sendSlackFile } from '../clients/slack';
import { readdirSync } from 'fs';
import { join } from 'path';
async function uploadAll() {
const token = process.env.SLACK_TOKEN!;
const channel = process.env.SLACK_CHANNEL!;
const dir = join(__dirname, 'diffWithBaseline');
const files = readdirSync(dir).filter(f => f.endsWith('.json'));
for (const file of files) {
const filePath = join(dir, file);
console.log(`上传 ${file} 到 Slack…`);
await sendSlackFile(token, channel, filePath, `Diff 文件:${file}`);
}
}
uploadAll().catch(err => {
console.error(err);
process.exit(1);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment