Answer the question
In order to leave comments, you need to log in
The bot code in VK does not work, why?
The code does not work, when I run it, it does not send anything in response to the message. Messages included in the community, bots too.
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from config import bottoken2
session = vk_api.VkApi(token=bottoken2)
def send_message(user_id, message):
session.method("messages.send", {
'user_id': user_id,
'message': message,
'random_id': 0
})
for event in VkLongPoll(session).listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
text = event.text.lower()
user_id = event.user_id
if text == 'Привет':
send_message(user_id, 'Привет')
elif text == 'Начать':
send_message(user_id, 'Ну начал ты, и что дальше?')
Answer the question
In order to leave comments, you need to log in
I figured out what the problem is, I wrote text = event.text.lower() and then checked the text for capital letters if text == 'Hi':
Corrected the code
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from config import bottoken2
session = vk_api.VkApi(token=bottoken2)
def send_message(user_id, message):
session.method("messages.send", {
'user_id': user_id,
'message': message,
'random_id': 0
})
for event in VkLongPoll(session).listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me:
text = event.text
user_id = event.user_id
if text == 'Привет':
send_message(user_id, 'Привет')
elif text == 'Начать':
send_message(user_id, 'Ну начал ты, и что дальше?')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question