0

I have a question. I am making a telegram bot that writes debts to a database. But I don't know how to make it possible to call the Vivod method by pressing the 2 button. And I also wanted to immediately ask if my code is at least a little bit correct?

import telebot
from telebot import types
import sqlite3

token = telebot.TeleBot('-------------------------')
chelZapis = None


@token.message_handler(commands=['start'])
def start(mainParam):
    conn = sqlite3.connect('dbOplat.sql')
    cur = conn.cursor()
    cur.execute('CREATE TABLE IF NOT EXISTS baza (id int auto_increment primary key, nikChela varchar(50), money int)')
    conn.commit()
    cur.close()
    conn.close()

    bts = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
    ButProdazha = types.KeyboardButton("Запись", token.register_next_step_handler(mainParam, prodazha))
    ButPokupka = types.KeyboardButton("Чекнуть долги", token.register_next_step_handler(mainParam, vivod))
    bts.add(ButProdazha, ButPokupka)
    token.send_message(mainParam.chat.id, 'Привет, залупа', reply_markup=bts)


def prodazha(message):
    token.send_message(message.chat.id, 'Ник чела:')
    token.register_next_step_handler(message, zapNik)

def zapNik(message):
    global chelZapis
    chelZapis = message.text.strip()
    token.send_message(message.chat.id, 'Сколько должен?')
    token.register_next_step_handler(message, dengi)

def dengi(message):
    dolg = message.text.strip()

    conn = sqlite3.connect('dbOplat.sql')
    cur = conn.cursor()
    cur.execute('INSERT INTO baza (nikChela, money) VALUES ("%s", "%s")' % (chelZapis, dolg))
    conn.commit()
    cur.close()
    conn.close()
    token.send_message(message.chat.id, 'Запись сделала успешно')



@token.callback_query_handler(func=lambda call: True)
def vivod(viv):
    conn = sqlite3.connect('dbOplat.sql')
    cur = conn.cursor()
    cur.execute('SELECT * FROM baza')
    a = cur.fetchall()

    info = ''
    for i in a:
        info += f'{i[1]}, {i[2]}\n'

    cur.close()
    conn.close()
    token.send_message(viv.message.chat.id, info)


token.polling(none_stop=True)

And if it’s not difficult, please tell me, I want to make a functional where, by clicking on an additional button, I write the name of a person and +500 or -500, the program would search for a person and add a debt to him or subtract, looking for him in the database

1 Answers1

0

You don't need to register callback_query_handler for vivod, because register_next_step_handler implicitly does something like that automatically here:

ButPokupka = types.KeyboardButton("Чекнуть долги", token.register_next_step_handler(mainParam, vivod))

Also, in dengi you use chelZapis which is not defined inside this function, but inside the previous one - zapNik. It will cause an error. To avoid it you can pass additional arguments to the next function when using register_next_step_handler. For example:

def zapNik(message):
    # do your stuff
    chelZapis = ... # define it
    token.register_next_step_handler(message, dengi, chelZapis=chelZapis)

def dengi(message, chelZapis):
    # do anything using variable chelZapis!

Apart from that from telebot point of view your code seems to be correct. If something is not working, perhaps it's something inside vivod. Did you try logging your bot and see if vivod is executed and where it fails?

I would be happy to answer your additional question separately, if you make it clearer which buttons do you want when and what should they do exactly.

Maria K
  • 1,491
  • 1
  • 3
  • 14