Python Forum
how to merge 2 handlers pyTelegramBotApi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to merge 2 handlers pyTelegramBotApi
#1
Please write how to run these 2 functions depending on message.text. For example, if message.text== from 1 to 10 and the letter “v” then let the 1-function be executed, and if message.text contains a query that is in the product names in the database, then send the name of these products to the user. Thank you.

@bot.message_handler()
def send_docs(message):
    if message.text in ['1','2','3','4','5','6','7','8','9','10','v']:
        file_source=open(f"D:\Project\domstroy_{message.text}.csv")
        bot.send_document(message.chat.id, file_source) 

@bot.message_handler()
def send_query(message):
    db=sqlite3.connect("ostatki.db",check_same_thread=False)
    cur=db.cursor()
    cur.execute("DROP table IF EXISTS tovarniye_ostatki")
    cur.execute("CREATE virtual table IF NOT EXISTS tovarniye_ostatki USING FTS5 (nazvaniye_tovara,ostatok_tovara)")
    with open ("domstroy.csv",'r', encoding="utf8",) as file:
        for row in file:   
            cur.execute("INSERT OR IGNORE INTO tovarniye_ostatki VALUES (?,?)",row.split(";"),)
    db.commit()(nazvaniye_tovara MATCH (?)",(message.text,))
    result = result_query.fetchall()
    for i in result:
       bot.send_message(message.chat.id, (f"{i[1]} == {i[0]}"))
    db.close()
Reply
#2
I'm not familar with the Telegram-API.
In general, a handler could call other functions.
Make the decision in the handler function and the handler function calls one of the other functions.

@bot.message_handler()
def message_handler(message):
    if message.text in ['1','2','3','4','5','6','7','8','9','10','v']:
        send_docs(message)
    else:
        send_query(message)

def send_docs(message):
    file_source=open(f"D:\Project\domstroy_{message.text}.csv")
    bot.send_document(message.chat.id, file_source) 

def send_query(message):
    db=sqlite3.connect("ostatki.db",check_same_thread=False)
    cur=db.cursor()
    cur.execute("DROP table IF EXISTS tovarniye_ostatki")
    cur.execute("CREATE virtual table IF NOT EXISTS tovarniye_ostatki USING FTS5 (nazvaniye_tovara,ostatok_tovara)")
    with open ("domstroy.csv",'r', encoding="utf8",) as file:
        for row in file:   
            cur.execute("INSERT OR IGNORE INTO tovarniye_ostatki VALUES (?,?)",row.split(";"),)
    db.commit()(nazvaniye_tovara MATCH (?)",(message.text,))
    result = result_query.fetchall()
    for i in result:
       bot.send_message(message.chat.id, (f"{i[1]} == {i[0]}"))
    db.close()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
thank you very much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python logrotate adding other handlers RRR 0 1,418 May-23-2021, 06:02 AM
Last Post: RRR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020