nojerryinlinebot/nojerry.js

67 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2024-06-18 15:29:12 +08:00
#!/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();