ziv-conntour commited on
Commit
6f15a44
·
verified ·
1 Parent(s): 161ee78

Update slack_bot.py

Browse files
Files changed (1) hide show
  1. slack_bot.py +23 -16
slack_bot.py CHANGED
@@ -1,21 +1,28 @@
1
- import requests
 
2
 
3
- SLACK_BOT_TOKEN = "xoxb-7956608570179-8288868683413-szG80H8DCFQvTbQSxMgN0z3S"
4
- SLACK_BOT_CHANNEL_ID = "C088D2NLN5C"
5
 
6
 
7
- def send_slack_message(message: str):
8
- payload = {"channel": SLACK_BOT_CHANNEL_ID, "text": message}
9
- headers = {
10
- "Authorization": f"Bearer {SLACK_BOT_TOKEN}",
11
- "Content-Type": "application/json",
12
- }
13
 
14
- response = requests.post(
15
- "https://slack.com/api/chat.postMessage", json=payload, headers=headers
16
- )
 
 
 
 
 
 
 
 
 
17
 
18
- if response.status_code == 200 and response.json().get("ok"):
19
- print("Message sent successfully!")
20
- else:
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')}")