J
J
Just1n2018-09-09 19:42:06
Python
Just1n, 2018-09-09 19:42:06

How to set a command for a VKontakte bot?

I want to set a command to the bot so that if a person writes a certain word, the bot writes an answer to him. Help how to do it? The code looks like this:

from flask import Flask, request, json
import vk

app = Flask(__name__)

@app.route('/', methods=['POST'])
def processing():
    data = json.loads(request.data)
    if 'type' not in data.keys():
        return 'not vk'
    if data['type'] == 'confirmation':
        return 'мой токен'
    elif data['type'] == 'message_new':
        session = vk.Session()
        api = vk.API(session, v=5.84)
        peer_id = data['object']['peer_id']
        api.messages.send(access_token='мой токен', peer_id=(peer_id), message='Привет, я бот, нахожусь в разработке')
        return 'ok'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2018-09-09
@trofimovdev

You need to take a message from the user (look at the data structure) and extract the command from there:

message = data[...]
if message == '/start': # все сообщение
    print('Стартуем!')
elif message.split(' ')[0] == '/end': # первое слово до пробела
    print('Пока, пока.')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question