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" ) ;
3
3
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" ) ;
4
9
5
10
function tg ( ) {
6
11
if ( Boolean ( process . env . TG_ENABLE ) === false ) return ;
7
12
8
- let bot = { } ;
13
+ let bot = null ;
9
14
10
- if ( process . env . NODE_ENV === "development" ) {
15
+ if ( process . env . NODE_ENV . includes ( "development" ) ) {
11
16
const socksAgent = new SocksProxyAgent ( "socks://127.0.0.1:10808" ) ;
12
17
13
18
bot = new Bot ( process . env . TG_TOKEN , {
@@ -19,6 +24,80 @@ function tg() {
19
24
} ) ;
20
25
} else bot = new Bot ( process . env . TG_TOKEN ) ;
21
26
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
+
22
101
const runner = run ( bot ) ;
23
102
24
103
if ( runner . isRunning ( ) ) console . log ( "Bot is running" ) ;
0 commit comments