From 721b3870b8ada1c3094a457cf094954faac6c704 Mon Sep 17 00:00:00 2001 From: NaAlOH4 Date: Tue, 18 Jun 2024 15:29:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nojerry.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 nojerry.js diff --git a/nojerry.js b/nojerry.js new file mode 100644 index 0000000..c437bf7 --- /dev/null +++ b/nojerry.js @@ -0,0 +1,66 @@ +#!/bin/node +const https = require('https'); +const token = process.env.BOT_TOKEN; +if(!token) {console.log("need BOT_TOKEN env.");return;} +const apiUrl = `https://api.telegram.org/bot${token}/`; + +let lastUpdateId = 0; + +function getUpdates() { + const url = `${apiUrl}getUpdates?offset=${lastUpdateId + 1}`; + + https.get(url, (res) => { + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + const updates = JSON.parse(data).result; + + updates.forEach((update) => { + handleUpdate(update); + lastUpdateId = Math.max(update.update_id, lastUpdateId); + }); + + getUpdates(); + }); + }).on('error', (err) => { + console.error('Error: ', err); + setTimeout(getUpdates, 1000); + }); +} + +function handleUpdate(update) { + if (update && update.message && update.message.via_bot && update.message.via_bot.username === 'jerryxiaobot') { + deleteMessage(update.message); + } +} + +function deleteMessage(chatId, messageId) { + const url = `${apiUrl}deleteMessage?chat_id=${message.chat.id}&message_id=${message.message_id}`; + + https.get(url, (res) => { + let data = ''; + + res.on('data', (chunk) => { + data += chunk; + }); + + res.on('end', () => { + const result = JSON.parse(data); + if (result.ok) { + console.log('Message deleted successfully'); + } else { + console.log('Failed to delete message', result); + } + }); + }).on('error', (err) => { + console.error('Error: ', err); + }); +} + +// 开始轮询 +getUpdates(); +