skip to Main Content

I am using python-telegram-bot. This is my Python handler for the CommandHandler:

async def bulk_q(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    questions_user = ' '.join(context.args[:])

It is intended to allow the user to send questions in bulk, one question at a time, and I’m especially interested on catching the n so that I can do a for-loop over each line of input.

However, if I log the content of context.args, there is no n anywhere.

Would it be possible to extract each line of text I receive from the user independently?

2

Answers


  1. context.args is a list that contains the command arguments, and it’s already split by whitespace (spaces and newlines). If you want to extract each line of text, I suggest you first join the arguments and then split them by newline character (‘n’). Here’s an example of how you can modify your bulk_q function to achieve this:

    async def bulk_q(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    # Join the arguments with a space, then split by newline character
    questions_raw = ' '.join(context.args[:])
    questions_list = questions_raw.split('n')
    
    # Process each question
    for question in questions_list:
        # Your processing code here
        pass
    

    Keep in mind that this assumes the user sends the questions using the newline character. If they’re sending questions in separate messages, you’ll need to handle those messages differently. If that’s the case, you can store the questions in a list and process them as needed.

    Login or Signup to reply.
  2. You can just access the original text of the message via update.(effective_)message.text, see here and here. You can then use the std-libraries split method.

    As Imi already pointed out, CallbackContext.args is mainly intended for commands with arguments


    Disclaimer: I’m currently the maintainer of python-telegram-bot.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search