Last active
December 5, 2022 19:04
-
-
Save MicrowaveDev/ac08719d5d121c424fe2ec8ffd9d5f02 to your computer and use it in GitHub Desktop.
Save last telegram messages and autodelete last messages by theme name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const lsMessages = { | |
}; | |
function pleaseRegister(msg) { | |
return sendMessageAndClear('register', msg, bot.sendMessage(msg.chat.id, `Please register in bot`, {parse_mode: 'HTML'})); | |
} | |
function alreadyRegistered(msg) { | |
return sendMessageAndClear('register', msg, bot.sendMessage(msg.chat.id, `You already registered`, {parse_mode: 'HTML'})); | |
} | |
async function sendMessageAndClear(themeName, receivedMsg, msgPromise) { | |
const [result] = await Promise.all([msgPromise, removeLastMessages(themeName, receivedMsg, bot)]); | |
saveLastMessages(themeName, receivedMsg, result); | |
} | |
function saveLastMessages(themeName, msg, result) { | |
const date = new Date().toLocaleDateString('en-GB'); | |
const chatId = msg.chat.id.toString(); | |
console.log('saveLastMessages ', date, chatId, themeName); | |
if (!lsMessages[date]) { | |
lsMessages[date] = {}; | |
} | |
if (!lsMessages[date][chatId]) { | |
lsMessages[date][chatId] = {}; | |
} | |
lsMessages[date][chatId][themeName] = [msg.message_id, result.message_id]; | |
const dateTwoDaysAgo = new Date(new Date().getTime() - 60 * 60 * 24 * 2000).toLocaleDateString('en-GB'); | |
if (lsMessages[dateTwoDaysAgo]) { | |
delete lsMessages[dateTwoDaysAgo]; | |
} | |
} | |
async function removeLastMessages(themeName, msg, bot) { | |
const date = new Date().toLocaleDateString('en-GB'); | |
const chatId = msg.chat.id.toString(); | |
console.log('removeLastMessages', date, chatId, themeName); | |
if (!lsMessages[date] || !lsMessages[date][chatId] || !lsMessages[date][chatId][themeName]) { | |
return; | |
} | |
await pIteration.forEach(lsMessages[date][chatId][themeName], msgId => bot.deleteMessage(chatId, msgId).catch(() => {})) | |
delete lsMessages[date][chatId][themeName]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment