-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslackmessenger.py
37 lines (30 loc) · 1.13 KB
/
slackmessenger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
from slack_url import shorten
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import logging
logging.basicConfig(level=logging.DEBUG)
# Initialize the Slack client
client = WebClient(token=os.environ["SLACK_TOKEN"])
timestamp = os.environ["TIMESTAMP"]
presigned_url = os.environ["PRESIGNED_URL"]
file_path = f"trivy_report_table_{timestamp}.txt"
shorten_presign_url = shorten(presigned_url)
try:
with open(file_path, "r") as file:
file_content = file.read()
new_file = client.files_upload_v2(
title="Trivy Report",
filename=file_path,
content=file_content,
)
file_url = new_file.get("file").get("permalink")
new_message = client.chat_postMessage(
channel="C076CEFAXJ5",
text=f"Here is the file: {file_url} and the detailed report can be accessed at: {shorten_presign_url}. Valid for 10 hours",
)
print("File uploaded and message sent successfully!")
except FileNotFoundError:
print(f"Error: File {file_path} not found.")
except SlackApiError as e:
print(f"Error uploading file or sending message to Slack: {e.response['error']}")