Answer the question
In order to leave comments, you need to log in
How do you count conversations that include the exact group that counts them?
How to count conversations in which there is a VK group that counts them?
Answer the question
In order to leave comments, you need to log in
VK does not allow you to get a list of conversations in which a group is present, with the exception of those conversations that the group itself created, however, you can get the total number of conversations using these 2 methods
async function checkConversationID(instanceVK, peerID) {
return instanceVK.api.messages
.getConversationsById({
peer_ids: peerID,
})
.then(({ items }) => {
const [data] = items;
return !!data.peer.id;
})
.catch((error) => {
return !error.code === 927;
});
}
async function getLastConversation(instanceVK) {
return new Promise(async (resolve) => {
let maxConversationID = 2147483647;
let minConversationID = 2000000001;
let currentConversationID = maxConversationID;
let status = false;
while (!status) {
if (!(await checkConversationID(instanceVK, currentConversationID))) {
maxConversationID = currentConversationID;
currentConversationID = Math.round(
(currentConversationID + minConversationID) / 2,
);
} else {
if (maxConversationID !== currentConversationID) {
while (!status) {
if (minConversationID + 10 > maxConversationID) {
for (let i = minConversationID; i < maxConversationID; i++) {
if (!(await checkConversationID(instanceVK, i))) {
status = true;
currentConversationID = i - 1;
resolve(currentConversationID);
}
}
}
currentConversationID = Math.round(
(minConversationID + maxConversationID) / 2,
);
if (
!(await checkConversationID(instanceVK, currentConversationID))
) {
maxConversationID = currentConversationID;
currentConversationID = Math.round(
(currentConversationID + minConversationID) / 2,
);
} else {
minConversationID = currentConversationID;
}
}
} else {
status = true;
resolve(currentConversationID);
}
}
}
resolve(currentConversationID);
});
}
const { VK } = require(`vk-io`);
(async function () {
const vk = new VK({
token:
"token",
});
console.log(await getLastConversation(vk));
})();
const { VK } = require(`vk-io`);
const utils = require(`rus-anonym-utils`);
(async function () {
const vk = new VK({
token:
"token",
});
console.log(await utils.vk.group.getLastConversation(vk));
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question