scnario commited on
Commit
c7254fd
·
verified ·
1 Parent(s): b2b927a

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +36 -0
server.js ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const TelegramBot = require('node-telegram-bot-api');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const os = require('os');
5
+ const express = require('express');
6
+
7
+ const bot = new TelegramBot('7727911031:AAFcPGWeyTynR1FR7Hhpr4HoGDeA4hk9Ebs', { polling: true });
8
+ const app = express();
9
+ const TMP_DIR = os.tmpdir();
10
+
11
+ const getFilePath = (chatId) => path.join(TMP_DIR, `${chatId}.txt`);
12
+ const readFilters = (chatId) => fs.existsSync(getFilePath(chatId)) ? JSON.parse(fs.readFileSync(getFilePath(chatId))) : {};
13
+ const saveFilters = (chatId, filters) => fs.writeFileSync(getFilePath(chatId), JSON.stringify(filters));
14
+
15
+ const isOwner = async (chatId, userId) => {
16
+ try {
17
+ const admins = await bot.getChatAdministrators(chatId);
18
+ return admins.find(a => a.status === 'creator')?.user.id === userId;
19
+ } catch {
20
+ return false;
21
+ }
22
+ };
23
+
24
+ bot.onText(/\/addfilters (\S+)/, async (msg, match) => {
25
+ if (!msg.reply_to_message || !(await isOwner(msg.chat.id, msg.from.id))) return;
26
+ const filters = readFilters(msg.chat.id);
27
+ filters[match[1].toLowerCase()] = msg.reply_to_message;
28
+ saveFilters(msg.chat.id, filters);
29
+ });
30
+
31
+ bot.on('message', (msg) => {
32
+ const reply = readFilters(msg.chat.id)[msg.text?.toLowerCase()];
33
+ if (reply) Object.keys(reply).forEach((k) => bot[`send${k[0].toUpperCase() + k.slice(1)}`](msg.chat.id, reply[k].file_id || reply[k]));
34
+ });
35
+
36
+ app.listen(7860, () => console.log('Running on port 7860'));