Skip to content

Commit

Permalink
feat: add npm run tl:set-command script
Browse files Browse the repository at this point in the history
  • Loading branch information
DikDns committed Jul 25, 2024
1 parent a276c10 commit 7f6229e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 48 deletions.
10 changes: 5 additions & 5 deletions bot/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const Bot = (token: string) => {
chatId: number,
text: string,
options?: TelegramAPI.SendMessageOptions
): Promise<TelegramAPI.Message> => {
): Promise<TelegramAPI.Message | undefined> => {
try {
const defaultOptions: TelegramAPI.SendMessageOptions = {
...options,
Expand All @@ -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) |
Expand All @@ -109,7 +109,7 @@ const Bot = (token: string) => {

return {
...methods,

commands,
listenCommands: async (chatId: number, rawTextCommand: string) => {
if (!rawTextCommand.startsWith("/")) return;

Expand Down
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions setCommands.js
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 7f6229e

Please sign in to comment.