-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
79 lines (66 loc) · 3.2 KB
/
app.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
import os
import requests
import websocket
import json
import random
import re
SLACK_AUTH_TOKEN = os.environ['SLACK_AUTH_TOKEN']
SLACK_REALTIME_ENDPOINT = 'https://slack.com/api/rtm.start'
def generateDataProject():
madlib = 'You could use {dataset_name} ({dataset_link}) to get a better understanding of {service}'
random_resource = random.choice(json.load(open('json_data/resources.json')))
params = {
'dataset_name': random_resource['title'],
'dataset_link': random_resource['url'],
'service': random.choice(json.load(open('json_data/services.json'))),
}
return madlib.format_map(params)
def generateAppIdea():
madlib = 'An app for {verb} the {noun_phrase} of the {agency} & the {org}'
params = {
'verb': random.choice(json.load(open('json_data/verbs.json'))),
'noun_phrase': random.choice(json.load(open('json_data/noun_phrases.json'))),
'agency': random.choice(json.load(open('json_data/agencies.json'))),
'org': random.choice(json.load(open('json_data/orgs.json')))
}
return madlib.format_map(params)
if __name__ == "__main__":
params = {'token': SLACK_AUTH_TOKEN}
socket = requests.get(SLACK_REALTIME_ENDPOINT, params=params)
channel_lookup = {c['id']: c['name'] for c in socket.json()['channels']}
user_lookup = {u['id']: u['name'] for u in socket.json()['users']}
ws = websocket.WebSocket()
ws.connect(socket.json()['url'])
while True:
result = json.loads(ws.recv())
message_id = 1
if result.get('type') == 'message' and not result.get('subtype'):
channel_name = channel_lookup.get(result['channel'], 'general')
user_name = user_lookup.get(result['user'], '')
formatted = None
if 'app me' in result['text'].lower():
message = generateAppIdea()
formatted = "Here's an idea, {0} \n {1}".format(user_name, message)
if 'data project' in result['text'].lower():
message = generateDataProject()
formatted = "Here's an idea, {0} \n {1}".format(user_name, message)
bad_phrases = ['you guys', 'hey guys', 'hi guys', 'sup guys', 'guys,', 'guys.']
if any(bad_phrase in result['text'].lower() for bad_phrase in bad_phrases):
alternatives = ['peeps', 'nerds', 'folks', 'y\'all', 'youse', 'wonderful people']
formatted = "did ya mean %s?" % random.choice(alternatives)
if "team_join" == result['type']:
formatted = "Welcome to the Chi Hack Night Slack, {0}".format(result["user"]["name"])
# acks = ['aws','js','sql','ds']
# for ack in acks:
# ack_re = re.compile("\\b"+ack+"\\b")
# if ack_re.match(result['text'].lower()):
# formatted = "Would you mind explaining what %s means? - @jpvelez ghost" % ack
if formatted:
postback = {
'id': message_id,
'type': 'message',
'channel': result['channel'],
'text': formatted,
}
ws.send(json.dumps(postback))
message_id += 1