Skip to content

added voice and audio support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

View-my-Git-Lab-krafi
Copy link

Now you can send your voice to your pc, or a audio file formet where mp3 wav or any other popular audio.

just added code is


@bot.message_handler(content_types=['audio', 'voice', 'document'])
def saveAudio(doc):
    if not check_user(doc):
        response = "Audio not saved. Please log in, insert a valid password."
        bot.send_message(doc.from_user.id, response)
        return

    if doc.content_type == 'audio':
        # Handles various audio file formats
        file_info = bot.get_file(doc.audio.file_id)
        file_name = doc.audio.file_name if doc.audio.file_name else str(doc.date) + ".mp3"  # Default to .mp3 if no name
    elif doc.content_type == 'voice':
        # Handles Telegram's voice message format (.ogg)
        file_info = bot.get_file(doc.voice.file_id)
        file_name = str(doc.date) + ".ogg"
    elif doc.content_type == 'document':
        # Handles audio files sent as a document (supports all formats like MP3, WAV, M4A, etc.)
        if doc.document.mime_type.startswith("audio/"):
            file_info = bot.get_file(doc.document.file_id)
            file_name = doc.document.file_name if doc.document.file_name else str(doc.date)

    # Download the file
    downloaded_file = bot.download_file(file_info.file_path)
    file_path = SHARED_FOLDER + file_name
    with open(file_path, 'wb') as new_file:
        new_file.write(downloaded_file)
    bot.send_message(doc.chat.id, f"Audio file saved as {file_path}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant