Last active
October 28, 2020 14:32
-
-
Save vlakam/b03eb5c04928272c56f2eb701bb8f7cf to your computer and use it in GitHub Desktop.
Workaround for https://github.com/telegramdesktop/tdesktop/issues/8226
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 { utimesSync } = require('fs'); | |
const { Airgram, Auth, isError, prompt, toObject, Message } = require('airgram'); | |
const probe = require('node-ffprobe'); | |
const tdLibFile = process.platform === 'win32' ? 'tdjson.dll' : 'libtdjson.so' | |
const { API_ID, API_HASH, CHAT_ID } = process.env; | |
const airgram = new Airgram({ | |
apiId: API_ID, | |
apiHash: API_HASH, | |
command: `./${tdLibFile}`, | |
databaseDirectory: './db', | |
logVerbosityLevel: 2 | |
}) | |
airgram.use(new Auth({ | |
code: () => prompt(`Please enter the secret code:\n`), | |
phoneNumber: () => prompt(`Please enter your phone number:\n`), | |
password: () => prompt('pass\n'), | |
})) | |
// async/await style of requests | |
void (async () => { | |
const me = toObject(await airgram.api.getMe()) | |
console.log('[Me] ', me) | |
})() | |
airgram.use((ctx, next) => { | |
return next() | |
}) | |
airgram.on('updateFile', async ({ update }) => { | |
console.log('[file upload]', update) | |
}) | |
// Getting new messages | |
airgram.on('updateNewMessage', async ({ update }) => { | |
const { message } = update | |
console.log('[new message]', message) | |
}) | |
const processChat = async function (chatId) { | |
const me = toObject(await airgram.api.getMe()) | |
console.log('[Me] ', me) | |
const { response: chats } = await airgram.api.getChats({ | |
limit: 10, | |
offsetChatId: 0, | |
offsetOrder: '9223372036854775807' | |
}) | |
console.log('[My chats] ', chats) | |
let lastMessageId = 0; | |
while (true) { | |
const currentMessages = await airgram.api.searchChatMessages({ chatId, fromMessageId: lastMessageId, limit: 100, filter: {_:'searchMessagesFilterDocument'} }) | |
if (toObject(currentMessages).messages.length === 0) { | |
console.log('Nothing to do'); | |
break; | |
} | |
if (isError(currentMessages)) { | |
console.log(currentMessages.response); | |
break; | |
} | |
let messages = toObject(currentMessages).messages; | |
let priority = 1; | |
for (const message of messages) { | |
if (!message.canBeEdited) { | |
console.log(`${message.id} cannot be edited`); | |
continue; | |
} | |
if (!message.content.audio) { | |
console.log(`${message.id} is not an audio`); | |
continue; | |
} | |
const { audio, performer, title} = message.content.audio; | |
const fileInfo = toObject(await airgram.api.getRemoteFile({ remoteFileId: audio.remote.id, fileType: { _: 'fileTypeAudio' } })); | |
const fileResponse = await airgram.api.downloadFile({ synchronous: true, fileId: fileInfo.id, priority: 1 }); | |
const file = toObject(fileResponse); | |
const probeData = await probe(file.local.path); | |
const duration = Math.floor(Number(probeData.format.duration)); | |
utimesSync(file.local.path, new Date(), new Date()); | |
const newFile = await airgram.api.uploadFile({ | |
priority: priority++, | |
file: { | |
_: 'inputFileLocal', | |
path: file.local.path | |
}, | |
fileType: { | |
_: 'fileTypeAudio' | |
} | |
}); | |
const editMediaResponse = await airgram.api.editMessageMedia({ | |
chatId, | |
messageId: message.id, | |
inputMessageContent: { | |
_: 'inputMessageAudio', | |
audio: { | |
_: 'inputFileId', | |
id: newFile.response.id | |
}, | |
duration, | |
title: title, | |
performer: performer, | |
caption: { | |
_: 'formattedText', | |
text: 'Fixed', | |
entities: [] | |
} | |
} | |
}); | |
console.log(`${performer} - ${title} is fixed`); | |
} | |
lastMessageId = messages[messages.length - 1].id; | |
} | |
} | |
processChat(CHAT_ID); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment