M
M
MIKHAN_GO2021-05-23 07:42:47
Python
MIKHAN_GO, 2021-05-23 07:42:47

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

1 answer(s)
M
MIKHAN_GO, 2021-05-23
@MIKHAN_GO

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 question

Ask a Question

731 491 924 answers to any question