简体   繁体   English

我如何使用电报机器人发送带有 python 的 mp3 文件

[英]How do i send mp3 file with python using telegram bot

How can I send an MP3 audio file to a Telegram Bot using the open-source Python library Requests ?如何使用开源库Requests Python 将 MP3 音频文件发送到 Telegram Bot?

I wrote the following code:我写了以下代码:

URL = 'api.telegram.org/bot'+TOKEN+'/sendAudio'
af = open("temp.mp3", 'rb')
params = {'chat_id' : 421087308, 'audio' : af}
req = requests.post(URL, params)
af.close()

使用python 电报机器人包装器,您可以使用以下方法轻松完成:

bot.send_audio(chat_id=chat_id, audio=open('tests/test.mp3', 'rb'))

python-telegram-bot can be redundant if you need a bot for a single action like sending mp3 file.如果您需要一个机器人来执行单个操作(例如发送mp3文件),那么python-telegram-bot可能是多余的。 So if you want to send the file with pure requests lib, you can use following snippet:因此,如果您想使用纯requests库发送文件,可以使用以下代码段:

with open('tests/test.mp3', 'rb') as audio:
    payload = {
        'chat_id': TELEGRAM_CHAT_ID,
        'title': 'file.mp3'
        'parse_mode': 'HTML'
    }
    files = {
        'audio': audio.read(),
    }
    resp = requests.post(
        "https://api.telegram.org/bot{token}/sendAudio".format(token=TELEGRAM_TOKEN),
        data=payload,
        files=files).json()

Try this cammand :试试这个命令

update.message.reply_audio(audio='https://audio_link.mp3')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 python 电报机器人中发送.mp3 文件 - cant send .mp3 files in python telegram bot 如何使用 python 机器人将本地 html 文件发送到 Telegram - How can I send local html file to Telegram with python bot 如何使用 python 电报机器人将 pdf 文件发送回用户? - How to send pdf file back to user using python telegram bot? 如何使用带有 python selenium 的电报机器人发送消息 - How I can send message using telegram bot with python selenium 如何在Python中使用诱变剂将封面图片添加到mp3文件中? - How do I add cover image to a mp3 file using mutagen in Python? 如何使用 Python Azure 文本到语音 api 生成 mp3 文件 - How do I generate an mp3 file using Python Azure text to speech api 如何让我的 Discord 机器人在特定 mp3 文件的时间长度内停留在语音通道中? - How do I make my Discord bot stay in the voice channel for the length of time of a specific mp3 file? 使用 python-telegram-bot Package 将文档发送到电报机器人 - Send document to telegram bot using python-telegram-bot Package 如何安排 python 电报机器人在特定时间/之后发送消息? - How do I schedule python telegram bot to send message at/after certain time? 如何使用 python-telegram-bot 发送或转发消息? - How do you send or forward messages using python-telegram-bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM