I
I
Innaro2022-02-16 10:29:31
Python
Innaro, 2022-02-16 10:29:31

Dictionary in Python is not cleared, how can I solve this?

Tell me, there is a code for the tg bot, and after one execution, the code goes into
'str' object is not callable620ca7b2eee5a194828623.png

doc = DocxTemplate("Eidos.docx")    
record_dict = {};

# Функция, обрабатывающая команду /start
# Команда start
       
@bot.message_handler(commands=["start"])
def start(m, res=False):
        bot.send_message(m.chat.id, 'Я на связи. Напиши мне что-нибудь )')

# Функция, обрабатывающая команду /pass
# Команда pass

@bot.message_handler(commands=["pass"])
def passn(message, res=False):
    print(record_dict)
    record_dict.clear() 
    path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'ЗАЯВКА НА ВЪЕЗД НА склад.docx')
    if os.path.isfile(path) == True:
        os.remove(path)
    else:
        print("Файла нет, идем дальше")
    bot.send_message(message.chat.id, 'Марка Автомобиля? ');
    print(record_dict)
    bot.register_next_step_handler(message, brand)

# Функция, обрабатывающая команду /send
# Команда send

@bot.message_handler(commands=["send"])
#Сохранием файл
def nexting(message, res=False):
    record_dict = {'Brand' : brand, 'Number' : number, 'ArrivalDate' : arrivaldate}
    print(record_dict)
    doc.render(record_dict)
    doc.save("ЗАЯВКА НА ВЪЕЗД НА склад.docx")
    record_dict.clear()
    
@bot.message_handler(content_types=["text"])
def brand(message): #получаем марку Автомобиля
    global brand;
    brand = message.text; 
    record_dict['Brand'] = brand
    print(record_dict)
    bot.send_message(message.chat.id, 'Номера Автомобиля? ');
    bot.register_next_step_handler(message, number);
    
def number(message):
    global number; #получаем номер Автомобиля
    number = message.text;
    record_dict['Number'] = number
    print(record_dict)
    bot.send_message(message.chat.id, 'Какого числа должен заехать? ');
    bot.register_next_step_handler(message, arrivaldate);        

def arrivaldate(message):
    global arrivaldate; #получаем дату въезда
    arrivaldate = message.text;
    record_dict['Arrivaldate'] = arrivaldate
    print(record_dict)

    
# Запускаем бота
while True:
    try:
        bot.polling(none_stop=True,interval=0)
    except Exception as e:
        print(e)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2022-02-16
@Innaro

You named a function and a string variable with the same name.
before brand = message.text, brand was the name of a function, and now it has become the name of some string. And the second time you try to call the brand function , the interpreter swears that this is a string and cannot be called.
And the same thing happens with the rest of the functions. Give them other names.
And for the future, indicate where the error occurred.
PS. I would also advise not to use the global record_dict variable. If 2 people leave applications through your bot at the same time, then everything will get confused and fall into an error. Better pass user-entered values ​​through register_next_step_handler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question