G
G
Georgy Saltykov2021-08-15 18:12:48
Python
Georgy Saltykov, 2021-08-15 18:12:48

How to implement the creation of an application by a user in a VK Python bot?

The essence of the bot is as follows: the user creates an application, consistently answering the questions that the bot asks. Then the application is sent to the moderator for consideration, and as a result, the user receives an answer (so far this has not been implemented). To implement all this, a function was created that creates this application. The question is: are there other ways to implement the filling of the application, or is my code completely useless.
Ps Do not judge strictly this is my first project in terms of programming. I would like real advice and adequate criticism. Thanks in advance)

import vk_api
from vk_api import keyboard
from vk_api.longpoll import VkLongPoll, VkEventType 
from vk_api import VkUpload
from vk_api.utils import get_random_id
from vk_api.keyboard import VkKeyboard, VkKeyboardColor 

# \\\КЛАВИАТУРЫ
keyboard1 = VkKeyboard(one_time=True)
keyboard1.add_button('Создать заявку', color=VkKeyboardColor.POSITIVE)

keyboard2 = VkKeyboard(one_time=False)
keyboard2.add_button('Назад', color=VkKeyboardColor.SECONDARY)
keyboard2.add_button('Отменить', color=VkKeyboardColor.NEGATIVE)

keyboard3 = VkKeyboard(one_time=True)
keyboard3.add_button('Сохранить', color=VkKeyboardColor.POSITIVE)
keyboard3.add_line()
keyboard3.add_button('Назад', color=VkKeyboardColor.SECONDARY)
keyboard3.add_button('Отменить', color=VkKeyboardColor.NEGATIVE)

# \\\ФУНКЦИЯ ОТПРАВКИ СООБЩЕНИЯ
def write_message (sender, message):
    if i==1: 
        authorize.method('messages.send', {'user_id': sender, 'message':  message, 'random_id': get_random_id(), 'keyboard': keyboard1.get_keyboard()})
    elif i==2:
        authorize.method('messages.send', {'user_id': sender, 'message':  message, 'random_id': get_random_id(), 'keyboard': keyboard2.get_keyboard()})
    elif i==3:
        authorize.method('messages.send', {'user_id': sender, 'message':  message, 'random_id': get_random_id(), 'keyboard': keyboard3.get_keyboard()})

# \\\ФУНКЦИЯ СОЗДАНИЯ АНКЕТЫ 
def reg():
    global i
    questionnaire = []
    for event in longpoll.listen():  
        if event.type == VkEventType.MESSAGE_NEW and event.to_me:
            name_user = event.text
            sender = event.user_id
            if name_user == event.text and name_user != "Назад" and name_user != "Отменить":    
                write_message(sender, "Вы ввели имя: "+ name_user + "\nВведите Ваш возраст")
                questionnaire.append(name_user)
                for event in longpoll.listen():  
                    if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                        year_user = event.text
                        sender = event.user_id
                        if year_user == event.text and year_user != "Назад" and year_user != 'Отменить': 
                            write_message(sender, "Вам "+ year_user + " лет\n С какого Вы города?")
                            questionnaire.append(year_user)
                            for event in longpoll.listen():  
                                if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                                    city = event.text
                                    sender = event.user_id
                                    if city == event.text and city != "Назад" and city != "Отменить":
                                        write_message(sender, "Вы с г. "+ city + "\n" + "Заполните заявку, укажите все нужные Вам критерии")
                                        questionnaire.append(city)
                                        for event in longpoll.listen():
                                            if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                                                text_user = event.text
                                                sender = event.user_id
                                                if text_user == event.text and text_user != "Назад" and text_user != "Отменить":
                                                    i=3
                                                    write_message(sender, "Ваша заявка: \n"+ name_user + " " + year_user + " " + city + "\n" + text_user)
                                                    questionnaire.append(text_user)
                                                    for event in longpoll.listen():
                                                        if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                                                            end_user = event.text
                                                            sender = event.user_id
                                                            if end_user =="Сохранить" and end_user != "Назад" and end_user != "Отменить":
                                                                i=1
                                                                write_message(sender, "Вы успешно создали заявку")
                                                                return questionnaire
                                                            elif end_user =="Назад":
                                                                i=2
                                                                write_message(sender, "Измените текст заявки")
                                                                break
                                                            elif end_user =="Отменить":
                                                                i=1
                                                                write_message(sender, "Заявка отменена")
                                                                questionnaire = []
                                                                return questionnaire
                                                elif text_user == "Назад":
                                                    write_message(sender, "Измени свой город")
                                                    break
                                                elif text_user == "Отменить":
                                                    i=1
                                                    text_user = None
                                                    questionnaire = []
                                                    write_message(sender, "Заявка отменена")
                                                    return questionnaire
                                    elif city == "Назад":
                                        write_message(sender, "Измени свой возраст")
                                        break
                                    elif city == "Отменить":
                                        i=1
                                        city = None
                                        questionnaire = []
                                        write_message(sender, "Заявка отменена")
                                        return questionnaire
                        elif year_user == "Назад":
                            write_message(sender, "Введи имя заново")
                            break
                        elif year_user == "Отменить":
                            i=1
                            year_user = None
                            questionnaire = []
                            write_message(sender, "Заявка отменена")
                            return questionnaire
            elif name_user == "Назад":
                i=1
                name_user = None
                questionnaire = []
                write_message(sender, "Заявка отменена")
                return questionnaire
            elif name_user == "Отменить":
                i=1
                name_user = None
                questionnaire = []
                write_message(sender, "Заявка отменена")
                return questionnaire

# \\\АВТОРИЗАЦИЯ            
token = 'TOKEN'
authorize = vk_api.VkApi(token = token) 
longpoll = VkLongPoll(authorize)

# \\\ОСНОВНАЯ ЛОГИКА
for event in longpoll.listen():  
    if event.type == VkEventType.MESSAGE_NEW and event.to_me:
        reseived_message = event.text
        sender = event.user_id
        if reseived_message == "Создать заявку":
            i=2
            write_message(sender, "Как тебя зовут?")
            reg()
        elif reseived_message == event.text:
            i=1
            write_message(sender, "Напиши 'Создать заявку'")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-08-15
@SoreMix

I would like real advice and adequate criticism

Why so many longpoll.listen?
In the middle of a dialogue with one user, write on behalf of another in the group's PM.
Global variables, especially in a multi-user application, are bad
First reseived_message = event.text, then elif reseived_message == event.text. What for?
The keyboards are stretched IMHO
The naming of variables is strange. name_user != "Назад"Why is the name checked for "Back"?. There are no such names, or the variable may contain something else instead of a name.
Useless check, the condition will always be fulfilled.
text_user = event.text
if text_user == event.text

Use for database. As far as I remember, there is no FSM support in vk_api, so by hand.

H
HemulGM, 2021-08-16
@HemulGM

You have problems with logic. Everything will go down the vulva in an instant as soon as the second person writes. And if there is more, then everyone will have porridge.
For such chatbots, a database is used. In which the state of each user is stored and for each individual user the conditions will be checked differently depending on the last state (current step).
As soon as someone writes. Go to the database, get the state of the current user (current step) and react to his message regarding the current step. And so for each message.
Criticism:
You have a terrible, incomprehensible code. A bunch of nested conditions. Magic lines, magic numbers. The variable city is compared to the string "Back". How can this be logically justified?
Global variables, erroneous use of the bot principle: longpoll.listen() is a method for one global loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question