-
Notifications
You must be signed in to change notification settings - Fork 0
/
angery.py
executable file
·239 lines (199 loc) · 7.63 KB
/
angery.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/python3
import os
import sys
import json
from random import choice
from urllib.request import urlopen, Request
from urllib.parse import parse_qsl, urlencode
import hmac
import hashlib
from time import time
import boto3
SLACK_OAUTH_API = "https://slack.com/api/oauth.access"
SLACK_POSTMESSAGE_API = "https://slack.com/api/chat.postMessage"
SLACK_USER_API = "https://slack.com/api/users.info?"
SLACK_TIMESTAMP = "X-Slack-Request-Timestamp"
SLACK_SIGNATURE = "X-Slack-Signature"
NOT_ANGERY = ["arranger", "danger", "hanger", "manger", "ranger", "tangerine"]
ANGERY_SUPPRESSOR = ["no_react", "noreact", "nobot", "no_bot"] + NOT_ANGERY
ANGERY_TRIGGER = (
"angery",
"angry",
"anger",
":frowning:",
":anger:",
":angry:",
"aggravating",
)
ANGERY_RESOURCES = os.path.join("resources", "angery.txt")
SDB_DOMAIN = "BOT_TOKENS"
def get_sdb(singleton=[]):
if not singleton:
singleton.append(boto3.client("sdb", region_name="eu-west-1"))
return singleton[0]
def verify_request_comes_from_slack(event):
try:
SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"].encode()
except KeyError:
raise RuntimeError("SLACK_SIGNING_SECRET environment variable is not set.")
headers = event.get("headers") or {}
if SLACK_TIMESTAMP not in headers or SLACK_SIGNATURE not in headers:
raise RuntimeError("Missing Slack verification headers")
timestamp = headers[SLACK_TIMESTAMP]
if time() - int(timestamp) > 60:
raise RuntimeError("Slack message too old - possible replay")
raw_body = event["body"]
signature_base = f"v0:{timestamp}:{raw_body}".encode()
signature = (
"v0=" + hmac.new(SIGNING_SECRET, signature_base, hashlib.sha256).hexdigest()
)
if signature != headers[SLACK_SIGNATURE]:
raise RuntimeError("Slack signature not matching")
def put_attributes(sdb, domain, team_id, token=None, reacts=True, **kwargs):
attributes = [
{"Name": name, "Value": value, "Replace": True}
for name, value in (
([("token", token)] if token else [])
+ [("reacts", str(reacts))]
+ list(kwargs.items())
)
]
response = sdb.put_attributes(
DomainName=domain, ItemName=team_id, Attributes=attributes
)
def get_attributes(sdb, domain, team_id):
response = sdb.get_attributes(DomainName=domain, ItemName=team_id)
return {attr["Name"]: attr["Value"] for attr in response.get("Attributes", [])}
def register(params):
try:
CLIENT_ID = os.environ["CLIENT_ID"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
except KeyError:
raise RuntimeError("Slack client id or secret environment variable is not set.")
if "error" in params:
return
code = params.get("code")
if not code:
raise RuntimeError("Malformed register request")
data = f"client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}&code={code}"
request = Request(
SLACK_OAUTH_API,
method="POST",
data=data.encode(),
headers={"Content-type": "application/x-www-form-urlencoded"},
)
with urlopen(request) as response:
auth_details = json.load(response)
team_id = auth_details.get("team_id")
team_name = auth_details.get("team_name")
bot_token = auth_details.get("bot", {}).get("bot_access_token")
if not auth_details.get("ok") or not team_id or not bot_token:
raise RuntimeError("Error getting auth tokens")
put_attributes(get_sdb(), SDB_DOMAIN, team_id, bot_token, team_name=team_name)
def check_admin(user_id, bot_token):
data = {"token": bot_token, "user": user_id}
request = Request(SLACK_USER_API + urlencode(data))
with urlopen(request) as response:
user = json.load(response)
if not user.get("ok"):
raise RuntimeError("Something wrong with user api")
return user["user"]["is_admin"]
def slash_command(body):
if body.get("command") == "/angery-reacts":
if body.get("text") not in ("activate", "deactivate"):
return "Not sure what you mean :angry:"
sdb = get_sdb()
team_id = body["team_id"]
attributes = get_attributes(sdb, SDB_DOMAIN, team_id)
if not check_admin(body["user_id"], attributes["token"]):
return "I don't talk to you non-admin folk"
if body["text"] == "deactivate":
put_attributes(sdb, SDB_DOMAIN, team_id, reacts=False)
return "Angery reacts deactivated"
else:
put_attributes(sdb, SDB_DOMAIN, team_id, reacts=True)
return "Angery reacts activated"
else:
return None
def handle_message(body):
if "challenge" in body:
return {"body": body["challenge"]}
team_id = body.get("team_id")
event = body.get("event", {})
channel = event.get("channel", "")
text = event.get("text", "").lower()
item = event.get("item", {})
reaction = event.get("reaction", "")
channel = channel or item.get("channel", "")
search_space = text + reaction
if not (
search_space
and not any(suppressor in search_space for suppressor in ANGERY_SUPPRESSOR)
and any(trigger in search_space for trigger in ANGERY_TRIGGER)
):
return {}
with open(ANGERY_RESOURCES, "r") as f:
imgs = f.readlines()
img = choice(imgs)[:-1]
sdb = get_sdb()
attributes = get_attributes(sdb, SDB_DOMAIN, team_id)
token = attributes.get("token")
if not token:
raise RuntimeError(f"No token for team_id: {team_id}")
if reaction and attributes.get("reacts", "True") != "True":
return {}
payload = {
"channel": channel,
"attachments": [{"image_url": img, "fallback": "Angery react"}],
}
encoded = json.dumps(payload).encode()
request = Request(
SLACK_POSTMESSAGE_API,
data=encoded,
headers={
"Content-type": "application/json",
"Authorization": f"Bearer {token}",
},
method="POST",
)
urlopen(request)
write_angery_metric()
return {}
def write_angery_metric():
cloudwatch = boto3.client("cloudwatch")
response = cloudwatch.put_metric_data(
Namespace="AngeryBot",
MetricData=[{"MetricName": "AngeryServed", "Unit": "Count", "Value": 1}],
)
def lambda_handler(event, context):
if event.get("resource") == "/AngeryBot/register":
register(event.get("queryStringParameters", {}))
try:
SLACK_APP_ID = os.environ["SLACK_APP_ID"]
return {
"statusCode": 302,
"headers": {"Location": f"https://slack.com/apps/{SLACK_APP_ID}"},
}
except KeyError:
return {
"body": "We will now deliver your angeries - don't forget to invite the Angery bot to your channels!"
}
elif event.get("resource") == "/AngeryBot/users":
sdb = get_sdb()
items = sdb.select(SelectExpression=f"select team_name from {SDB_DOMAIN}").get(
"Items", []
)
response = {"users": [i["Attributes"][0]["Value"] for i in items]}
response["count"] = len(response["users"])
return {"body": json.dumps(response)}
elif event.get("resource") == "/AngeryBot/command":
verify_request_comes_from_slack(event)
response = slash_command(dict(parse_qsl(event["body"])))
return {"statusCode": 200 if response else 400, "body": response}
verify_request_comes_from_slack(event)
body = json.loads(event["body"])
if body.get("event", {}).get("type") == "app_uninstalled":
get_sdb().delete_attributes(DomainName=SDB_DOMAIN, ItemName=body["team_id"])
else:
return handle_message(body)
return {}