K
K
kirito12020-06-27 01:00:50
Python
kirito1, 2020-06-27 01:00:50

How to prefix a bot?

I want to set a prefix for my VK bot. Please tell me how to do it.

My code:

import vk_api
import time
import random
import config
 
token = "тут_токен"
 
vk = vk_api.VkApi(token=token)

password_length = 32

possible_characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

vk._auth_token()


while True:
    try:
        messages = vk.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
        if messages["count"] >= 1:
            id = messages["items"][0]["last_message"]["from_id"]
            body = messages["items"][0]["last_message"]["text"]
            if body.lower() == "Пароль" or body.lower() == "пароль":
                random_character_list = [random.choice(possible_characters) for i in range(password_length)]
                random_password = "".join(random_character_list)
                password = random_password
                vk.method("messages.send", {"peer_id": id, "message": password, "random_id": random.randint(1, 2147483647)})
            elif body.lower() == "Факты" or body.lower() == "факты":
                vk.method("messages.send", {"peer_id": id, "message": "А ты знал что? \n\n" + random.choice(config.facts), "random_id": random.randint(1, 2147483647)})
            elif body.lower() == "Факт" or body.lower() == "факт":
                vk.method("messages.send", {"peer_id": id, "message": "А ты знал что? \n\n" + random.choice(config.facts), "random_id": random.randint(1, 2147483647)})
    except Exception as E:
        time.sleep(1)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
truenotnamed, 2020-06-27
@kirito1

Designate a variable with a prefix: And then add a check if the message is a command (that is, it starts with a prefix):
prefix = "/"

if body.lower().startswith(prefix):
    # код с логикой бота

S
soremix, 2020-06-27
@SoreMix

What is a prefix? Lower case text will never be capitalized
body.lower() == "Пароль

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question