T
T
Tim Novikov2021-10-29 22:51:53
Python
Tim Novikov, 2021-10-29 22:51:53

How to implement sending music in Telegramm bot?

def Music(message):
    for filename in os.listdir("GeometryDash"):              
        audio = open(f"GeometryDash\{filename}", "rb")
        bot.send_audio(message.chat.id, audio,timeout=60)


This function should send all the tracks from the folder to the chat, but for some reason it
also sends some extraneous files that are not in my folder.

617c506344592239713911.png

In turn, there are only 4 files in the folder.

617c50c430380356477080.png

what can be done about it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2021-10-29
@Tim_1527

Check filename and only submit if filename has the correct extension.
Something like this:

def Music(message):
    for filename in os.listdir("GeometryDash"):
        if filename.endswith('.mp3') or filename.endswith('m4a'): 
            audio = open(f"GeometryDash\{filename}", "rb")
            bot.send_audio(message.chat.id, audio,timeout=60)

PS: There is nothing to check.
PS2: - not according to Feng Shui (PEP8) def Music

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question