Analysis of the mistakes of last year's bot

    Hello. I want to make this article as an analysis of my mistakes in my past articles as a whole, they are relevant, but sometimes I, due to small experience, made mistakes.

    Functions


    Python is a functional programming language and when developing a bot I did not take this into account because of this, the article constantly repeats the same piece of code:

    if event.from_user:
        vk.messages.send( #Если написали в ЛС
            user_id=event.user_id,
            message='какой-то текст'
        )
    elif event.from_chat: #Если написали в беседе
        vk.messages.send(
        chat_id=event.chat_id,
        message='какой-то текст'

    It was more reasonable to make a function:

    defmessage(text,noLinks=0):if event.from_user: 
            vk.messages.send( 
                user_id=event.user_id,
                message=str(text),
                dont_parse_links = noLinks
    	)
        elif event.from_chat: 
            vk.messages.send( 
                chat_id=event.chat_id,
                message=str(text),
                dont_parse_links = noLinks
    	)

    And then call her

    message('какой-то текст')

    Double loops


    Here the approach is simple. Split a string using split (). You can leave the function empty, then the line is divided by spaces and characters for the crown translation or by another separator (for example, a comma). At the exit we get a list with which you can work. One crutch less.

    if event.text == 'Wikipedia' or event.text == 'Wikis' or event.text == <...>


    This quiet horror was made for a variety of phrases and struggle with the register.

    The register problem is solved with the help of the casefold () function, well, or lower () in Russian is not important. After that, the user can write at least: “ComAnd”, after processing, we get a “command”.

    It is also a good idea to bring words to the initial / undefined form. But here the standard functions can not do, will have to use pymorphy2.

    import pymorphy2
    textMsg = event.text.casefold() #приводим к нижнему регистру
    Msg = textMsg.split() #делим
    tmp = 0
    event.text = ''for items in Msg:
        p = morph.parse(str(Msg[tmp]))[0]
        Msg[tmp] = p.normal_form #приводим в нормальную форму
        event.text = str(event.text) +' '+ str(Msg[tmp]) #возвращаем в строку
        tmp += 1

    Now even if the user writes: “KOMANDA”, “ComOde” or “team” the output will be 'command' and terribly long “or event.text ==” removed

    Different answers


    Not long ago, Norazord asked me for help and asked:
    Sorry if I am bothering again, but how to make it so that the bot could respond with different phrases. For example, they write to him Hi he answers Hello wrote again Hi he already answers Good afternoon
    to inexperience I replied:
    Well, for example, make a variable and each time give it a random number (within the number of phrases) and then if. It will look something like this

    import random
    servis1=random.randint(1,3)
    if servis1==1:
        print('1')
    elif servis1==2:
        print('2')
    else:
        print('3')

    In general, the idea is correct, but instead of a heap of if-s I would do this:

    import random
    r = random.randint(1,3)
    answers = ['1','2','3']
    message(answers[r]) #с учётом ранее созданной функции

    or

    import random
    answers = ['1','2','3']
    message(random.choice(answers))

    Thank you Framework

    Also popular now: