使用 Python 将带有附件的消息发送到 Slack
我正在尝试使用Python向Slack发送消息。它适用于普通消息,但我需要附加文件。
在此示例中,我正在尝试发送位于本地设备上的图像。在以下代码中:
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']}")
当我尝试运行代码时,会显示此类错误/警告:
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
解决此类问题的任何方法?
I'm trying to send messages to Slack using Python. It's working for the normal messages but I need it with attaching files.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误丢失的_scope意味着使用此令牌的应用程序没有足够的权限,这意味着“范围”是Slack许可的术语。
要求解此检查所需的范围部分。 https://api.slack.com/methods/methods/filess/filess/filess/files 。
noreferrer“> https://api.slack.com - >您的应用在右上角 - >选择您的应用程序,然后转到'Oauth&权限的选项卡,向下滚动,您会发现范围部分,您可以从那里添加所需的范围。
您将在页面顶部收到一个通知(横幅),即您需要重新安装应用程序,然后邀请您的bot/application访问频道并再次运行代码。
只需确保使用最新的slack_sdk,而不是弃用的slave_sdk。
该脚本应没有错误运行:
The error missing_scope means the application with this token doesn't have enough permissions, meaning 'scope' is the terminology for permission in Slack.
To solve this check the Required Scopes section here 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.
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.
This script should run with no errors: