管理一個擁有 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中文網其他相關文章!