P
P
PegasusPega2020-07-05 11:53:28
API
PegasusPega, 2020-07-05 11:53:28

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

2 answer(s)
A
Alexander Semin, 2020-11-17
@PegasusPega

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

1 way

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);
  });
}

Первая функция checkConversationID проверяет существование чата, а вторая функция выполняет подсчёт чатов.
Вторая функция зависит от первой, поэтому лучше не удалять ничего :)
В функцию getLastConversation передаётся один параметр в котором содержится инстанс от vk-io
Пример:
const { VK } = require(`vk-io`);

(async function () {
  const vk = new VK({
    token:
      "token",
  });
  console.log(await getLastConversation(vk));
})();

Данный код выведет идентификатор последней беседы в боте.

2 way (shorter)

Вы можете использовать мой модуль для сокращения кода
Пример:
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));
})();

S
Saraman, 2020-07-05
@Saraman

Why not use https://vk.com/dev/messages.getConversations from a group token?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question