我使这个电报机器人能够检测并删除任何广告链接,但是由于某些原因它无法正常工作,日志也仅给出了轮询错误,除此之外,我什么也不知道。这是代码:
require('dotenv').load();
const TelegramBot = require('node-telegram-bot-api');
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(process.env.TOKEN, { polling: true });
function banAndKick(chatId, userId, msgId) {
bot.kickChatMember(chatId, userId);
bot.sendMessage(chatId, `Removed sspam and banned a user.`)
bot.deleteMessage(chatId, msgId);
}
bot.onText(/u.to\//, (msg, match) => {
const chatId = msg.chat.id;
console.log(`Spam message: ${msg.text}`)
banAndKick(chatId, msg.from.id, msg.message_id)
})
// Listen for any kind of message
bot.on('message', (msg) => {
const chatId = msg.chat.id;
if (msg.forward_from_chat) {
console.log(`Forwareded message: ${msg.text}`)
if (msg.entities[0].url) {
console.log(`Message has a link: ${msg.text}`);
banAndKick(chatId, msg.from.id, msg.message_id)
}
}
if (msg.caption.match(/t.cn/g)) {
banAndKick(chatId, msg.from.id, msg.message_id)
}
if (msg.caption.match(/^hi$/g)) {
banAndKick(chatId, msg.from.id, msg.message_id)
}
if (msg.text.match(/t.me\/bitcoin/g)) {
console.log(`Spam message: ${msg.text}`)
banAndKick(chatId, msg.from.id, msg.message_id)
}
if (msg.text.match(/t.me\/joinchat\/*/)) {
console.log(`Spam message: ${msg.text}`)
banAndKick(chatId, msg.from.id, msg.message_id)
}
});