-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
154 lines (121 loc) · 5.63 KB
/
bot.js
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
const { Telegraf, Markup, InputFile } = require('telegraf');
const { MongoClient } = require('mongodb');
const mongoUri = "YOUR MONGOURL";
const client = new MongoClient(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true });
let usersCollection;
let userFileSequences = {};
// start message//
async function startMessage(ctx) {
const userId = ctx.message.from.id;
const username = ctx.message.from.username;
const name = ctx.message.from.first_name;
updateUserInfo(userId, username, name);
const buttonURL = 'https://t.me/eBotHub';
const welcomeText = `Welcome, ${name}! 🌟 I am a file sequencing bot. Built with ❤️ using JavaScript and the Telegraf library.\n\n`;
const botDescription = `🤖 What I do:\nI help you sequence and organize your files. Use /ssequence to start the process. Send documents, videos, or audio files, and when you're done, use /esequence to get the sequenced files. Use /cancel to cancel all sequences.\n\n`;
const additionalInfo = `🔗 Owner: @ImMitsuoSuwa`;
const messageText = welcomeText + botDescription + additionalInfo;
await ctx.reply(messageText, Markup.inlineKeyboard([Markup.button.url('Updates!', buttonURL)]));
}
// function for processing ur files //
async function processFileSequence(ctx, fileType) {
const userId = ctx.message.from.id;
if (userId in userFileSequences) {
const user_data = userFileSequences[userId];
const file = ctx.message[fileType];
if (file) {
user_data.files.push(ctx.message);
ctx.reply('File received and added to the sequencing process.');
} else {
ctx.reply('Unsupported file type. Send documents or videos.');
}
}
}
// end sequence to get ur files //
async function endSequence(ctx) {
const userId = ctx.message.from.id;
if (userId in userFileSequences) {
const user_data = userFileSequences[userId];
if (user_data.files.length > 0) {
user_data.files.sort((a, b) => {
const fileA = a.document || a.video;
const fileB = b.document || b.video;
return fileA.file_name.localeCompare(fileB.file_name);
});
for (let i = 0; i < user_data.files.length; i++) {
const file_message = user_data.files[i];
const file = file_message.document || file_message.video;
if (file) {
const caption = file_message.caption || '';
if (file_message.document) {
await ctx.replyWithDocument(file.file_id, { caption });
} else if (file_message.video) {
await ctx.replyWithVideo(file.file_id, { caption });
}
}
}
ctx.reply(`File sequencing completed. You have received ${user_data.files.length} sequenced files.`);
const total_sequences = (await usersCollection.findOne({ user_id: userId }))?.total_sequences || 0;
usersCollection.updateOne({ user_id: userId }, { $set: { total_sequences: total_sequences + user_data.files.length } });
} else {
ctx.reply('No files to sequence. Send some files with /ssequence first.');
}
delete userFileSequences[userId];
} else {
ctx.reply('No ongoing file sequencing process. Use /ssequence to begin.');
}
}
async function updateUserInfo(userId, username, name) {
await usersCollection.updateOne(
{ user_id: userId },
{ $set: { username: username, name: name } },
{ upsert: true }
);
}
// bot stats //
async function showStats(ctx) {
const totalUsers = await usersCollection.countDocuments();
const totalSequences = (await usersCollection.aggregate([{ $group: { _id: null, total: { $sum: '$total_sequences' } } }]).toArray())[0]?.total || 0;
ctx.reply(`Total Users: ${totalUsers}\nTotal File Sequences: ${totalSequences}`);
}
client.connect().then(() => {
console.log('Connected to MongoDB');
const db = client.db('seq');
usersCollection = db.collection('users');
// fll ur bot token correctly//
const bot = new Telegraf('UR BOT TOKEN');
const port = process.env.PORT || 8080; // don't change port
bot.start(startMessage);
bot.command('ssequence', (ctx) => {
const userId = ctx.message.from.id;
if (userId in userFileSequences) {
ctx.reply('You are currently in a file sequencing process. Use /esequence to finish it.');
return;
}
userFileSequences[userId] = { files: [] };
ctx.reply('You have started a file sequencing process. Send the files you want to sequence one by one. ' +
'When you are done, use /esequence to finish and get the sequenced files.');
});
bot.on('document', (ctx) => processFileSequence(ctx, 'document'));
bot.on('video', (ctx) => processFileSequence(ctx, 'video'));
bot.on('audio', (ctx) => processFileSequence(ctx, 'audio'));
bot.command('esequence', endSequence);
bot.command('stats', showStats);
// cancel sequence//
bot.command('cancel', (ctx) => {
const userId = ctx.message.from.id;
if (userId in userFileSequences) {
delete userFileSequences[userId];
ctx.reply('File sequencing process canceled. Use /ssequence to start a new one.');
} else {
ctx.reply('No ongoing file sequencing process to cancel. Use /ssequence to begin.');
}
});
bot.launch({
port: port,
}).then(() => {
console.log(`Bot is running on port ${port}`);
}).catch((err) => {
console.error(`Error starting bot: ${err}`);
});
});