使用 Slack API 进行音频文件后期处理

问题描述 投票:0回答:1

我正在尝试使用 Slack Webclient API 从 slack post 下载音频文件,在“app_memtion”事件处理程序中,我需要检索音频文件并处理它(如果它是音频帖子):

@app.event("app_mention")
def handle_starting_events(body, say, logger):
    if ('files' in body["event"]) and (body["event"]['files'][0]['mimetype'] == 'audio/webm'):
        audiourl = body["event"]['files'][0]['url_private_download']
        print(body["event"]['files'][0])
        response = requests.get(audiourl)
        with open('video.mp4', 'wb') as f:
            f.write(response.content)```
The problem is, the downloaded file is a html file, rather than an expected mp4 file. If I copy paste the link to a web browser, I can download the file and able to play it without any issue.

The variable "body" looks like this:

{'id': 'F04V96W21NK', '创建': 1679772610, '时间戳': 1679772610, '名称': 'audio_message.webm', '标题': 'audio_message.webm', 'mimetype': 'audio/webm '、'文件类型':'webm'、'pretty_type':'WebM'、'用户':'U04PV0SQDEJ'、'user_team':'T04QR9BEN3A'、'可编辑':False、'大小':29313、'模式': 'hosted', 'is_external': False, 'external_type': '', 'is_public': True, 'public_url_shared': False, 'display_as_bot': False, '用户名': '', 'subtype': 'slack_audio', '转录':{'状态':'处理'},'url_private':'https://files.slack.com/THEMP4FILE','url_private_download':'https://files.slack.com/THEMP4FILE', 'duration_ms':1820,'aac':'https://files.slack.com/THEMP4FILE','audio_wave_samples':[0,0,1,1,0,1,1,1,0,0,0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 7, 48, 89, 73, 54, 58, 76, 100, 93, 73, 81, 51, 57, 49, 41, 34, 25, 20, 11, 6, 3 , 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'media_display_type': '音频', '永久链接': 'https://myworkspace.slack .com/files/U04PV0SQDEJ/F04V96W21NK/audio_message.webm', 'permalink_public': 'https://slack-files.com/MYSLACKSTRING', 'is_starred': False, 'has_rich_preview': False, 'file_access': '可见'}


I tried all 3 links but nothing worked.

Is there anything wrong?
Thanks.

python api audio bots slack
1个回答
0
投票

您必须在获取请求中添加身份验证标头:

requests.get(url, headers={'Authorization': 'Bearer %s' % slack_token})

如果不使用身份验证标头,下载链接将重定向到登录页面。

© www.soinside.com 2019 - 2024. All rights reserved.