Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working in Python 3 and Mysql 10.1.13-MariaDB databases

I want to display information from the database (select) using the TelegramBot buttons. For example, if in a telegram bot you click on the "Balances" or "Debt" button using a request, send the result to the user. I have a button code and a request BUT I can't combine them if I press the first button to answer the first requests or if I press the second button to answer the secondary requests (not enough knowledge) Sorry if it's a stupid question, I'm a beginner. Please tell me. thank

What I have tried:

Python
import telebot
from telebot import types
import mysql.connector

bot = telebot.TeleBot("TOKEN")

db = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="hosil"
)

kontragent = db.cursor()
kontragent.execute("SELECT * FROM kontragent")
myresult = kontragent.fetchall()

for x in myresult:
    print(x)

cursor = db.cursor()
cursor.execute("SELECT * FROM product")
myresult1 = cursor.fetchall()

for x in myresult1:
    print(x)


@bot.message_handler(commands=['start'])
def start(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
    btn1 = types.KeyboardButton("Склад")
    btn2 = types.KeyboardButton("Долг")
    markup.add(btn1, btn2)

    send_mess = f"Привет {message.from_user.first_name}!\nВведите "
    bot.send_message(message.chat.id, send_mess, parse_mode='html', reply_markup=markup)

bot.polling(none_stop=True)
Posted
Updated 4-Mar-21 22:40pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900