-2

So I've been making a tg bot. One of the commands is /rickroll which is planned to send them a music file from my computer along with a message when they enter that command. This is part of the code that is focusing on that command now:

def playrickroll_command(update, context: CallbackContext):

    update.message.reply_text("You didn't give me up, so I won't let you down!")

    with open('E:/Music v 2.0.0.1/mp3 Player Music/Misc Soundtracks/Never Gonna Give you Up.mp3', 'rb') as file:

        update.effective_message.reply_audio(file)

dp.add_handler(CommandHandler("rickroll", playrickroll_command))

Please help!

  • Yes, it's probably possible. You'll get better answers when you [edit] your question and include the code you've tried so far and ask specific questions about problems with that code. As is, it sounds like you want someone to write that code for you. This is not how stackoverflow works. See [ask]. – Robert Jun 02 '21 at 20:24
  • what did you try? What did you find in Google? What did you find in telegram documentation? First make research before ask. – furas Jun 02 '21 at 22:09

1 Answers1

1

Yes, the Bot API supports sending media. See here for the official docs of the sendAudio method. Other methods for sending photos, videos etc. are above/below that method.

The python-telegram-bot library supports all of those methods (you used the corresponding tag, so I'll assume that you want to use that library). See e.g. here for the ptb docs of send_audio. There is also a section in the wiki about how sending media files works in ptb.

The easiest way to trigger such a method on /rickroll will be to use a CommandHandler. This could look something like

def callback(update: Update, context: CallbackContext):
    with open('never_gonna_give_you_up.mp3', 'rb') as file:
        # let's use ptbs shortcut logic for send_audio
        update.effective_message.reply_audio(file)

...

dispatcher.add_handler(CommandHandler('rickroll', callback))

If you're new to python-telegram-bot and you can't follow the above explanations, I recommend reading the tutorial.


Disclaimer: I'm currently the maintainer of python-telgeram-bot

CallMeStag
  • 5,467
  • 1
  • 7
  • 22