0% found this document useful (0 votes)
20 views5 pages

J

This document outlines functions for processing messages from Telegram groups and channels. It retrieves message data including text, attachments, mentions, and buttons. The functions analyze message content to detect languages, extract linked groups/channels, and add relevant groups to a processing queue. The goal is to gather messaging data from Telegram public and private communities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

J

This document outlines functions for processing messages from Telegram groups and channels. It retrieves message data including text, attachments, mentions, and buttons. The functions analyze message content to detect languages, extract linked groups/channels, and add relevant groups to a processing queue. The goal is to gather messaging data from Telegram public and private communities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

taskFunction

/* let privateId = null


let publicId = null
let info;

if (isPrivate) {

const privateChat = await this.getPrivateChat(username);

if (!privateChat) {
await this.deleteChat({ invite_id: username })

// se o convite expirar tire o scheduler

return null;
}

info = await tme.getUsername(`+${username}`)

if (!privateChat?.chat?.id) {
let is_group = !privateChat.gigagroup && !privateChat.megagroup
&& (privateChat.broadcast || privateChat.channel) ? false : true

let data = {
title: privateChat?.title || info.title,
description: privateChat?.about || info?.description,
username: null,
members: privateChat?.participantsCount || info?.members ||
null,
language: null, // verificar a linguagem por titulo e
descricao depois
invite_id: username,
link: `https://t.me/+${username}`,
status: true,
is_public: false,
is_group,
view_messages: false,
allow_extraction: !is_group ? false : null,
is_preview_info: false
}

let language = new Language()

await language.start()

language.detectLang(data.title)
language.detectLang(data.description)

data.language = language.mainLanguage

const chatId = await this.createOrUpdateChat(data);

logger.info({ msg: `Canal/Grupo privado sem visualização`, data


})

// se for um canal privado que não pode ver as mensagens defina


um repeat a cada 2 semanas

//await addQueue({ id: chatId, ...data }, 1000 * 60 * 60 * 24 *


15) // A cada 15 dias

return data;
}

privateId = new Api.InputPeerChannel({ channelId:


privateChat.chat.id, accessHash: privateChat.chat.accessHash })
} else {
info = await tme.getUsername(username)

if (!info) {
await this.deleteChat({ username })

return null
}

publicId = await this.getPublicId(username)

if (!publicId)
return null
}

const fullInfo = await this.getFullChat(publicId, privateId)

const is_group = !fullInfo ? info.isGroup : !


fullInfo.chats[0].gigagroup && !fullInfo.chats[0].megagroup &&
(fullInfo.chats[0].broadcast || fullInfo.chats[0].channel) ? false : true

const data = {
id_telegram: this.parseId(fullInfo?.fullChat?.id) || null,
title: fullInfo?.chats[0]?.title || info?.title || null,
description: fullInfo?.fullChat?.about || info?.description ||
null,
username: isPrivate ? null : (!fullInfo ? username :
fullInfo?.chats[0]?.username || ("usernames" in fullInfo?.chats[0] ?
(fullInfo?.chats[0]?.usernames[0] || { username: null }).username : null)),
members: fullInfo?.fullChat?.participantsCount || info?.members,
language: null, // verificar a linguagem depois
invite_id: isPrivate ? username : null,
link: isPrivate ? `https://t.me/+${username}` : null,
view_messages: true,
status: true,
is_public: !isPrivate,
is_group,
allow_extraction: !is_group ? false : (!fullInfo ? null :
(fullInfo?.fullChat?.participantsHidden ? false : true)),
}

const chatId = await this.createOrUpdateChat(data);

logger.info({ msg: `Canal/grupo publico ou privado (com visualização)`,


data: { id: chatId, ...data } })

let language = new Language()

await language.start()

language.detectLang(data.title)
language.detectLang(data.description)
//await addQueue({ id: chatId, ...data }, 1000 * 60 * 60 * 24) //Todo
santo dia

let offsetId = undefined


let totalOfMessages = 0;
let countRead = 0

do {
logger.info(`${data.title} - ${data.username ? `@$
{data.username}` : data.invite_id} | Ids: ${chatId} - ${data.id_telegram} | $
{countRead}/${totalOfMessages}`)
const msgs = await this.getMessages(publicId, privateId, offsetId,
random(90, 80))

totalOfMessages = totalOfMessages || msgs.count


countRead += msgs.messages.length
offsetId = msgs.messages[msgs.messages.length - 1].id

const msgLimit = pLimit(30)


const limitEntity = pLimit(4)

await Promise.allSettled(
msgs.messages.map(msg =>
msgLimit(
async () => {
try {
if (!msg?.message)
return;

let msg_data = {
chat_id: chatId,
id_telegram: msg?.id,
user_id: msg?.fromId?.userId ?
msg.fromId.userId.toString() : null,
message: msg?.message || null,
forwards: msg?.forwards || null,
views: msg?.views || null,
date: msg?.date ?
moment.unix(msg.date).format("YYYY-MM-DD HH:mm:ss") : null,
edit_date: msg?.editDate ?
moment.unix(msg.editDate).format("YYYY-MM-DD HH:mm:ss") : null
}

await this.createMessage(msg_data)

language.detectLang(msg_data.message)

let buttonsPromises = []

for (const buttonsRow of


(msg?.replyMarkup?.rows || [])) {
for (const button of (buttonsRow?.buttons
|| [])) {
if (!button?.url)
continue;

const link = button.url


const url = new
URL(link.startsWith("http") ? link : `https://${link}`);

if (url.hostname != "t.me" &&


url.hostname != "telegram.me")
continue;

let chat =
this.getUsername(url.pathname)

buttonsPromises.push(limitEntity(async
() => await this.addChatToQueue(chat, { id: chatId, ...data })))
}
}

await Promise.allSettled(
[
...(msg?.entities || []).map(entity =>
limitEntity(async () => {
try {
if (entity.className !=
'MessageEntityMention' && entity.className != 'MessageEntityUrl' &&
entity.className != 'MessageEntityTextUrl')
return;

let chat;

if (entity.className !=
'MessageEntityMention') {
let link = entity?.url
|| this.parseEntity(msg.message, entity.offset, entity.length)
const url = new
URL(link.startsWith("http") ? link : `https://${link}`);

if (url.hostname !=
"t.me" && url.hostname != "telegram.me")
return;

chat =
this.getUsername(url.pathname)
} else {
chat = {
isPrivate: false,
username:
this.parseEntity(msg.message, entity.offset, entity.length).replace("@",
"").toLowerCase()
}
}

await
this.addChatToQueue(chat, { id: chatId, ...data })
} catch (error) {
logger.error(error)
}
})
),
...buttonsPromises
]
)
} catch (error) {
logger.error(error)
}
}
)
)
)

await delay(250)
} while (countRead < totalOfMessages)

await knex("chats").where({ id: chatId }).update({ language:


language.mainLanguage || null })

return { id: chatId, ...data } */

You might also like