def get_user(pattern: str) -> int:
if "vk.com/" in pattern:
domen = pattern.split("/")[-1]
print(domen)
return vk.users.get(user_ids=domen, fields=0, name_case=0)["id"]
while True:
try:
for event in longpoll.listen():
if event.type == VkEventType.MESSAGE_NEW:
text = event.text.split(' ')
if text[0] == "Вернуть" or text[0] == "вернуть":
vk.messages.addChatUser(
chat_id=event.chat_id,
user_id=get_user(text[1]),
visible_messages_count=250
)
except Exception as e:
print(repr(e))
pip install vk_api
pip install yandex_translate
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api.bot_longpoll import VkBotEventType
from yandex_translate import YandexTranslate
group_id = айди группы
token = "токен"
translate = YandexTranslate('токен')
vk = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk, group_id)
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, 'На какой язык? Указывать двумя буквами.\n Например: Русский - ru, Английский - en')
flag = 0
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
trTo = event.object.text
send_msg(event.obj.peer_id, 'Введите фразу, которую надо перевести ')
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
trNormal = 1
try:
trFrom = translate.detect(event.object.text)
trResult = translate.translate(event.object.text, trFrom + '-' + trTo)
except Exception as e:
trNormal = 0
print("Exception:", e)
pass
if trNormal == 1:
send_msg(event.obj.peer_id, 'Перевод:\n' + str(trResult['text']))
flag = 1
break
if trNormal == 0:
send_msg(event.obj.peer_id, 'Неправильно введён язык')
flag = 1
break
flag = 1
break
if flag == 1:
break
except Exception as e:
print(repr(e))