forked from saitota/SlackChannelArchiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (110 loc) · 4.82 KB
/
main.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
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
import json
import logging
import urllib.request
import os
from datetime import datetime
import time
print('Loading function... ')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class PostJson(object):
def __init__(self):
self.LEGACY_TOKEN = os.environ['LEGACY_TOKEN']
#self.OAUTH_TOKEN = os.environ['OAUTH_TOKEN']
#self.BOT_TOKEN = os.environ['BOT_TOKEN']
self.BOT_NAME = os.environ['BOT_NAME']
self.BOT_ICON = os.environ['BOT_ICON']
self.BOT_MESSAGE = os.environ['BOT_MESSAGE']
def headers(self):
return {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer {0}'.format(self.LEGACY_TOKEN)
}
def data_list(self):
return {
'token': self.LEGACY_TOKEN,
'exclude_archived': True
}
def data_hist(self, channel):
return {
'token': self.LEGACY_TOKEN,
'channel': channel,
'count': 1
}
def data_message(self, channel):
return {
'token': self.LEGACY_TOKEN,
'channel': channel,
'text': self.BOT_MESSAGE,
'username': self.BOT_NAME,
'icon_emoji': self.BOT_ICON
}
def headers_archive(self):
return {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer {0}'.format(self.LEGACY_TOKEN)
}
def data_archive(self, channel):
return {'token': self.LEGACY_TOKEN, 'channel': channel}
def handler(event, context):
# Output the received event to the log
#logging.info(json.dumps(event))
ARCHIVE_AFTER_DAYS = os.environ['ARCHIVE_AFTER_DAYS']
# list channel
url = 'https://slack.com/api/channels.list'
post_head = PostJson().headers()
post_body = PostJson().data_list()
req = urllib.request.Request(
url,
data=json.dumps(post_body).encode('utf-8'),
method='POST',
headers=post_head)
res = urllib.request.urlopen(req)
logger.info('post result: %s', res.msg)
channels = json.loads(res.read().decode('utf8'))
# get channels info / require scope :channels:history
for channel in channels.get('channels'):
#logger.info('check channel: %s ', channel.get('name', ''))
url = "https://slack.com/api/channels.history" # does not support application/json
post_body = PostJson().data_hist(channel.get('id'))
req = urllib.request.Request(
url,
urllib.parse.urlencode(post_body).encode('utf-8'))
res = urllib.request.urlopen(req)
channelhist = json.loads(res.read().decode('utf-8'))
# get timestamp of latest message
messages = channelhist.get('messages')
message = messages.pop(0) if messages else {}
ts = message.get('ts',
'1000000000') # epoch 2001/9/9 10:46:40 if blank
ts_datetime = datetime.fromtimestamp(float(ts))
now_datetime = datetime.now()
diff_datetime = now_datetime - ts_datetime
logger.info('Channel:%s is_archived:%s / ts_datetime:%s now_datetime:%s diff_datetime:%s ARCHIVE_AFTER_DAYS:%s', channel.get('name', ''), str(channel.get('is_archived', True)), str(ts_datetime), str(now_datetime), str(diff_datetime.days), str(ARCHIVE_AFTER_DAYS) )
# check old channels
if ((channel.get('is_archived', True) == False) and # exclude_archived Flag does not work well...
(int(diff_datetime.days) > int(ARCHIVE_AFTER_DAYS))):
logger.info('target channel to archive: %s', channel.get('name', ''))
# message to target channel / require scope : chat:write:user
post_data = PostJson().data_message(channel.get('id'))
url = 'https://slack.com/api/chat.postMessage'
req = urllib.request.Request(
url,
data=json.dumps(post_data).encode('utf-8'),
method='POST',
headers=post_head)
res = urllib.request.urlopen(req)
logger.info('message result: %s', res.msg)
# archive target channel / require scope : LEGACY TOKEN
post_head = PostJson().headers_archive()
post_data = PostJson().data_archive(channel.get('id'))
url = 'https://slack.com/api/channels.archive'
req = urllib.request.Request(
url,
data=json.dumps(post_data).encode('utf-8'),
method='POST',
headers=post_head)
res = urllib.request.urlopen(req)
logger.info('archive result: %s', res.msg)
return 'ok' # archive 1 channel and exit function, comment-out if you want archive every old channel
return 'ok'