Last active
August 19, 2024 11:21
-
-
Save iwonz/8eb35c5b6bacb74b59f355ce36a62dc4 to your computer and use it in GitHub Desktop.
Create folders with user from specified friends lists in messenger on VK
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 CONFIG = [ | |
{ folderName: '🤝', listIds: [1] }, | |
{ folderName: '🏉', listIds: [2, 3, 4, 5, 6] }, | |
{ folderName: '🐌', listIds: [7] }, | |
]; | |
const getFolder = async (name) => { | |
let folders = await vkApi.api('messages.getFolders', { with_peers: 1 }); | |
let folder = folders.items.find((f) => f.name === name); | |
if (folder) { | |
return folder; | |
} | |
await vkApi.api('messages.createFolder', { | |
name, | |
}); | |
folders = await vkApi.api('messages.getFolders', { with_peers: 1 }); | |
folder = folders.items.find((f) => f.name === name); | |
return folder; | |
}; | |
const main = async () => { | |
for await (const cfg of CONFIG) { | |
let folder = await getFolder(cfg.folderName); | |
const ids = (await Promise.all( | |
cfg.listIds.map((listId) => vkApi.api('friends.get', { list_id: listId })), | |
)).reduce((ids, list) => { | |
ids.push(...list.items); | |
return ids; | |
}, []); | |
if (folder.included_peer_ids.length) { | |
await vkApi.api('messages.updateFolder', { | |
folder_id: folder.id, | |
add_included_peer_ids: [], | |
remove_included_peer_ids: folder.included_peer_ids, | |
}); | |
} | |
await vkApi.api('messages.updateFolder', { | |
folder_id: folder.id, | |
add_included_peer_ids: ids, | |
remove_included_peer_ids: [], | |
}); | |
console.log(`${cfg.folderName} created with users from lists: ${cfg.listIds}. Total users: ${ids.length}.`); | |
} | |
}; | |
await main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment