From 7f6229ee9a5034a1ae95f3c5e8fb009f606a80cd Mon Sep 17 00:00:00 2001 From: "Andika E. Kurnia" <73420758+DikDns@users.noreply.github.com> Date: Thu, 25 Jul 2024 12:07:52 +0700 Subject: [PATCH] feat: add npm run tl:set-command script --- bot/bot.ts | 10 +++--- package-lock.json | 84 +++++++++++++++++++++++------------------------ package.json | 3 +- setCommands.js | 59 +++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 setCommands.js diff --git a/bot/bot.ts b/bot/bot.ts index 1d79d7b..fd5e873 100644 --- a/bot/bot.ts +++ b/bot/bot.ts @@ -66,7 +66,7 @@ const Bot = (token: string) => { chatId: number, text: string, options?: TelegramAPI.SendMessageOptions - ): Promise => { + ): Promise => { try { const defaultOptions: TelegramAPI.SendMessageOptions = { ...options, @@ -86,15 +86,15 @@ const Bot = (token: string) => { return data; } catch (error) { console.error(error); - return error; + return; } }, }; - const commands = { + const commands: Commands = { start: { command: "/start", - description: "Memulai bot HIMARPL.", + description: "Memulai bot HIMARPL", handler: async (chatId: number) => { const message = ` *Halo, apakah ada yang bisa dibantu?*\n\n /notifyme - Untuk mendapatkan notifikasi terkait postingan terbaru dari [Blog HIMARPL](https://blog.himarpl.com) \n\n| [Website](https://www.himarpl.com) | [Instagram](https://instagram.com/himarpl) | [Youtube](https://www.youtube.com/@himarplcibiru5901) | [TikTok](https://www.tiktok.com/@himarpl) | @@ -109,7 +109,7 @@ const Bot = (token: string) => { return { ...methods, - + commands, listenCommands: async (chatId: number, rawTextCommand: string) => { if (!rawTextCommand.startsWith("/")) return; diff --git a/package-lock.json b/package-lock.json index b61fd6c..eb43bd4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -556,6 +556,48 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.11.tgz", "integrity": "sha512-3oJbGBUWuS6ahSnEq1eN2XrCyf4YsWI8OyCvo7c64zQJNplk3mO84t53o8lfTk+2ji59g5ycfc6qQ3fdHliHuA==" }, + "node_modules/@vercel/node/node_modules/ts-node": { + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, "node_modules/@vercel/static-config": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@vercel/static-config/-/static-config-3.0.0.tgz", @@ -2354,48 +2396,6 @@ "code-block-writer": "^10.1.1" } }, - "node_modules/ts-node": { - "version": "10.9.1", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", - "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", - "dependencies": { - "@cspotcode/source-map-support": "^0.8.0", - "@tsconfig/node10": "^1.0.7", - "@tsconfig/node12": "^1.0.7", - "@tsconfig/node14": "^1.0.0", - "@tsconfig/node16": "^1.0.2", - "acorn": "^8.4.1", - "acorn-walk": "^8.1.1", - "arg": "^4.1.0", - "create-require": "^1.1.0", - "diff": "^4.0.1", - "make-error": "^1.1.1", - "v8-compile-cache-lib": "^3.0.1", - "yn": "3.1.1" - }, - "bin": { - "ts-node": "dist/bin.js", - "ts-node-cwd": "dist/bin-cwd.js", - "ts-node-esm": "dist/bin-esm.js", - "ts-node-script": "dist/bin-script.js", - "ts-node-transpile-only": "dist/bin-transpile.js", - "ts-script": "dist/bin-script-deprecated.js" - }, - "peerDependencies": { - "@swc/core": ">=1.2.50", - "@swc/wasm": ">=1.2.50", - "@types/node": "*", - "typescript": ">=2.7" - }, - "peerDependenciesMeta": { - "@swc/core": { - "optional": true - }, - "@swc/wasm": { - "optional": true - } - } - }, "node_modules/ts-toolbelt": { "version": "6.15.5", "resolved": "https://registry.npmjs.org/ts-toolbelt/-/ts-toolbelt-6.15.5.tgz", diff --git a/package.json b/package.json index 4720404..0306707 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "ngrok": "npx ngrok http http://localhost:3000", "db:studio": "npx prisma studio", "db:push": "npx prisma db push", - "tl:set-webhook": "node setWebhook.js" + "tl:set-webhook": "node setWebhook.js", + "tl:set-command": "node setCommands.js" }, "devDependencies": { "@types/node": "^17.0.42", diff --git a/setCommands.js b/setCommands.js new file mode 100644 index 0000000..89ae023 --- /dev/null +++ b/setCommands.js @@ -0,0 +1,59 @@ +import dotenv from "dotenv"; + +dotenv.config(); + +async function deleteCommands() { + console.log("\n=> Deleting commands..."); + + const response = await fetch( + `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/deleteMyCommands`, + { + method: "POST", + headers: { + "Content-type": "application/json", + }, + } + ); + + const data = await response.json(); + + if (!data.ok) { + console.error("=! Failed to delete commands:", data); + process.exit(1); + } + + console.log("=> Successfully deleted commands:", data); +} + +async function setCommands() { + console.log("\n=> Setting commands..."); + + const response = await fetch( + `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/setMyCommands`, + { + method: "POST", + headers: { + "Content-type": "application/json", + }, + body: JSON.stringify({ + commands: [{ command: "/start", description: "Memulai bot HIMARPL" }], + }), + } + ); + + const data = await response.json(); + + if (!data.ok) { + console.error("=! Failed to set commands:", data); + process.exit(1); + } + + console.log("=> Successfully set commands:", data); +} + +async function main() { + await deleteCommands(); + await setCommands(); +} + +main();