forked from rn0x/TelegramWhisperer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
259 lines (218 loc) · 9.51 KB
/
index.mjs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import dotenv from 'dotenv';
dotenv.config();
import { Telegraf, session, Scenes } from 'telegraf';
import fs from 'fs-extra';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { downloadFile, deleteFile } from './utils/fileManager.mjs';
import { processAudio } from './utils/whisperService.mjs';
import handleMyChatMember from './utils/handleMyChatMember.mjs';
import handleText from './utils/handleText.mjs';
import displayMembers from './utils/displayMembers.mjs';
import getMembersCount from './utils/getMembersCount.mjs'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const supportedLanguages = [
'af', 'am', 'ar', 'as', 'az', 'ba', 'be', 'bg', 'bn', 'bo', 'br', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en',
'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'gl', 'gu', 'ha', 'haw', 'he', 'hi', 'hr', 'ht', 'hu', 'hy', 'id', 'is',
'it', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'la', 'lb', 'ln', 'lo', 'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn',
'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'nn', 'no', 'oc', 'pa', 'pl', 'ps', 'pt', 'ro', 'ru', 'sa', 'sd', 'si', 'sk',
'sl', 'sn', 'so', 'sq', 'sr', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tk', 'tl', 'tr', 'tt', 'uk', 'ur', 'uz',
'vi', 'yi', 'yo', 'yue', 'zh',
];
// الحد الأقصى المسموح به للمدة (بالدقائق)
const MAX_DURATION_MINUTES = 5; // على سبيل المثال، 5 دقائق
// إنشاء مشهد لاختيار اللغة
const languageScene = new Scenes.BaseScene('languageScene');
languageScene.enter((ctx) => {
ctx.reply(
'Please select the language of the audio from the list below (e.g., "ar" for Arabic):\n' +
supportedLanguages.join(', '),
{
reply_to_message_id: ctx?.message?.message_id
}
);
});
languageScene.on('text', (ctx) => {
const language = ctx.message.text.trim().toLowerCase(); // تجاهل حالة الأحرف
if (!supportedLanguages.includes(language)) {
return ctx.reply('❌ Invalid language. Please choose a supported language.', {
reply_to_message_id: ctx?.message?.message_id
});
}
ctx.session.language = language;
ctx.scene.enter('taskScene');
});
// إنشاء مشهد لاختيار المهمة
const taskScene = new Scenes.BaseScene('taskScene');
taskScene.enter((ctx) => {
ctx.reply(
'Choose the task you want to perform:\n' +
'1. Transcribe to the same language (type: "Transcribe").\n' +
'2. Translate to English (type: "Translate").',
{
reply_to_message_id: ctx?.message?.message_id
}
);
});
taskScene.on('text', async (ctx) => {
const task = ctx.message.text.trim().toLowerCase(); // تجاهل حالة الأحرف
if (task !== 'transcribe' && task !== 'translate') {
return ctx.reply('❌ Invalid task. Please choose either "Transcribe" or "Translate".', {
reply_to_message_id: ctx?.message?.message_id
});
}
ctx.session.task = task === 'transcribe' ? 'transcribe' : 'translate';
const { fileData, language } = ctx.session;
if (!fileData || !language) {
ctx.reply('❌ An error occurred! Please upload the file again.', {
reply_to_message_id: ctx?.message?.message_id
});
return ctx.scene.leave();
}
const options = {
audioPath: fileData.filePath,
task: ctx.session.task,
outputFormat: 'txt',
language: language,
};
try {
await ctx.reply('🔄 Processing the file, please wait...', {
reply_to_message_id: ctx?.message?.message_id
});
// معالجة الملف باستخدام processAudio
const result = await processAudio(options);
if (result?.path && fs.existsSync(result?.path)) {
// إرسال الملف أولاً
await ctx.telegram.sendDocument(ctx.chat.id, {
source: result.path,
filename: path.basename(result.path),
}, { reply_to_message_id: ctx?.session?.message_id });
// قراءة محتوى الملف
const fileContent = await fs.readFile(result.path, 'utf-8');
// تحقق من طول المحتوى
if (fileContent.length > 4096) {
// إذا كان المحتوى أطول من 4096 حرفًا، نقوم بتقسيمه
const chunks = [];
let i = 0;
while (i < fileContent.length) {
chunks.push(fileContent.slice(i, i + 4096)); // تقسيم إلى أجزاء بطول 4096
i += 4096;
}
// إرسال كل جزء على حدة
for (const chunk of chunks) {
await ctx.reply(chunk, {
parse_mode: 'Markdown',
reply_to_message_id: ctx?.session?.message_id
});
}
} else {
// إذا كان المحتوى أقل من الحد الأقصى، يتم إرساله كله في رسالة واحدة
await ctx.reply(fileContent,
{
parse_mode: 'Markdown',
reply_to_message_id: ctx?.session?.message_id
}
);
}
// حذف الملف المؤقت بعد الإرسال
await deleteFile(result.path);
await ctx.reply(
'✅ The file has been processed successfully!\n' +
'👥 [Join our channel](https://t.me/i8xApp) to continue using the bot and get more updates.',
{
parse_mode: 'Markdown',
reply_to_message_id: ctx?.session?.message_id,
disable_web_page_preview: true
}
);
} else {
await ctx.reply('❌ Error occurred while processing the file.');
}
} catch (error) {
console.error('Error during processing:', error);
await ctx.reply('❌ An error occurred while processing the file. Please try again.');
}
ctx.scene.leave();
});
// بوت تيليجرام
const bot = new Telegraf(process.env.BOT_TOKEN, {
handlerTimeout: Infinity
});
const botInfo = await bot.telegram.getMe();
// استخدام الجلسة
bot.use(session());
// إنشاء الـ Stage وربط المشاهد
const stage = new Scenes.Stage([languageScene, taskScene]);
bot.use(stage.middleware());
// استقبال الصوت أو الفيديو
bot.on(['voice', 'video'], async (ctx) => {
const fileId = ctx.message.voice?.file_id || ctx.message.video?.file_id;
const fileType = ctx.message.voice ? 'voice' : 'video';
// الحصول على مدة الصوت أو الفيديو
const duration = ctx.message.voice?.duration || ctx.message.video?.duration; // بالثواني
// تحقق إذا كانت المدة تتجاوز الحد المسموح به
if (duration > MAX_DURATION_MINUTES * 60) {
return ctx.reply(
`❌ The file is too long. The maximum allowed duration is ${MAX_DURATION_MINUTES} minutes. Please upload a shorter file.`,
{ reply_to_message_id: ctx?.message?.message_id }
);
}
try {
const fileLink = await ctx.telegram.getFileLink(fileId);
const downloadsDir = path.join(__dirname, 'downloads');
const filePath = path.join(
downloadsDir,
`${fileId}.${fileType === 'voice' ? 'mp3' : 'mp4'}` // تحديد الامتداد بناءً على نوع الملف
);
// تنزيل الملف
await downloadFile(fileLink.href, filePath);
// تخزين بيانات الملف في الجلسة
ctx.session.fileData = { filePath, fileType, message_id: ctx?.message?.message_id };
// بدء مشهد اختيار اللغة
ctx.scene.enter('languageScene');
} catch (error) {
console.error('Error handling file:', error);
ctx.reply('❌ An error occurred while uploading the file. Try again.');
}
});
// رسالة عند الضغط على زر Start
bot.start((ctx) => {
ctx.reply(
'👋 Welcome to the bot!\n\n' +
'To get started, follow these steps:\n' +
'1. Upload the audio or video file you want to process.\n' +
'2. Select the language that the audio is in.\n' +
'3. Choose the task you want to perform:\n' +
' - "Transcribe": To get the text in the same language.\n' +
' - "Translate": To translate the text to English.\n\n' +
'⚠️ Don’t forget to join our channel for updates!\n' +
'👥 Join our channel: [i8xApp](https://t.me/i8xApp)\n\n' +
'⬇️ Press "Start" to begin using the bot.',
{
parse_mode: 'Markdown',
reply_to_message_id: ctx?.message?.message_id,
disable_web_page_preview: true
}
);
});
bot.command('list', async (ctx) => {
await displayMembers(ctx);
});
bot.on('my_chat_member', async (ctx) => handleMyChatMember(ctx));
bot.on('text', async (ctx) => handleText(ctx));
bot.catch((error) => {
console.error('An error occurred:', error);
});
// تشغيل البوت
bot.launch();
const startupMessage = `
🤖 **Bot Startup Information**
📅 Current Time: ${new Date().toLocaleString()}
🚀 Bot Status: Operational
👥 Users: ${await getMembersCount()}
🤖 Bot Username: @${botInfo.username}
🌟 Enjoy using the bot!
`;
// Print startup message
console.log(startupMessage);