Skip to content

Commit

Permalink
chore: send nightly build to telegram channel
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko233 committed Apr 8, 2024
1 parent 07c72f3 commit 0207ee0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
83 changes: 83 additions & 0 deletions scripts/telegram-upload-nightly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Telegraf } from "telegraf";
import { downloadFileToBuffer } from "./utils/fetch";
import { consola } from "./utils/logger";
import { execSync } from "child_process";
import { array2text } from "./utils";
import { getOctokit } from "@actions/github";

export const sendReleaseNotify = async () => {
if (!process.env.TELEGRAM_TOKEN) {
throw new Error("TELEGRAM_TOKEN is required");
}

if (!process.env.TELEGRAM_TO) {
throw new Error("TELEGRAM_TO is required");
}

if (!process.env.GITHUB_TOKEN) {
throw new Error("GITHUB_TOKEN is required");
}

const github = getOctokit(process.env.GITHUB_TOKEN);

const release = await github.rest.repos.getLatestRelease();

const fileList: {
name: string;
url: string;
buffer: Buffer;
}[] = [];

for (const item of release.data.assets) {
const supportedExtensions = [
"x64-setup.exe",
"x64_portable.zip",
"amd64.AppImage",
"amd64.deb",
"x64.dmg",
"aarch64.dmg",
];

if (supportedExtensions.some((ext) => item.name.endsWith(ext))) {
consola.log("Download file: " + item.name);

const buffer = await downloadFileToBuffer(item.browser_download_url);

fileList.push({
name: item.name,
url: item.browser_download_url,
buffer: buffer,
});
}
}

const bot = new Telegraf(process.env.TELEGRAM_TOKEN);

consola.log("Send media to Nyanpasu channel");

const GIT_SHORT_HASH = execSync("git rev-parse --short HEAD")
.toString()
.trim();

const caption = array2text([
"Clash Nyanpasu Nightly Build",
"",
`Current git short hash: ${GIT_SHORT_HASH}`,
]);

bot.telegram.sendMediaGroup(
process.env.TELEGRAM_TO,
fileList.map((file, index) => {
return {
type: "document",
caption: index == fileList.length ? caption : undefined,
media: {
source: file.buffer,
filename: file.name,
},
};
}),
);
};

sendReleaseNotify();
11 changes: 11 additions & 0 deletions scripts/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const downloadFileToBuffer = async (url: string): Promise<Buffer> => {
try {
const response = await fetch(url);

const buffer = Buffer.from(await response.arrayBuffer());

return buffer;
} catch (error) {
throw error;
}
};

0 comments on commit 0207ee0

Please sign in to comment.