mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(telegram-bot): initialize
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
"id": "1038762591805247518"
|
||||
},
|
||||
|
||||
"telegram": {
|
||||
"token": "YOUR-BOT-TOKEN-HERE"
|
||||
},
|
||||
|
||||
"server": {
|
||||
"port": 3000,
|
||||
"host": "192.168.1.6"
|
||||
|
||||
30
bots/telegram/commands/train.js
Normal file
30
bots/telegram/commands/train.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export default {
|
||||
command: /\/train/,
|
||||
async execute(msg) {
|
||||
console.log(msg);
|
||||
if (msg.reply_to_message.message_id === msg.message_thread_id) return global.bot.sendMessage(msg.chat.id, 'Please reply to a message!', {
|
||||
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id
|
||||
});
|
||||
|
||||
const options = [];
|
||||
|
||||
for (const { label } of global.config.responses) {
|
||||
options.push({
|
||||
text: label,
|
||||
callback_data: `label_${label.toLowerCase()}`
|
||||
});
|
||||
}
|
||||
|
||||
const admins = await global.bot.getChatAdministrators(msg.chat.id);
|
||||
const isAdmin = admins.find(admin => admin.user.id === msg.from.id);
|
||||
|
||||
if (!isAdmin) return global.bot.sendMessage(msg.chat.id, 'You\'re not an admin.', {
|
||||
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.message_id
|
||||
});
|
||||
global.bot.sendMessage(msg.chat.id, 'Please select the corresponding label to train the bot.', {
|
||||
message_thread_id: msg.message_thread_id, reply_to_message_id: msg.reply_to_message.message_id, reply_markup: {
|
||||
inline_keyboard: [options]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
18
bots/telegram/events/helper/aiResponse.js
Normal file
18
bots/telegram/events/helper/aiResponse.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export default {
|
||||
name: 'aiResponse',
|
||||
once: false,
|
||||
async execute(aiRes) {
|
||||
const response = global.config.responses.find(
|
||||
(res) => res.label === aiRes.predictions[0].label
|
||||
);
|
||||
if (!response) return;
|
||||
|
||||
if (Number(aiRes.predictions[0].score) >= response.threshold) {
|
||||
const ids = aiRes.id.split('/');
|
||||
|
||||
global.bot.sendMessage(ids[0], response.text, { message_thread_id: ids[1], reply_to_message_id: ids[2] });
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
18
bots/telegram/events/telegram/callbackQuery.js
Normal file
18
bots/telegram/events/telegram/callbackQuery.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export default {
|
||||
name: 'callback_query',
|
||||
once: false,
|
||||
async execute(cb) {
|
||||
const admins = await global.bot.getChatAdministrators(cb.message.chat.id);
|
||||
const isAdmin = admins.find(admin => admin.user.id === cb.message.from.id);
|
||||
|
||||
if (!isAdmin) return global.bot.sendMessage(cb.message.chat.id, 'You\'re not an admin.', {
|
||||
message_thread_id: cb.message.message_thread_id, reply_to_message_id: cb.message.message_id
|
||||
});;
|
||||
|
||||
global.helper.sendTrainData(cb.message.reply_to_message.text.toLowerCase(), cb.data.replace('label_', '').toUpperCase());
|
||||
|
||||
global.bot.sendMessage(cb.message.chat.id, 'Sent train data to server.', {
|
||||
message_thread_id: cb.message.message_thread_id, reply_to_message_id: cb.message.message_id
|
||||
});
|
||||
}
|
||||
}
|
||||
7
bots/telegram/events/telegram/message.js
Normal file
7
bots/telegram/events/telegram/message.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export default {
|
||||
name: 'message',
|
||||
once: false,
|
||||
async execute(msg) {
|
||||
global.helper.scanText(msg.text.toLowerCase(), `${msg.chat.id}/${msg.message_thread_id}/${msg.message_id}`);
|
||||
}
|
||||
}
|
||||
66
bots/telegram/index.js
Normal file
66
bots/telegram/index.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import TelegramBot from 'node-telegram-bot-api';
|
||||
import { readFileSync, readdirSync } from 'node:fs';
|
||||
// Fix __dirname not being defined in ES modules. (https://stackoverflow.com/a/64383997)
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
import HelperClient from '../../client/index.js';
|
||||
|
||||
const configJSON = readFileSync('../config.json', 'utf-8');
|
||||
global.config = JSON.parse(configJSON);
|
||||
|
||||
global.helper = new HelperClient(global.config);
|
||||
global.helper.connect();
|
||||
|
||||
global.bot = new TelegramBot(config.telegram.token, { polling: true });
|
||||
|
||||
const commandsPath = join(__dirname, 'commands');
|
||||
const commandFiles = readdirSync(commandsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of commandFiles) {
|
||||
const filePath = join(commandsPath, file);
|
||||
const command = (await import(`file://${filePath}`)).default;
|
||||
if ('command' in command && 'execute' in command) {
|
||||
global.bot.onText(command.command, (...args) => command.execute(...args));
|
||||
} else {
|
||||
console.log(
|
||||
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const tgEventsPath = join(__dirname, 'events/telegram');
|
||||
const tgEventFiles = readdirSync(tgEventsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of tgEventFiles) {
|
||||
const filePath = join(tgEventsPath, file);
|
||||
const event = (await import(`file://${filePath}`)).default;
|
||||
if (event.once) {
|
||||
global.bot.once(event.name, (...args) => event.execute(...args));
|
||||
} else {
|
||||
global.bot.on(event.name, (...args) => event.execute(...args));
|
||||
}
|
||||
}
|
||||
|
||||
// The ReVanced Helper events.
|
||||
|
||||
const helperEventsPath = join(__dirname, 'events/helper');
|
||||
const helperEventFiles = readdirSync(helperEventsPath).filter((file) =>
|
||||
file.endsWith('.js')
|
||||
);
|
||||
|
||||
for (const file of helperEventFiles) {
|
||||
const filePath = join(helperEventsPath, file);
|
||||
const event = (await import(`file://${filePath}`)).default;
|
||||
if (event.once) {
|
||||
global.helper.once(event.name, (...args) => event.execute(...args));
|
||||
} else {
|
||||
global.helper.on(event.name, (...args) => event.execute(...args));
|
||||
}
|
||||
}
|
||||
1994
bots/telegram/package-lock.json
generated
Normal file
1994
bots/telegram/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
bots/telegram/package.json
Normal file
15
bots/telegram/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "telegram-bot",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Reis Can",
|
||||
"license": "GPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"node-telegram-bot-api": "^0.60.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user