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?