-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslackintegrationgit.py
More file actions
137 lines (120 loc) · 4.08 KB
/
Copy pathslackintegrationgit.py
File metadata and controls
137 lines (120 loc) · 4.08 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import time
import datetime
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
import boto3
# --- CONFIGURATION ---
# Get credentials from environment variables
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
slack_token = os.environ.get('SLACK_BOT_TOKEN')
# Initialize Slack client
client = WebClient(token=slack_token)
# --- SLACK FUNCTIONS ---
def get_all_joined_channel_ids():
channel_ids = []
cursor = None
while True:
response = client.conversations_list(
types="public_channel,private_channel",
limit=1000,
cursor=cursor
)
for channel in response['channels']:
if channel.get('is_member'):
channel_ids.append(channel['id'])
cursor = response.get('response_metadata', {}).get('next_cursor')
if not cursor:
break
return channel_ids
def fetch_hcmsupportbot_messages_all_channels(oldest, latest):
all_messages = []
channel_ids = get_all_joined_channel_ids()
for channel_id in channel_ids:
try:
response = client.conversations_history(
channel=channel_id,
oldest=oldest,
latest=latest,
limit=1000
)
for msg in response['messages']:
if '#hcmsupportbot' in msg.get('text', '').lower():
all_messages.append({
'channel': channel_id,
'user': msg.get('user', ''),
'text': msg.get('text', ''),
'ts': msg.get('ts')
})
except Exception as e:
print(f"Error fetching from {channel_id}: {e}")
return all_messages
# --- PDF FORMATTING ---
styles = getSampleStyleSheet()
style_normal = ParagraphStyle(
'Normal',
parent=styles['Normal'],
fontName='Courier',
fontSize=10,
leading=12
)
style_header = ParagraphStyle(
'Header',
parent=styles['Heading2'],
alignment=1,
spaceAfter=20
)
# --- PDF GENERATION ---
def save_to_pdf(messages, filename):
doc = SimpleDocTemplate(filename, pagesize=letter)
flowables = []
header_text = f"Daily #hcmsupportbot Messages\n{datetime.datetime.now().strftime('%Y-%m-%d')}"
flowables.append(Paragraph(header_text, style_header))
for msg in messages:
# Use actual user/text from Slack message objects
user = msg.get('user', 'unknown')
text = msg.get('text', '')
flowables.append(Paragraph(f"<b>{user}</b>: {text}", style_normal))
flowables.append(Spacer(1, 12))
doc.build(flowables)
def upload_to_s3(filename):
# Create S3 client using environment variables
s3 = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
s3.upload_file(
Filename=filename,
Bucket='hcmbotknowledgesource',
Key='slack_pdfs/' + filename
)
print(f"Uploaded {filename} to s3://hcmbotknowledgesource/slack_pdfs/{filename}")
# --- MAIN WORKFLOW ---
def main():
# Calculate time range
now = datetime.datetime.now()
yesterday = now - datetime.timedelta(days=1)
# Fetch messages
messages = fetch_hcmsupportbot_messages_all_channels(
oldest=int(yesterday.timestamp()),
latest=int(now.timestamp())
)
if messages:
# Generate PDF
filename = f"hcmsupportbot_{now.strftime('%Y-%m-%d')}.pdf"
save_to_pdf(messages, filename)
# Upload to S3
upload_to_s3(filename)
print(f"Successfully processed {len(messages)} messages and created {filename}")
# Return the filename for further processing
return filename
else:
print("No messages found for today.")
return None
if __name__ == "__main__":
main()