Skip to content

Commit 3e8f6a7

Browse files
committed
Completed telegram bot
1 parent 9ccd098 commit 3e8f6a7

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def();
3636
ws.logs();
3737
})();
3838

39-
if (process.env.NODE_ENV === "production") {
39+
if (process.env.NODE_ENV.includes("production")) {
4040
nodeCron.schedule(
4141
`*/${process.env.CHECK_INACTIVE_USERS_DURATION} * * * *`,
4242
() => {

package-lock.json

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
},
2020
"homepage": "https://github.com/mmdzov/luIP-marzban#readme",
2121
"dependencies": {
22+
"@grammyjs/files": "^1.0.4",
23+
"@grammyjs/menu": "^1.2.1",
2224
"@grammyjs/runner": "^2.0.3",
2325
"axios": "^1.4.0",
2426
"body-parser": "^1.20.2",

telegram/tg.js

+83-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
const { Bot } = require("grammy");
2-
const { run, sequentialize } = require("@grammyjs/runner");
1+
const { Bot, session, InputFile } = require("grammy");
2+
const { run } = require("@grammyjs/runner");
33
const { SocksProxyAgent } = require("socks-proxy-agent");
4+
const { Menu } = require("@grammyjs/menu");
5+
const { hydrateFiles } = require("@grammyjs/files");
6+
const fs = require("fs");
7+
const { join } = require("path");
8+
const { File } = require("../utils");
49

510
function tg() {
611
if (Boolean(process.env.TG_ENABLE) === false) return;
712

8-
let bot = {};
13+
let bot = null;
914

10-
if (process.env.NODE_ENV === "development") {
15+
if (process.env.NODE_ENV.includes("development")) {
1116
const socksAgent = new SocksProxyAgent("socks://127.0.0.1:10808");
1217

1318
bot = new Bot(process.env.TG_TOKEN, {
@@ -19,6 +24,80 @@ function tg() {
1924
});
2025
} else bot = new Bot(process.env.TG_TOKEN);
2126

27+
bot.use((ctx, next) => {
28+
if (+ctx.chat.id !== +process.env.TG_ADMIN) return;
29+
30+
return next();
31+
});
32+
33+
bot.api.setMyCommands([
34+
{
35+
command: "start",
36+
description: "Start the bot",
37+
},
38+
]);
39+
40+
bot.api.config.use(hydrateFiles(bot.token));
41+
42+
const initial = () => ({
43+
waitingFor: "",
44+
});
45+
46+
bot.use(session({ initial }));
47+
48+
const menu = new Menu("main-menu")
49+
.text("Import backup", async (ctx) => {
50+
ctx.reply("Send me users.csv or users.json or both files");
51+
52+
ctx.session.waitingFor = "FILE";
53+
})
54+
.row()
55+
.text("Export backup", async (ctx) => {
56+
await ctx.replyWithDocument(
57+
new InputFile(join(__dirname, "../", "users.json"), "users.json"),
58+
);
59+
await ctx.replyWithDocument(
60+
new InputFile(join(__dirname, "../", "users.csv"), "users.csv"),
61+
);
62+
});
63+
64+
bot.use(menu);
65+
66+
bot.command("start", (ctx) => {
67+
ctx.reply(
68+
`
69+
Hi
70+
How can i help you ?
71+
`,
72+
{
73+
reply_markup: menu,
74+
},
75+
);
76+
});
77+
78+
bot.on(
79+
":document",
80+
(ctx, next) => {
81+
if (ctx.session.waitingFor === "FILE") return next();
82+
},
83+
async (ctx) => {
84+
const file = await ctx.getFile();
85+
86+
let filename = "";
87+
88+
if (file.file_path.includes(".csv")) {
89+
filename = join(__dirname, "../", "users.csv");
90+
} else if (file.file_path.includes(".json")) {
91+
filename = join(__dirname, "../", "users.json");
92+
} else return ctx.reply("Unknown file type");
93+
94+
fs.rmSync(filename);
95+
await file.download(filename);
96+
97+
ctx.reply("The backup was registered successfully");
98+
},
99+
);
100+
22101
const runner = run(bot);
23102

24103
if (runner.isRunning()) console.log("Bot is running");

0 commit comments

Comments
 (0)