Spaces:
Running
Running
Update slack_bot.py
Browse files- slack_bot.py +23 -16
slack_bot.py
CHANGED
@@ -1,21 +1,28 @@
|
|
1 |
-
import
|
|
|
2 |
|
3 |
-
SLACK_BOT_TOKEN = "
|
4 |
-
SLACK_BOT_CHANNEL_ID = "
|
5 |
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
"Content-Type": "application/json",
|
12 |
-
}
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
print("Failed to send message:", response.json())
|
|
|
1 |
+
from slack_sdk import WebClient
|
2 |
+
from slack_sdk.errors import SlackApiError
|
3 |
|
4 |
+
SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN")
|
5 |
+
SLACK_BOT_CHANNEL_ID = os.getenv("SLACK_BOT_CHANNEL_ID")
|
6 |
|
7 |
|
8 |
+
def send_slack_message_with_image(
|
9 |
+
title: str, filename: str, message: str, file_path: str
|
10 |
+
):
|
11 |
+
client = WebClient(token=SLACK_BOT_TOKEN)
|
|
|
|
|
12 |
|
13 |
+
try:
|
14 |
+
response = client.files_upload_v2(
|
15 |
+
channel=SLACK_BOT_CHANNEL_ID,
|
16 |
+
initial_comment=message,
|
17 |
+
file=file_path,
|
18 |
+
title=title,
|
19 |
+
filename=filename,
|
20 |
+
)
|
21 |
+
if response.get("ok"):
|
22 |
+
print("Message with image sent successfully!")
|
23 |
+
else:
|
24 |
+
print("Failed to send message with image:", response)
|
25 |
|
26 |
+
except SlackApiError as e:
|
27 |
+
# Handle Slack-specific errors
|
28 |
+
print(f"Slack API Error: {e.response.get('error')}")
|
|