add: bot:
This commit is contained in:
34
ts/.gitignore
vendored
Normal file
34
ts/.gitignore
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
# dependencies (bun install)
|
||||
node_modules
|
||||
|
||||
# output
|
||||
out
|
||||
dist
|
||||
*.tgz
|
||||
|
||||
# code coverage
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# logs
|
||||
logs
|
||||
_.log
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# caches
|
||||
.eslintcache
|
||||
.cache
|
||||
*.tsbuildinfo
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
13
ts/README.md
Normal file
13
ts/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# ts
|
||||
|
||||
To install dependencies:
|
||||
|
||||
```bash
|
||||
bun install
|
||||
```
|
||||
|
||||
To run:
|
||||
|
||||
```bash
|
||||
bun run send.ts
|
||||
```
|
||||
52
ts/bun.lock
Normal file
52
ts/bun.lock
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"lockfileVersion": 1,
|
||||
"configVersion": 1,
|
||||
"workspaces": {
|
||||
"": {
|
||||
"name": "ts",
|
||||
"dependencies": {
|
||||
"grammy": "^1.38.4",
|
||||
"zod": "^4.1.12",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest",
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5",
|
||||
},
|
||||
},
|
||||
},
|
||||
"packages": {
|
||||
"@grammyjs/types": ["@grammyjs/types@3.22.2", "", {}, "sha512-uu7DX2ezhnBPozL3bXHmwhLvaFsh59E4QyviNH4Cij7EdVekYrs6mCzeXsa2pDk30l3uXo7DBahlZLzTPtpYZg=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.3.3", "", { "dependencies": { "bun-types": "1.3.3" } }, "sha512-ogrKbJ2X5N0kWLLFKeytG0eHDleBYtngtlbu9cyBKFtNL3cnpDZkNdQj8flVf6WTZUX5ulI9AY1oa7ljhSrp+g=="],
|
||||
|
||||
"@types/node": ["@types/node@24.10.1", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ=="],
|
||||
|
||||
"abort-controller": ["abort-controller@3.0.0", "", { "dependencies": { "event-target-shim": "^5.0.0" } }, "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg=="],
|
||||
|
||||
"bun-types": ["bun-types@1.3.3", "", { "dependencies": { "@types/node": "*" } }, "sha512-z3Xwlg7j2l9JY27x5Qn3Wlyos8YAp0kKRlrePAOjgjMGS5IG6E7Jnlx736vH9UVI4wUICwwhC9anYL++XeOgTQ=="],
|
||||
|
||||
"debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="],
|
||||
|
||||
"event-target-shim": ["event-target-shim@5.0.1", "", {}, "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ=="],
|
||||
|
||||
"grammy": ["grammy@1.38.4", "", { "dependencies": { "@grammyjs/types": "3.22.2", "abort-controller": "^3.0.0", "debug": "^4.4.3", "node-fetch": "^2.7.0" } }, "sha512-z07Kin3HgRwMdy40KUs+c9fmNBvGlSxGwcqY8NAH0a8KULGFYEMQaFAo3ge0V5tvmgr02Jgubkf54KjHLAMCbw=="],
|
||||
|
||||
"ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
|
||||
|
||||
"node-fetch": ["node-fetch@2.7.0", "", { "dependencies": { "whatwg-url": "^5.0.0" }, "peerDependencies": { "encoding": "^0.1.0" }, "optionalPeers": ["encoding"] }, "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A=="],
|
||||
|
||||
"tr46": ["tr46@0.0.3", "", {}, "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="],
|
||||
|
||||
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
||||
|
||||
"undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
|
||||
|
||||
"webidl-conversions": ["webidl-conversions@3.0.1", "", {}, "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="],
|
||||
|
||||
"whatwg-url": ["whatwg-url@5.0.0", "", { "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" } }, "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw=="],
|
||||
|
||||
"zod": ["zod@4.1.12", "", {}, "sha512-JInaHOamG8pt5+Ey8kGmdcAcg3OL9reK8ltczgHTAwNhMys/6ThXHityHxVV2p3fkw/c+MAvBHFVYHFZDmjMCQ=="],
|
||||
}
|
||||
}
|
||||
17
ts/package.json
Normal file
17
ts/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "ts",
|
||||
"module": "send.ts",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"author": "TypeFlu <TypeFlu@gmail.com> (https://Typeflu.me/)",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
},
|
||||
"dependencies": {
|
||||
"grammy": "^1.38.4",
|
||||
"zod": "^4.1.12"
|
||||
}
|
||||
}
|
||||
199
ts/send.ts
Normal file
199
ts/send.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
import { Bot, InlineKeyboard } from "grammy";
|
||||
import { z } from "zod";
|
||||
|
||||
const EnvSchema = z.object({
|
||||
BOT_TOKEN: z.string(),
|
||||
TELEGRAM_GROUP_ID: z.string(),
|
||||
TELEGRAM_TOPIC_COMMITS: z.string(),
|
||||
TELEGRAM_TOPIC_PRS: z.string(),
|
||||
GITHUB_TOKEN: z.string(),
|
||||
GITHUB_EVENT_PATH: z.string(),
|
||||
});
|
||||
const env = EnvSchema.parse(process.env);
|
||||
|
||||
const bot = new Bot(env.BOT_TOKEN);
|
||||
|
||||
const PullRequestSchema = z.object({
|
||||
action: z.string(),
|
||||
number: z.number(),
|
||||
repository: z.object({
|
||||
full_name: z.string(),
|
||||
html_url: z.url(),
|
||||
}),
|
||||
pull_request: z.object({
|
||||
url: z.url(),
|
||||
title: z.string(),
|
||||
body: z.string().nullable(),
|
||||
user: z.object({
|
||||
login: z.string(),
|
||||
html_url: z.url(),
|
||||
}),
|
||||
head: z.object({ ref: z.string() }),
|
||||
base: z.object({ ref: z.string() }),
|
||||
changed_files: z.number(),
|
||||
additions: z.number(),
|
||||
deletions: z.number(),
|
||||
}),
|
||||
});
|
||||
|
||||
const PushSchema = z.object({
|
||||
ref: z.string(),
|
||||
repository: z.object({
|
||||
full_name: z.string(),
|
||||
html_url: z.url(),
|
||||
}),
|
||||
head_commit: z
|
||||
.object({
|
||||
id: z.string(),
|
||||
url: z.url(),
|
||||
message: z.string(),
|
||||
author: z.object({
|
||||
name: z.string(),
|
||||
email: z.email(),
|
||||
}),
|
||||
added: z.array(z.string()),
|
||||
modified: z.array(z.string()),
|
||||
removed: z.array(z.string()),
|
||||
})
|
||||
.nullable(),
|
||||
});
|
||||
|
||||
function detectLanguage(files: string[]): string {
|
||||
const extCount = files.reduce<Record<string, number>>((count, file) => {
|
||||
const ext = file.split(".").pop()?.toLowerCase() ?? "";
|
||||
count[ext] = (count[ext] || 0) + 1;
|
||||
return count;
|
||||
}, {});
|
||||
if (extCount.kt || extCount.kts) return "Kotlin";
|
||||
if (extCount.c && !extCount.h) return "C";
|
||||
if (extCount.rs) return "Rust";
|
||||
if (extCount.sh) return "Shell";
|
||||
if (extCount.ts || extCount.tsx) return "TypeScript";
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
interface GitHubPRFilesResponse {
|
||||
data: {
|
||||
repository: {
|
||||
pullRequest: {
|
||||
files: {
|
||||
nodes: { path: string }[];
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchPrFiles(
|
||||
repoFullName: string,
|
||||
prNumber: number,
|
||||
): Promise<string[]> {
|
||||
const query = `
|
||||
query($owner: String!, $name: String!, $number: Int!) {
|
||||
repository(owner: $owner, name: $name) {
|
||||
pullRequest(number: $number) {
|
||||
files(first: 100) {
|
||||
nodes { path }
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
const [owner, name] = repoFullName.split("/");
|
||||
const resp = await fetch("https://api.github.com/graphql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `bearer ${env.GITHUB_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
variables: { owner, name, number: prNumber },
|
||||
}),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
throw new Error(`GitHub API error: ${resp.status} ${resp.statusText}`);
|
||||
}
|
||||
const json = (await resp.json()) as GitHubPRFilesResponse;
|
||||
if (!json?.data?.repository?.pullRequest?.files?.nodes) return [];
|
||||
return json.data.repository.pullRequest.files.nodes.map((n) => n.path);
|
||||
}
|
||||
|
||||
async function formatPrMessage(
|
||||
evt: z.infer<typeof PullRequestSchema>,
|
||||
): Promise<string> {
|
||||
const files = await fetchPrFiles(evt.repository.full_name, evt.number);
|
||||
const lang = detectLanguage(files);
|
||||
return `Repository: [${evt.repository.full_name}](${evt.repository.html_url})
|
||||
Pull Request #: [${evt.number}](${evt.pull_request.url})
|
||||
Author: [${evt.pull_request.user.login}](${evt.pull_request.user.html_url})
|
||||
Files Changed: ${evt.pull_request.changed_files}, +${evt.pull_request.additions}/- ${evt.pull_request.deletions}
|
||||
Likely Language: ${lang}
|
||||
Title: ${evt.pull_request.title}
|
||||
Description: ${evt.pull_request.body ?? "_None provided_"}`;
|
||||
}
|
||||
|
||||
function formatPushMessage(evt: z.infer<typeof PushSchema>): string {
|
||||
const c = evt.head_commit;
|
||||
if (!c) {
|
||||
return `Repository: [${evt.repository.full_name}](${evt.repository.html_url})
|
||||
Push event but no head commit data.`;
|
||||
}
|
||||
const details = [
|
||||
c.added.length ? `Added: ${c.added.join(", ")}` : "",
|
||||
c.modified.length ? `Modified: ${c.modified.join(", ")}` : "",
|
||||
c.removed.length ? `Removed: ${c.removed.join(", ")}` : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
const lang = detectLanguage([...c.added, ...c.modified, ...c.removed]);
|
||||
return `Repository: [${evt.repository.full_name}](${evt.repository.html_url})
|
||||
Commit: [${c.id}](${c.url})
|
||||
Author: ${c.author.name} <${c.author.email}>
|
||||
Message: ${c.message}
|
||||
${details ? details + "\n" : ""}Detected Language: ${lang}`;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
try {
|
||||
const raw = await Bun.file(env.GITHUB_EVENT_PATH).text();
|
||||
const parsed = JSON.parse(raw);
|
||||
|
||||
let message: string;
|
||||
let topic: number;
|
||||
|
||||
const prResult = PullRequestSchema.safeParse(parsed);
|
||||
if (prResult.success) {
|
||||
message = await formatPrMessage(prResult.data);
|
||||
topic = Number(env.TELEGRAM_TOPIC_PRS);
|
||||
} else {
|
||||
const pushResult = PushSchema.safeParse(parsed);
|
||||
if (!pushResult.success) {
|
||||
console.error(
|
||||
"Unrecognized event payload",
|
||||
prResult.error,
|
||||
pushResult.error,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
message = formatPushMessage(pushResult.data);
|
||||
topic = Number(env.TELEGRAM_TOPIC_COMMITS);
|
||||
}
|
||||
|
||||
const keyboard = new InlineKeyboard().url(
|
||||
"View on GitHub",
|
||||
(parsed.repository as any).html_url as string,
|
||||
);
|
||||
await bot.api.sendMessage(Number(env.TELEGRAM_GROUP_ID), message, {
|
||||
parse_mode: "Markdown",
|
||||
message_thread_id: topic,
|
||||
reply_markup: keyboard,
|
||||
});
|
||||
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error("Fatal error in main execution:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void main();
|
||||
29
ts/tsconfig.json
Normal file
29
ts/tsconfig.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
// Environment setup & latest features
|
||||
"lib": ["ESNext"],
|
||||
"target": "ESNext",
|
||||
"module": "Preserve",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"noImplicitOverride": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user