How to display a list of conversation administrators? I write in python with vk api library

Question:

I encountered such a problem that very little is said about vk api python chat bots on the Internet.

I want to display a list of Administrators/Creator, how can I do that? I write in python library vk api

Thanks in advance) Here is the code in which this function is not yet:

from random import random
from vk_api import VkApi
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.longpoll import VkLongPoll, VkEventType


vk_session = VkApi(token="**********************")
longpoll = VkBotLongPoll(vk_session, "**********")
vk = vk_session.get_api()

def photo(id, url):
    vk.messages.send(user_id = id, attachment = url,random_id=0) 

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat:
        
            
        if event.obj.text=='!команды':
               random_id = round(random() * 10 ** 9)
               chat_id = int(event.chat_id)
               message = "да ты ***** нет у меня команд!"
               vk.messages.send(random_id=random_id,
chat_id=chat_id,
message=message)
    

Answer:

You can solve this problem using the groups.getMembers method.

We need to pass the filter parameter with the value managers

managers – Only community leaders will be returned (available when requested by passing an access_token on behalf of a community administrator).

We can do it like this: (instead of event.group_id, you can immediately enter the id of your community)

vk.groups.getMembers(group_id=event.group_id, filter='managers')

Or:

vk.method('groups.getMembers', {'group_id': event.group_id, 'filter': 'managers'})

The result of this query will be something like this:

{'count': 6, 'items': [{'id': 123456789, 'role': 'editor'}, 
                       {'id': 234567891, 'role': 'administrator', 'permissions': ['ads']},
                       {'id': 345678912, 'role': 'moderator'}, 
                       {'id': 456789123, 'role': 'creator', 'permissions': ['ads']}, 
                       {'id': 567891234, 'role': 'administrator', 'permissions': ['ads']}, 
                       {'id': 678912345, 'role': 'administrator', 'permissions': ['ads']}]}

Here is the code for your specific task if you only want to return the admins/owner id:

administrators = [] 

for manager in session.groups.getMembers(group_id=event.group_id, filter='managers')['items']:
    if manager['role'] in ('administrator', 'creator'): 
        administrators.append(manager['id'])
Scroll to Top