-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.py
118 lines (102 loc) · 3.45 KB
/
events.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
import json
import logging
import os
import re
import random
# These dependencies need to be explicitly added to build.sh
from slackclient import SlackClient
logger = logging.getLogger()
logger.setLevel(logging.INFO)
expected_token = os.environ['VERIFICATION_TOKEN']
sc = SlackClient(os.environ['BOT_ACCESS_TOKEN'])
bot_id = os.environ['BOT_ID']
user_id = os.environ['USER_ID']
def lambda_handler(event, context):
logger.info("Event received: %s", event)
body = json.loads(event['body'])
if body['token'] != expected_token:
return empty_response(401)
if body['type'] == 'url_verification':
return verification_response(body)
process_event(body['event'])
return empty_response(200)
def process_event(event):
if event['type'] in ['message', 'app_mention']:
# don't respond to self or slackbot
if 'bot_id' in event and event['bot_id'] == bot_id:
return
if 'user' in event and event['user'] == user_id:
return
if 'user' in event and event['user'] == 'USLACKBOT':
return
if 'text' in event:
channel = event['channel']
thread_ts = event.get('thread_ts')
text = event['text']
respond_to_text(channel, thread_ts, text)
def respond_to_text(channel, thread_ts, text):
# Grill Vogel
if re.search(r"\bgreat(er|est)?\b", text, re.I) != None:
word = "hug"
match = re.search(r"\bgreat(?:er|est)?\b ((?:\w|-|')+)", text, re.I)
if match:
word = match.group(1)
post_text = "Ah, the greatest %s!" % word
username = "Grill Vogel"
post_message(channel, thread_ts, post_text, username, "grill_vogel.jpg")
# Peach
if re.search(r"\bbingo\b", text, re.I) != None:
post_text = "Bingo, bye-bye!"
username = 'Peach'
post_message(channel, thread_ts, post_text, username, "peach.png")
# Ivan
if re.search(r"\bivan\b", text, re.I) != None:
options = [
'I just say fuck it and go numb',
"now all of my jokes will be comedy-based",
"why don't you just say what you mean if it's so important",
"I don't know what it is or what it means, but I instantly hate everything about it",
"yeah cool good"
]
post_text = random.choice(options)
username = 'Ivan Anderson'
post_message(channel, thread_ts, post_text, username, 'ivan.png')
def post_message(channel, thread_ts, text, username, icon_name):
if thread_ts == None:
sc.api_call(
"chat.postMessage",
channel=channel,
text=text,
as_user=False,
username=username,
icon_url=icon_url(icon_name)
)
else:
sc.api_call(
"chat.postMessage",
channel=channel,
thread_ts=thread_ts,
text=text,
as_user=False,
username=username,
icon_url=icon_url(icon_name)
)
def empty_response(code):
return {
'statusCode': code,
'body': None,
'headers': {
'Content-Type': 'application/json'
}
}
def verification_response(body):
return {
'statusCode': 200,
'body': body['challenge'],
'headers': {
'Content-Type': 'application/json'
},
}
# files go in docs/icons/
def icon_url(filename):
return "https://jmanian.github.io/demon/icons/%s" % filename