Answer the question
In order to leave comments, you need to log in
How to fix vk api error?
writes the following error Traceback (most recent call last):
File "/home/aboaus/bot/bot", line 3, in
import vk_api
ModuleNotFoundError: No module named 'vk_api'
Here is the code if that
import random
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from commander.commander import Commander
def write_msg(user_id, message):
vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random.randint(0, 2048)})
# API key created earlier
token = "Your API token here..."
# Authorize as community
vk = vk_api.VkApi(token=token)
# Working with messages
longpoll = VkLongPoll(vk)
# Commander
commander = Commander()
print("Bot is running")
# Main loop
for event in longpoll.listen():
# If a new message has arrived
if event.type == VkEventType.MESSAGE_NEW:
# If it has a label for me( then there is a bot)
if event.to_me:
# Message from the user
request = event.text
# Stone response logic
if request == "hello":
write_msg(event.user_id, "Hi")
elif request == "bye":
write_msg(event .user_id, "Bye((")
elif request.split()[0] == "command":
write_msg(event.user_id, commander.do(request[8::]))
else:
write_msg(event.user_id, "I didn't understand your answer...")
The script is hosted by pythonanywhere if anything.
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