管理一个拥有 3,000 名成员的 Telegram 群组不仅仅是为了增加成员数量,而是为了培育一个安全、相互尊重的社区。在多次遇到仇恨言论后,我开发了一个基于 Node.js 的审核机器人,可以自动识别和限制发布种族诽谤的用户。在本文中,我将引导您完成从构思到部署的整个开发过程。
手动管理大型 Telegram 群组会带来多项挑战:
首先,我们设置基本的机器人结构并进行适当的错误处理:
const TelegramBot = require('node-telegram-bot-api'); const express = require('express'); const bodyParser = require('body-parser'); const dotenv = require('dotenv'); const logging = require('console'); dotenv.config(); const BOT_TOKEN = process.env.BOT_TOKEN; const bot = new TelegramBot(BOT_TOKEN, { polling: true });
机器人实现了多层监控系统:
bot.on('message', async (msg) => { if (!msg.chat || !msg.from) return; // Message processing logic });
function checkForRacialSlurs(message) { if (!message) return false; return RACIAL_SLURS.some(slur => message.toLowerCase().includes(slur) ); }
async function restrictUser(chatId, userId) { try { await bot.restrictChatMember(chatId, userId, { permissions: { can_send_messages: false, can_send_media_messages: false, can_send_polls: false, can_send_other_messages: false, can_add_web_page_previews: false, can_change_info: false, can_invite_users: false, can_pin_messages: false, }, }); return true; } catch (error) { logging.error('Restriction failed:', error); return false; } }
该机器人包括一个人员布局系统,可轻松访问主持人信息:
const STAFF_LAYOUT = ` <b>GROUP STAFF</b> ? <b>Founder</b> └ @Sixademiks ⚜️ <b>Verified Vendors</b> ├ @Vendor1 (City1) └ @Vendor2 (City2) `;
新用户会收到带有 HTML 解析的格式化欢迎消息:
bot.onText(/\/start/, async (msg) => { try { const welcomeMessage = ` <b>Welcome to the DirtyNewz Bot!</b> Please read the <b>pinned messages</b> for the group rules...`; await bot.sendMessage(msg.chat.id, welcomeMessage, { parse_mode: 'HTML' }); } catch (error) { logging.error("Error in /start:", error); } });
全面的日志记录有助于跟踪机器人性能和用户违规行为:
logging.log('User was restricted due to violation'); logging.error('Error during restriction:', error);
在我们的 5,000 名成员组中部署机器人后,我们观察到:
最初,我们在高流量期间达到了 Telegram 的速率限制。解决方案:
const rateLimiter = { messageCount: 0, lastReset: Date.now(), check: function() { if (Date.now() - this.lastReset > 1000) { this.messageCount = 0; this.lastReset = Date.now(); } return this.messageCount++ < 30; } };
强大的错误处理功能可防止机器人崩溃:
process.on('unhandledRejection', (reason, promise) => { logging.error('Unhandled Rejection at:', promise, 'reason:', reason); }); bot.on('polling_error', (error) => { logging.error('Polling error:', error); });
高效的消息处理以处理大量信息:
const TelegramBot = require('node-telegram-bot-api'); const express = require('express'); const bodyParser = require('body-parser'); const dotenv = require('dotenv'); const logging = require('console'); dotenv.config(); const BOT_TOKEN = process.env.BOT_TOKEN; const bot = new TelegramBot(BOT_TOKEN, { polling: true });
该机器人部署在 Linux 服务器上,使用 PM2 进行进程管理:
bot.on('message', async (msg) => { if (!msg.chat || !msg.from) return; // Message processing logic });
定期监控至关重要:
function checkForRacialSlurs(message) { if (!message) return false; return RACIAL_SLURS.some(slur => message.toLowerCase().includes(slur) ); }
async function restrictUser(chatId, userId) { try { await bot.restrictChatMember(chatId, userId, { permissions: { can_send_messages: false, can_send_media_messages: false, can_send_polls: false, can_send_other_messages: false, can_add_web_page_previews: false, can_change_info: false, can_invite_users: false, can_pin_messages: false, }, }); return true; } catch (error) { logging.error('Restriction failed:', error); return false; } }
const STAFF_LAYOUT = ` <b>GROUP STAFF</b> ? <b>Founder</b> └ @Sixademiks ⚜️ <b>Verified Vendors</b> ├ @Vendor1 (City1) └ @Vendor2 (City2) `;
bot.onText(/\/start/, async (msg) => { try { const welcomeMessage = ` <b>Welcome to the DirtyNewz Bot!</b> Please read the <b>pinned messages</b> for the group rules...`; await bot.sendMessage(msg.chat.id, welcomeMessage, { parse_mode: 'HTML' }); } catch (error) { logging.error("Error in /start:", error); } });
为大型 Telegram 群组构建审核机器人,让我们学到了有关可扩展性、错误处理和社区管理的宝贵经验。该机器人显着改善了我们小组的环境,同时减少了主持人的工作量。
请记住,有效的审核是在自动化系统和人类监督之间找到适当的平衡。虽然这个机器人负责第一道防线,但它旨在补充而不是取代人类主持人。
如有问题请随时联系或查看我的 GitHub 上的完整代码!
以上是为 Telegram 构建可扩展的反仇恨言论审核机器人:深入探讨的详细内容。更多信息请关注PHP中文网其他相关文章!