1

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

You can outright make individualreq a closure.

CommandHandler takes a command or list of command to listen to and a list other options. There is a pass_user_data option that allows for user data to be passed to the callback.

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

The signature for individualreq callback will be updated to take the user_data

def individualreq(bot, update, user_data=None):
     #user_data is a dict
     print(user_data)

4 Comments

I tried this but the output is only a blank parentheses [ ]. I also tried the pass_args=True but the output is the same.
Are you polling the dispatcher and testing with a user account?
Yes I am polling and testing with user account. The bot also can reply to Telegram group.
Hello. I tried some stuff by looking at the GitHub example on how to use CommandHandler test_commandhandler.py and find out a way to pass the args to the function. I used id = update.message.text to store the passed command into id variable. Thank you very much for your answer sir that guide me to solve this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.