N
N
nonamess2021-01-20 20:56:58
Python
nonamess, 2021-01-20 20:56:58

How to make the bot save the value in a variable when sending a message?

Hello, please tell me how to do it when I send a message to the bot, for example "write hello, he wrote hello, sorry if I didn’t write clearly"

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import random
import time

vk_session = vk_api.VkApi(token='token')
longpoll = VkLongPoll(vk_session)
vk = vk_session.get_api()

def send_msg(peer_id: int, message: str, attachment: str = ""):
    return vk.messages.send(**locals(), random_id=0)

while True:
    try:
        for event in longpoll.listen():
            if event.type == VkEventType.MESSAGE_NEW:
                response = event.text.lower()
                if response == "пинг":
                   time.sleep(3)
                   send_msg(event.peer_id, "PONG")
    except Exception as e:
        print(repr(e))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Muntyan, 2021-01-20
@limontasher

And it’s really not clearly written, but I kind of assumed, look, in the response variable you store the text of the message from the user, if you want the bot to write what you tell him, you need to learn indexing, and if it’s quite simple

if response[:6] == "напиши":
                   time.sleep(3)
                   send_msg(event.peer_id, response[7:])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question