-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtmbot.py
executable file
·133 lines (123 loc) · 3.24 KB
/
rtmbot.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# Script that logs slack messages to text files. Configure with a "rtmbot.conf"
# text file like this
#
# SLACK_TOKEN: "xox-...."
# LOGFILE: /var/log/slackbot.log
#
# Will create files like logs/2018-03-10.txt, containing one JSON object per
# line, for each event. Will log all events in the channels except for
# "hello", "pong", and "user_typing".
from pathlib import Path
import sys
sys.dont_write_bytecode = True
this_file_dir = Path(__file__).parent.resolve()
sys.path.insert(0, str(this_file_dir / "vendor"))
import yaml
import json
import logging
import datetime
import codecs
import functools
import slack
rtm_event_list = [
'accounts_changed',
'bot_added',
'bot_changed',
'channel_archive',
'channel_created',
'channel_deleted',
'channel_history_changed',
'channel_joined',
'channel_left',
'channel_marked',
'channel_rename',
'channel_unarchive',
'commands_changed',
'dnd_updated',
'dnd_updated_user',
'email_domain_changed',
'emoji_changed',
'external_org_migration_finished',
'external_org_migration_started',
'file_change',
'file_comment_added',
'file_comment_deleted',
'file_comment_edited',
'file_created',
'file_deleted',
'file_public',
'file_shared',
'file_unshared',
'goodbye',
'group_archive',
'group_close',
'group_deleted',
'group_history_changed',
'group_joined',
'group_left',
'group_marked',
'group_open',
'group_rename',
'group_unarchive',
# 'hello',
'im_close',
'im_created',
'im_history_changed',
'im_marked',
'im_open',
'manual_presence_change',
'member_joined_channel',
'member_left_channel',
'message',
'pin_added',
'pin_removed',
'pref_change',
'presence_change',
'presence_query',
'presence_sub',
'reaction_added',
'reaction_removed',
'reconnect_url',
'star_added',
'star_removed',
'subteam_created',
'subteam_members_changed',
'subteam_self_added',
'subteam_self_removed',
'subteam_updated',
'team_domain_change',
'team_join',
'team_migration_started',
'team_plan_change',
'team_pref_change',
'team_profile_change',
'team_profile_delete',
'team_profile_reorder',
'team_rename',
'user_change',
# 'user_typing',
]
def process_event(event_type, **payload):
data = payload['data']
data['type'] = event_type
with codecs.open(config["DESTINATION"] + datetime.date.today().strftime('%Y-%m-%d.txt'), 'ab', 'utf-8') as f:
f.write(json.dumps(data))
f.write("\n")
def main_loop():
if "LOGFILE" in config:
logging.basicConfig(filename=config["LOGFILE"], level=logging.INFO, format='%(asctime)s %(message)s')
logging.info('rtmbot started.')
try:
rtm_client.start()
except KeyboardInterrupt:
sys.exit(0)
except:
logging.exception('Caught rtmbot exception.')
if __name__ == "__main__":
config = yaml.load(open('rtmbot.conf', 'r'), Loader=yaml.Loader)
slack_token = config["SLACK_TOKEN"]
rtm_client = slack.RTMClient(token=slack_token)
for e in rtm_event_list:
slack.RTMClient.on(event=e, callback=functools.partial(process_event, e))
main_loop()