This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
forked from department-of-veterans-affairs/wtf-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wtf.py
70 lines (47 loc) · 1.73 KB
/
wtf.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
import csv
import http
import requests
from flask import Flask, make_response, request
from config import Config
APP = Flask(__name__)
APP.config.from_object(Config())
@APP.route('/slack', methods=['GET', 'POST'])
def slack():
req = dict(request.form)
if not all(k in req.keys() for k in ['text', 'token']):
return make_response('Improper request.', http.HTTPStatus.BAD_REQUEST)
if req['token'] not in APP.config['SLACK_TOKENS']:
return make_response('Not authorized', 401)
raw = requests.get(APP.config['DATA_URL'])
decoded = raw.content.decode('utf-8')
reader = csv.reader(decoded.split('\n'), delimiter=',')
data = [r for r in reader if len(r) > 1]
term_dict = {}
for d in data:
acroynm = d[0].lower()
definition = d[1].strip()
context = ''
notes = ''
if len(d[2]) > 0:
context = "\n\t- " + d[2].strip()
if len(d[3]) > 0:
notes = "\n\t- " + d[3].strip()
full_data = "{}{}{}".format(definition, context, notes)
existing = term_dict.get(acroynm, None)
if not existing:
term_dict[acroynm] = [full_data]
else:
term_dict[acroynm] = existing + [full_data]
try:
acroynm_defined = term_dict[req['text'].lower()]
if len(acroynm_defined) > 1:
response = ' - ' + '; \n - '.join(acroynm_defined)
else:
response = ' - ' + acroynm_defined[0]
response = req['text'] + '\n' + response
except KeyError:
response = """
Entry for '{}' not found! Acronyms may be added at
https://github.com/department-of-veterans-affairs/acronyms
""".format(req['text'])
return make_response(response)