Search code examples
pythontelegrampython-telegram-bot

Telegram Bot respond to specific command in Python list


I am making a Telegram bot that can can access database to reply users' query. The bot need to respond to specific request of certain data in database. I was able to solve for when users request for all data but I am stuck with individual data. I am using telegram.ext from telegram package in python. Here is what I have done so far.

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import MySQLdb

currr = [] # global list var ~don't bash me for using global in python please, I'm a newbie

# request for all data in database
def request2(bot, update):
    db = MySQLdb.connect(host = "local", user = "root", passwd = "pwd", db = "mydb")
    cur = db.cursor()
    cur.execute("select ID from table")
    ID = cur.fetchall()

    cur.execute("SELECT ID, temp FROM table2 order by indexs desc")
    each_rows = cur.fetchall()
    for IDs in ID:
        for each_row in each_rows:
            if str(each_row[0])[0:4]==str(ID)[2:6]:
                update.message.reply_text('reply all related data here')
                break

# request for single data
def individualreq(bot, update):
    db = pymysql.connect(host = "localhost", user = "root", passwd = "pwd", db = "mydb")
    update.message.reply_text('reply individual data to users here')

def main():
    updater = Updater("TOKEN")
    dp = updater.dispatcher

    global currr
    # get all ID form database
    db = MySQLdb.connect(host = "localhost", user = "root", passwd = "pwd", db = "mydb")
    cur = db.cursor()
    cur.execute("select ID from table")
    curr_ID = cur.fetchall()

    # example ID = 'F01', 'F02', 'F03'
    for curr_IDs in curr_ID:
        currr.append(curr_IDs[0])

    # request all data
    dp.add_handler(CommandHandler("all", request2))

    # request individual data
    dp.add_handler(CommandHandler(currr, individualreq)) # list command in currr[]

if __name__ == '__main__':
    main()

I am looking for a way to pass the current command which is also the ID in database that user request in the currr[] list to the individualreq(bot, update) function so that only data of the called ID is being replied. Users will select from a list of ID in telegram and the command handler can pass the selected ID to the function. I have not found a way to pass the ID to the function. Could someone help me to solve this please. Thanks


Solution

  • I find out a solution for my question from the answer provided by Oluwafemi Sule. CommandHandler can pass the arguments of the command to the function by adding pass_args=True in the CommandHandler.

    dp.add_handler(CommandHandler(currr, individualreq, pass_args=True))
    

    To print out the args in the function, the function need to receive the args.

    def individualreq(bot, update, args):
        # id store the args value
        id = update.message.text
        print(id[1:]) # [1:] is to get rid of the / in id