-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
82 lines (66 loc) · 2.32 KB
/
query.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
# This is a script that expects a user to provide a phrase that JV says
# and returns the human readable meaning back.
# Creator: Cotton Beckfield, 1-31-2020
import os
import logging
import json
import urllib.request
# Environmental Variables
BOT_TOKEN = os.environ["BOT_TOKEN"]
SLACK_URL = os.environ["SLACK_URL"]
def lambda_handler(data, context):
print(data['event'])
if "challenge" in data:
return data["challenge"]
# Dictionary of Visneski-isms
jvdict = {
"hot wash":"lessons learned",
"press": "move forward",
"They're a great American": "they are an idiot",
"Making sure we're not destroying christmas": "be cautious",
"Let's rap about it on x day": "schedule a meeting",
"Taking away someone's birthday": "we're going to make them upset",
"Baby powder their feet": "hold their hand"
}
# Handling some potential errors
slack_event = data['event']
if "bot_id" in slack_event:
logging.warn("Ignore bot event")
return
text = slack_event["text"]
# Setting up the definition, and searching the dict for the
# relevant word search.
duplicate_words = ["at", "the", "their", "it", "on", "of", "a", "and", "that",
"i", "in", "i'm", "not", "go", "we're", "they're", "but", "there"]
# definition = [val for key, val in jvdict.items() if text in key]
definition = None
query_terms = text.split(" ")
for single_term in query_terms:
term_to_check = single_term.lower()
if term_to_check in duplicate_words:
continue
for jv_key, jv_value in jvdict.items():
if term_to_check in jv_key.lower():
definition = jv_value
if not definition:
logging.warning("Unknown phrase")
return
print("The definition is:", definition)
# definition = jvdict[text]
channel_id = slack_event["channel"]
print("Sending message to Slack: {}".format(definition))
json_txt = json.dumps({
"channel": channel_id,
"text": definition
}).encode('utf8')
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(BOT_TOKEN)
}
req = urllib.request.Request(
SLACK_URL,
data=json_txt,
headers=headers
)
urllib.request.urlopen(req)
return "200 OK"