Python Telegram Bot not processing voice messages properly

I’m trying to create a telegram bot that can handle both text and voice messages from users. The bot should detect whether the user sent a text message or a voice message and process them differently. However, my bot seems to ignore voice messages completely and only responds when I send regular text.

Here’s my message handler function:

async def process_user_input(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    db_connection = sqlite3.connect(database='main.db')
    cursor = db_connection.cursor()

I’m trying to check if the message contains text or voice, but the bot skips voice messages entirely:

    if update.message.text:
        sql_query = f"update clients set issue = '{update.message.text}' where user_id = {context.user_data['current_user']}"
    else:
        voice_file = update.message.audio
        downloaded_file = await context.bot.get_file(voice_file.file_id)
        voice_path = f'voices/{voice_file.file_unique_id}.ogg'
        await downloaded_file.download(voice_path)
        
        sql_query = f"insert into voice_messages ('user_id', 'file_path') values ({context.user_data['current_user']}, '{voice_path}')"
        cursor.execute(sql_query)
        db_connection.commit()
        
        sql_query = f"update clients set issue = 'sent voice message' where user_id = {context.user_data['current_user']}"
    
    cursor.execute(sql_query)
    db_connection.commit()
    db_connection.close()
    
    await context.bot.send_video(chat_id=update.effective_chat.id, video=response_video)
    return await handle_next_step(update, context)

And here’s my conversation handler setup:

USER_INPUT_STATE: [MessageHandler(filters.TEXT | filters.AUDIO, process_user_input)],

I couldn’t find proper documentation for audio message handling, so I just used filters.AUDIO. What am I doing wrong?

Yeah, you nailed the filter issue, but there’s another bug - you’re using update.message.audio when you should be using update.message.voice for voice messages. Try voice_file = update.message.voice then grab voice_file.file_id. Also, heads up - voice messages are typically .oga format, not .ogg.

Had this exact problem building my first telegram bot last year. You’ve got two issues here: the filter mismatch and bad logic structure. Your if update.message.text: followed by else: assumes everything non-text is a voice message - that’s wrong. Users send photos, documents, all kinds of stuff. Use if update.message.text: then elif update.message.voice: to handle each type explicitly. Stops your bot from crashing on random media. Also throw some error handling around file downloads - network hiccups will kill the download process.

Your problem is the filter type. Voice messages in Telegram aren’t the same as regular audio files. You’re using filters.AUDIO, which only grabs standard audio files like MP3s. For voice messages, you need filters.VOICE instead. Change your conversation handler to: python USER_INPUT_STATE: [MessageHandler(filters.TEXT | filters.VOICE, process_user_input)], Also, make sure you’re checking for update.message.voice in your processing function, not update.message.audio. Voice messages have different attributes than regular audio files.