简体   繁体   English

使用 Python 向 Slack 发送带有附件的消息

[英]Send messages to Slack with attachments using Python

I'm trying to send messages to Slack using Python. It's working for the normal messages but I need it with attaching files.我正在尝试使用 Python 向 Slack 发送消息。它适用于普通消息,但我需要附加文件。

In this example, I'm trying to send an image located on my local device.在此示例中,我尝试发送位于本地设备上的图像。 Within the following piece of code:在以下代码中:

import os
import slack
from slack_sdk import WebClient
from pathlib import Path
from dotenv import load_dotenv
from slack_sdk.errors import SlackApiError

env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
client = slack.WebClient(token=os.environ['SLACK_TOKEN'])

try:
    filepath = "./ccc.jpg"
    response = client.files_upload(channels='#test', file=filepath)
    assert response["file"]  # the uploaded file
except SlackApiError as e:
    assert e.response["ok"] is False
    assert e.response["error"]
    print(f"Got an error: {e.response['error']}")

When I try to run the code it shows this type of error/warning:当我尝试运行代码时,它显示了这种类型的错误/警告:

C:\ProgramData\Anaconda3\envs\practice\lib\site-packages\slack\deprecation.py:16: UserWarning: slack package is deprecated. Please use slack_sdk.web/webhook/rtm package instead. For more info, go to https://slack.dev/python-slack-sdk/v3-migration/
  warnings.warn(message)
Got an error: missing_scope

Any way to solve this kind of problem?有什么办法可以解决这类问题?

The error missing_scope means the application with this token doesn't have enough permissions, meaning 'scope' is the terminology for permission in Slack.错误 missing_scope 表示具有此令牌的应用程序没有足够的权限,这意味着“范围”是 Slack 中权限的术语。

To solve this check the Required Scopes section here https://api.slack.com/methods/files.upload要解决此问题,请在此处查看 Required Scopes 部分https://api.slack.com/methods/files.upload

you will find you need to give your application the following permissions 'files:write', you can do that by going to https://api.slack.com -> your apps on the top right -> pick your application and go to 'OAuth & Permissions' tab, scroll down and you will find scopes sections, from there you can add the required scope.你会发现你需要给你的应用程序以下权限'files:write',你可以通过转到https://api.slack.com - >右上角的你的应用程序 - >选择你的应用程序和go来做到这一点“OAuth & Permissions”选项卡,向下滚动,您将找到范围部分,您可以从那里添加所需的 scope。

you will get a notification (banner) at the top of the page that you need to reinstall your app, do that then invite your bot/application to your channel and run your code again.您将在页面顶部收到一条通知(横幅),告知您需要重新安装您的应用程序,然后将您的机器人/应用程序邀请到您的频道并再次运行您的代码。

Just make sure to use the latest slack_sdk, not the deprecated one.只需确保使用最新的 slack_sdk,而不是已弃用的 slack_sdk。

This script should run with no errors:该脚本应该可以正常运行:

import os
from slack_sdk import WebClient
from pathlib import Path
from dotenv import load_dotenv
from slack_sdk.errors import SlackApiError

env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
client = WebClient(token=os.environ['SLACK_TOKEN'])

try:
    filepath = "./ccc.jpg"
    response = client.files_upload(channels='#test', file=filepath)
    assert response["file"]  # the uploaded file
except SlackApiError as e:
    assert e.response["ok"] is False
    assert e.response["error"]
    print(f"Got an error: {e.response['error']}")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM