-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
217 lines (200 loc) · 6.78 KB
/
Copy pathmain.js
File metadata and controls
217 lines (200 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import TelegramBot from 'node-telegram-bot-api';
import 'dotenv/config';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import execute from './lib/execute.js';
import { createLog } from './lib/log.js';
const logError = createLog();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const commands = [];
const addCommand = (cmd) => {
commands.push(cmd);
};
const readDir = (cmdir) => {
const files = fs.readdirSync(cmdir);
for (let file of files) {
const fullPath = path.join(cmdir, file);
if (fs.statSync(fullPath).isDirectory()) {
readDir(fullPath);
} else if (path.extname(fullPath) === '.js') {
import(fullPath).then(module => addCommand(module.default || module));
}
}
};
function checkInternet() {
return new Promise((resolve) => {
dns.lookup('telegram.org', (err) => {
if (err && err.code === "ENOTFOUND") {
resolve(false);
} else {
resolve(true);
}
});
});
}
export const runNodeBot = async () => {
try {
await new Promise((resolve) => {
readDir(path.join(__dirname, './command'));
resolve(true);
});
const bot = new TelegramBot(process.env.TOKEN, { polling: true });
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
const messageId = msg.message_id;
const userName = msg.chat.username;
let text;
if (msg.text) {
msg.body = msg.text;
} else if (msg.caption) {
msg.body = msg.caption;
} else {
msg.body = "";
}
if (!msg.body) return
const isCmd = msg.body.startsWith('/');
if (!isCmd) return
//const cmd = msg.body.trim().replace('/', "").split(" ")[0].toLowerCase();
const cmd = msg.body
.replace('/', "")
.trim()
.split(/ +/)
.shift()
.toLowerCase();
//console.log(msg.body)
const userIds = process.env.USERID.split(',')
const owner = userIds.some(id => id.trim() === chatId.toString());
const msgden = `<blockquote>USER: @${userName}\nID: ${chatId}\n⚠️ ACCESS DENIED ⚠️\n\nJika kamu owner dari bot ini, silahkan ganti USERID anda dengan mengetik diterminal node-bot -cc dan ketik 2 lalu masukkan userid anda <b>${chatId}</b> lalu restart service\nnode-bot restart.</blockquote>`;
if (!owner) return bot.sendMessage(chatId,msgden,{
parse_mode: 'html',
reply_to_message_id: messageId
})
const options = {
chat_id: chatId,
message_id: messageId + 1,
parse_mode: "html",
disable_web_page_preview: true
}
if (msg.body === '/restartbot'){
await bot.sendMessage(chatId, 'successful restart bot, please wait..', {
reply_to_message_id: messageId
})
await process.send('restart')
}
const caseMsg = await import('./lib/message.js');
caseMsg.message(bot, cmd, msg, chatId, messageId, options);
switch (cmd) {
case 'start':
bot.sendMessage(chatId, `<b>Hi @${msg.from.username}, please click the menu button to view all bot commands.</b>`, {
reply_markup: {
inline_keyboard: [
[{
text: 'menu',
callback_data: 'menu'
}]
]
},
reply_to_message_id: messageId,
parse_mode: "html"
});
break
case 'stopbot':
bot.sendMessage(chatId, 'are you sure you want to stop bots?', {
reply_markup: {
inline_keyboard: [
[{
text: 'yes',
callback_data: 'stop bot'
}],
[{
text: 'nooooo',
callback_data: 'cancel'
}
]
]
},
reply_to_message_id: messageId
});
break;
default:
}
bot.reply = (text) => {
bot.editMessageText(`<b>${text}</b>`, {
chat_id: chatId,
message_id: messageId + 1,
parse_mode: "HTML",
disable_web_page_preview: true
});
};
bot.send = (text) => {
bot.sendMessage(chatId, text, {
parse_mode: "HTML",
disable_web_page_preview: true
});
};
for (let command of commands) {
if (command.cmds && command.cmds.includes(cmd)) {
await bot.sendChatAction(chatId, 'typing');
await bot.sendMessage(chatId, "loading..", {
reply_to_message_id: messageId
});
await command.exec(bot, msg, chatId, messageId);
}
}
});
bot.on('callback_query', async (query) => {
const module = await import('./lib/callback.js');
const chatId = query.message.chat.id;
const messageId = query.message.message_id;
const data = query.data;
module.callback(bot, query, chatId, messageId, data);
});
bot.on('video', (msg) => {
const fileName = msg.video.file_id;
bot.sendMessage(msg.chat.id, `<blockquote>video uploaded successfully, if you want to upload to server reply to the file with the text /upfile /path/namefilevideo.mp4 for example /upfile /root/video.mp4</blockquote>`, {
parse_mode: "HTML",
disable_web_page_preview: true
})
});
bot.on('photo', (msg) => {
const fileName = msg.photo[3].file_id;
bot.sendMessage(msg.chat.id, `<blockquote>photo uploaded successfully, if you want to upload to server reply to the file with the text /upfile /path/namefilephoto.png for example /upfile /root/img.png</blockquote>`, {
parse_mode: "HTML",
disable_web_page_preview: true
});
});
bot.on('document', (msg) => {
const fileName = msg.document.file_name
bot.sendMessage(msg.chat.id, `<blockquote>${fileName} uploaded successfully, if you want to upload to server reply to the file with the text /upfile /path/dir for example /upfile /root</blockquote>`, {
parse_mode: "HTML",
disable_web_page_preview: true
});
});
bot.on('polling_error', (error) => {
logError(error)
if (error.response && error.response.statusCode) {
if (error.response.statusCode === 409) {
bot.sendMessage(process.env.USERID, '⚠️ bot is already running in the background, force off the bot ..');
process.send('stop')
} else if (error.response.statusCode === 401) {
bot.sendMessage(process.env.USERID, 'The API token used is invalid. Make sure the token used is correct and registered in BotFather, please check token and restart service.');
process.send('stop')
} else {
bot.sendMessage(process.env.USERID, `${error.message}\nforce restart service`)
process.send('restart')
}
}
console.log(error)
});
} catch (e){
const isOnline = await checkInternet();
logError(e)
if(!isOnline) {
logError('internet off detected!!')
}
console.log(e)
}
};
runNodeBot();