[英]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.