T
T
tokyodead2021-07-13 11:31:10
MySQL
tokyodead, 2021-07-13 11:31:10

How to make some functions in telegrabotapi python?

Good afternoon, I'm a beginner, and I'm just learning python, I'm writing a telegram bot that, at the request of the user, will issue him a certificate that he is studying at the university, a certificate in pdf format.
1) When sending for the first time, the bot sends him a certificate, on the second attempt, an error appears that the database is closed.
2) There will be several references, and I don’t know how to store them and how the bot should understand that the user needs not the 1st reference, but the 2nd. Help is generated automatically using the fpdf library.
Here is the code, ask for shit code, I'm trying to write it on my own.

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
    item1 = types.KeyboardButton('Заказать справку')
    markup.add(item1)
    bot.send_message(message.chat.id, 'Вас приветствует бот myboy', reply_markup=markup)

@bot.message_handler(content_types=['text'])
def bot_message(message):
    if message.chat.type == 'private':
        if message.text == 'Заказать справку':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('Справка#1')
            back = types.KeyboardButton('Назад')
            markup.add(item1, back)
            bot.send_message(message.chat.id, 'Какая справка Вам нужна?', reply_markup=markup)

        elif message.text == 'Справка#1':
            bot.send_message(message.chat.id, 'Введите ИИН')

        elif message.text.isdigit and len(message.text) == 12:
            with con:
                cur = con.cursor()
                sql = "SELECT `fullname` FROM `bot_db` WHERE `iin`=%s and status=1 and user_type=1 limit 1 "
                cur.execute(sql, (message.text,))
                result = cur.fetchone()
                name = result
                age = '1997-01-08'
                page_name = str(name) + '.pdf'
                get_pdf(name,age, page_name)
                bot.send_message(message.chat.id, result)
                r = open('C:\\Users\\Alkon PC\\PycharmProjects\\mainpy\\{}'.format(page_name), 'rb')
                bot.send_document(message.chat.id, r)

        elif message.text == 'Назад':
            markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
            item1 = types.KeyboardButton('Заказать справку')
            markup.add(item1)
            bot.send_message(message.chat.id, 'Назад', reply_markup=markup)

bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-13
@Vindicar

1. "with con:" will close the connection upon exiting the with block.
2. Why store certificates at all if you generate them on the fly? Or will they be processed in some other way?
3. I would advise you to move everything related to working with the database and PDF generation into a separate class, and leave the bot as a bot, i.e. cart integration. This makes it easier to debug and maintain the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question