This repository has been archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addwbwusercommand.py
63 lines (53 loc) · 1.84 KB
/
addwbwusercommand.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
import boto3
import json
import logging
import os
import re
from urllib.parse import parse_qs
from base64 import b64decode
dynamodb = boto3.client("dynamodb")
expected_token = os.environ["SLACK_TOKEN"]
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def respond(res):
return {
"statusCode": "200",
"body": json.dumps(res),
"headers": {
"Content-Type": "application/json",
},
}
def lambda_handler(event, context):
params = parse_qs(b64decode(event["body"]).decode("utf-8"))
print(params)
token = params["token"][0]
if token != expected_token:
logger.error("Request token (%s) does not match expected", token)
return {"statusCode": "400"}
if "text" not in params:
return respond("Usage: /addwbwuser <uuid> <user> [comment]")
text = params["text"][0]
try:
uuid, user, *comment = text.split(" ")
if not re.match(
r"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}", uuid
):
return respond("First argument must be a uuid")
if m := re.match(r"<@([A-Z0-9]+)\|.*>", user):
if m.group(1).startswith("U"):
user = m.group(1)
comment = " ".join(comment)
dynamodb.put_item(
TableName="SlackMapping",
Item={
"WbwUUID": {"S": uuid},
"Name": {"S": comment},
"SlackId": {"S": user},
},
)
return respond(
f"Saved uuid {uuid}, userid {user} in the database with comment {comment}"
)
return respond(f"User should be an @ mention of a slack user")
except ValueError:
return respond("Usage: /addwbwuser <uuid> <user> [comment]")