[id12345|Вася Пупкин]
def get_user_id(pattern: str) -> int:
if "[id" in pattern:
return int(pattern.split("|")[0].replace("[id", ""))
def get_user_id(pattern: str) -> int:
if "vk.com/" in pattern:
domen = pattern.split("/")[-1]
return vk.method("users.get", {"user_ids": domen})[0]["id"]
def get_name(uid: int) -> str:
data = vk.method("users.get", {"user_ids": uid})[0]
return "{} {}".format(data["first_name"], data["last_name"])
f"Приветствую [id{event.user_id}|{get_name(event.user_id)}]"
if event.object.text == "Кик" or event.object.text == "кик":
vk.method("messages.removeChatUser", {
"chat_id": event.object.peer_id - 2000000000,
"member_id": event.obj.reply_message['from_id']})
longpoll = VkBotLongPoll(vk_session, idgrup)
def get_user(pattern: str) -> int:
if "[id" in pattern:
return int(pattern.split("|")[0].replace("[id", ""))
if "vk.com/" in pattern:
domen = pattern.split("/")[-1]
return vk.users.get(user_ids=domen)[0]["id"]
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
text = event.obj["message"]["text"].split()
if (len(text) == 2) and text[0] == "кик":
try:
vk.messages.removeChatUser(
chat_id=event.chat_id,
member_id=get_user(text[1])
)
except Exception as e:
vk.messages.send(
chat_id=event.chat_id,
message="Произошла ошибка.",
random_id=0
)
def get_name(uid: int) -> str:
data = vk.method("users.get", {"user_ids": uid})[0]
return "{} {}".format(data["first_name"], data["last_name"])
full_name = get_name(тут юзер айди)
messages.getChat
недоступен.messages.getConversationsById
def get_chat_title(chat):
return vk.method("messages.getConversationsById", {"peer_ids": chat})['items'][0]['chat_settings']['title']
while True:
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
print('Название беседы: ' + get_chat_title(event.obj.peer_id))
except Exception as e:
print(repr(e))
if 'action' in event.raw['object']:
if 'chat_invite_user' or 'chat_invite_user_by_link' in event.raw['object']['action']['type']:
user_id = event.raw['object']['action']['member_id']
VkLongPoll, VkEventType
VkBotLongPoll, VkBotEventType
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
vk = vk_api.VkApi(token="ТОКЕН")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, АЙДИ ГРУППЫ)
def send_msg(peer_id: int, message: str, attachment: str = ""):
return vk.method("messages.send", {**locals(), "random_id": 0})
while True:
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.object.peer_id != event.object.from_id: #Для беседы
if event.object.text == "Привет":
send_msg(event.obj.peer_id, "Пока")
else:
send_msg(event.obj.peer_id, "Я не знаю такой команды")
if event.object.peer_id == event.object.from_id: #Для Лс
if event.object.text == "Привет":
send_msg(event.obj.peer_id, "Пока")
else:
send_msg(event.obj.peer_id, "Я не знаю такой команды")
except Exception as e:
print(repr(e))
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
vk = vk_api.VkApi(token="ТОКЕН")
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, АЙДИ ГРУППЫ)
def send_msg(peer_id: int, message: str, attachment: str = ""):
return vk.method("messages.send", {**locals(), "random_id": 0})
while True:
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.object.peer_id != event.object.from_id: #Для беседы
if event.object.text == "Привет":
send_msg(event.obj.peer_id, "Пока")
if event.object.peer_id == event.object.from_id: #Для Лс
if event.object.text == "Привет":
send_msg(event.obj.peer_id, "Пока")
except Exception as e:
print(repr(e))
if event.object.text == "Нами" or event.object.text == "нами":
photo_send = random.randint(31, 33)
vk.method("messages.send",
{
"chat_id": event.object.peer_id - 2000000000,
"attachment": "photo-193643776_4572390" + f'{photo_send}',
"random_id": random.randint(1, 2147483647)
})
text_1 = event.obj['text'].split(' ', maxsplit=1)
if text_1[0] == "Выбери" or text_1[0] == "выбери":
text_2 = text_1[1].split('или')
list = [text_2[0], text_2[1]]
change = random.choice(list)
vk.method("messages.send",
{
"chat_id": event.object.peer_id - 2000000000,
"message": "Я выбираю " + f'{change}',
"random_id": random.randint(1, 2147483647)
})