-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.vanilla_everyone_slack_channels_bot.py
178 lines (139 loc) · 4.99 KB
/
2.vanilla_everyone_slack_channels_bot.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#import markovify
from custom_markov import markov
import os
import slack
import json
import re
DEBUG = True
# get the channels name and ID
channel_sc = slack.WebClient(token=os.environ['SLACK_TOKEN'])
list_response = channel_sc.conversations_list()
channels = {}
for channel in list_response['channels']:
if channel['name'] not in channels:
channels[channel['name']] = []
channels[channel['name']] = channel['id']
# load the name_dict of users
name_dict = {
'Tony': 'aaa',
'Abbie': 'dcs',
'Wennie': 'xyz',
'Eric' : 'oxj',
'Andy': 'fsd'
}
def get_user_id(user_name):
"""
Get a user's displayed name using the slack id
"""
return name_dict[user_name]
def _load_everyone_db():
"""
Reads 'database' from a JSON file on disk.
Returns a dictionary keyed by unique message permalinks.
"""
with open('data/everyone_merged_file.json', 'r') as json_file:
messages = json.loads(json_file.read())
return messages
def _load_channel_db():
"""
Reads 'database' from a JSON file on disk.
Returns a dictionary keyed by unique message permalinks.
"""
with open('data/channel_merged_file.json', 'r') as json_file:
messages = json.loads(json_file.read())
return messages
def _store_everyone_db(obj):
"""
Takes a dictionary keyed by unique message permalinks and writes it to the JSON 'database' on
disk.
"""
with open('data/everyone_merged_file.json', 'w') as json_file:
json_file.write(json.dumps(obj))
def _store_channel_db(obj):
"""
Takes a dictionary keyed by unique message permalinks and writes it to the JSON 'database' on
disk.
"""
with open('data/channel_merged_file.json', 'w') as json_file:
json_file.write(json.dumps(obj))
def build_text_model(trigger_id):
"""
Read the latest 'database' off disk and build a new markov chain generator model.
Returns TextModel.
"""
if DEBUG:
print("Building new model...")
messages = _load_everyone_db()
if trigger_id in name_dict.values():
messages = _load_everyone_db()
messages = messages[trigger_id]
if trigger_id in list(channels.values()):
messages = _load_channel_db()
messages = messages[list(channels.keys())[list(channels.values()).index(trigger_id)]]
if trigger_id == 'Company':
messages = _load_everyone_db()
messages = [item for sublist in list(messages.values()) for item in sublist]
return markov(messages)
def format_message(original):
"""
Do any formatting necessary to markon chains before relaying to Slack.
"""
if original is None:
return '< No text is generated>'
# Clear <> from urls
cleaned_message = re.sub(r'<(htt.*)>', '\1', original)
return cleaned_message
def main():
"""
Startup logic and the main application loop to monitor Slack events.
"""
# Create the slackclient instance
# v2 version: https://github.com/slackapi/python-slackclient/wiki/Migrating-to-2.x
sc = slack.RTMClient(token=os.environ['BOT_TOKEN'])
# store incoming new messages either in JSON by user or JSON by channels
@slack.RTMClient.run_on(event='message')
def store_messages(**payload):
data = payload['data']
channel = data['channel']
if 'user' in data and 'text' in data:
msg = data['text']
# don't store trigger word
if 'Hello' not in msg and 'Hi' not in msg and data['user'] in name_dict.values():
messages = _load_everyone_db()
messages[data['user']].append(msg)
_store_everyone_db(messages)
if channel in list(channels.values()):
messages = _load_channel_db()
messages[list(channels.keys())[list(channels.values()).index(channel)]].append(msg)
_store_channel_db(messages)
print("added {} to corpus", msg)
@slack.RTMClient.run_on(event='message')
def say_Hello_name(**payload):
data = payload['data']
if 'text' not in data:
return
# make a list of trigger words
trigger_everyone = ['Hello ' + i for i in list(name_dict.keys())]
trigger_channel = ['Hello ' + i for i in list(channels.keys())]
trigger_id = None
if data['text'] in trigger_everyone:
trigger_id = name_dict[data['text'][6:]]
if data['text'] in trigger_channel:
trigger_id = channels[data['text'][6:]]
if data['text'] == "Hello Company":
trigger_id = 'Company'
if not trigger_id:
return
model = build_text_model(trigger_id)
channel_id = data['channel']
thread_ts = data['ts']
webclient = payload['web_client']
webclient.chat_postMessage(
channel=channel_id,
text="{}".format(format_message(model)),
thread_ts=thread_ts,
as_user = True
)
sc.start()
if __name__ == '__main__':
main()