Y
Y
y_o_l_k_i_n___e_g_o_r2021-02-13 13:06:29
Python
y_o_l_k_i_n___e_g_o_r, 2021-02-13 13:06:29

python vk_api code not working?

There is a code:

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

vk_sess = vk_api.VkApi(token='')
api_sess = vk_sess.get_api()
longpool = VkBotLongPoll(vk_sess, group_id)


def send(text):
    api_sess.method('message.send', {'chat_id': event.chat_id, 'message': text, 'random_id': 0})


for event in longpool.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
        msg = event.text.lower()

        if msg == 'привет':
            send('Мяу-привет')


But it doesn't work.
The bot doesn't send messages at all!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alex K, 2021-02-13
@alexk111

The "message.send" method is called. Should be "messages.send"

P
Puma Thailand, 2021-02-13
@opium

So you look at the logs or debugging

Y
ya_ne_znau, 2021-02-13
@ya_ne_znau

def send(text):

'chat_id': event.chat_id

pass in event parameters

A
Andrey, 2021-02-13
@anerev

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.utils import get_random_id

vk_session = vk_api.VkApi(token='your_group_token')

vk = vk_session.get_api()

longpoll = VkBotLongPoll(vk_session, 'your_group_id')

for event in longpoll.listen():
    msg = event.text.lower()
    if msg == 'привет':
        if event.type == VkBotEventType.MESSAGE_NEW:
            vk.messages.send(
                        user_id=event.chat_id,
                        random_id=get_random_id(),
                        message='Мяу-привет'
                    )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question