Answer the question
In order to leave comments, you need to log in
How to solve the error in Vk_Api?
I am making a VKontakte bot in python. When run, it gives an error:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\VK.py", line 37, in
write_msg(event.user_id, "I didn't understand your answer...")
File "C:\Users\Admin\Desktop\VK.py", line 5, in write_msg
vk.messages.send( #Sending message
AttributeError: 'VkApi' object has no attribute 'messages'
<code lang="python">
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
def write_msg(user_id, message):
vk.messages.send( #Отправляем сообщение
user_id=event.user_id,
random_id=event.random_id,
message='Тест')
# API-ключ созданный ранее
token = "secret_token"
# Авторизуемся как сообщество
vk = vk_api.VkApi(token=token)
# Работа с сообщениями
longpoll = VkLongPoll(vk)
# Основной цикл
for event in longpoll.listen():
# Если пришло новое сообщение
if event.type == VkEventType.MESSAGE_NEW:
# Если оно имеет метку для меня( то есть бота)
if event.to_me:
# Сообщение от пользователя
request = event.text
# Каменная логика ответа
if request == "привет":
write_msg(event.user_id, "Хай")
elif request == "пока":
write_msg(event.user_id, "Пока((")
else:
write_msg(event.user_id, "Не поняла вашего ответа...")
</code>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question