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
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:
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.
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-librariessplit
method.As Imi already pointed out,
CallbackContext.args
is mainly intended for commands with argumentsDisclaimer: I’m currently the maintainer of
python-telegram-bot
.