S
S
Sasha Bower2019-12-17 15:59:40
Python
Sasha Bower, 2019-12-17 15:59:40

I made a bot to work in a conversation. But he responds to absolutely any message. What to do?

from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import random

vk = vk_api.VkApi(token="token")

vk._auth_token()

vk.get_api()

longpoll = VkBotLongPoll(vk, vk group id)

while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            if event.object.peer_id != event.object.from_id:
                if event.object.text.lower() == "привет бот" or "Привет бот" or "эй бот" or "Эй бот":
                    vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "хай",
                                                "random_id": 0 })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Shokhrov, 2019-12-17
@Darkcan

Make the right condition.

if event.object.text.lower() == "привет бот" or "Привет бот" or "эй бот" or "Эй бот":
reads "if the message is "hello bot" or always".
The operator ordoes not list the value options to be compared with, but the condition options (which, in this case, should be comparisons of the result event.object.text.lower()with different strings.
Also, since lower() lowers the text, it does not make sense to compare with strings containing large letters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question