Answer the question
In order to leave comments, you need to log in
How to allow the bot to execute commands only from a specific user?
Is it possible to make the bot respond to this command only if it is written by someone from the administration of the conversation?
# -*- coding: utf8 -*-
from vk_api import VkApi
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.longpoll import VkLongPoll, VkEventType, VkChatEventType
vk_session = VkApi(token='токен') #токен сообщества
vk = vk_session.get_api()
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
message = event.obj['message']
peer_id = message['peer_id']
from_id = message['from_id']
textl = message['text']
if textl.lower() == 'ку': #команда которую нужно принять
try:
vk.messages.send(
peer_id=peer_id,
message='ку', #ответ бота
random_id=get_random_id(),
)
except Exception as e:
print ("Сообщение не отправленно")
Answer the question
In order to leave comments, you need to log in
def get_members(peer_id: int) -> list:
return vk.method("messages.getConversationMembers", {
"peer_id": peer_id
})["items"]
def get_admins(peer_id: int) -> list:
return [
i["member_id"] for i in get_members(peer_id)
if i.get("is_admin", False)
]
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.text == "какая-то команда":
if event.obj.from_id in get_admins(event.obj.peer_id):
#выполнение команды
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question