-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformhandler.py
executable file
·55 lines (44 loc) · 1.98 KB
/
formhandler.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
import json, boto3, os, time
from urllib import request, parse
subscription_table = os.environ['subscription_table']
slack_token_parameter_name = os.environ['slack_token_parameter_name']
client = boto3.client('dynamodb')
ssm_client = boto3.client('ssm')
ssm_response = ssm_client.get_parameter(Name=slack_token_parameter_name, WithDecryption=True)
def send_slack_invite(email):
slack_token = ssm_response["Parameter"]["Value"]
form_payload = parse.urlencode({"token": slack_token, "email": email})
req = request.Request("https://slack.com/api/users.admin.invite", form_payload.encode("ascii"), headers={"Content-Type": "application/x-www-form-urlencoded"})
resp = request.urlopen(req)
print(resp.read())
return True
def save_subscription(email):
response = client.put_item(
TableName=subscription_table,
Item={'email': {'S': email}, 'timestamp': {'N': str(int(time.time()))}, 'Expiration': {'N': str(int(time.time())+31536000)}}
)
return True
def handle_post(event, context):
payload = json.loads(event["body"])
slack_invited = False
subscribed = False
if "email" not in payload or payload["email"] != "[email protected]":
# primitive antispam: decoy email field which should be set to loeffe. Actual email should be in _email
return {"statusCode": 401, "headers": { "Access-Control-Allow-Origin": 'https://pioco.fi', "Access-Control-Allow-Credentials": 'true'}, "body": "forbidden"}
if payload["sendSlackInvite"] == True:
slack_invited = send_slack_invite(payload["_email"])
# if payload["addToSubscribers"] == True:
# subscribed = save_subscription(payload["email"])
body = {
"slackInviteSent": slack_invited,
"subscribed": subscribed
}
response = {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin": 'https://pioco.fi',
"Access-Control-Allow-Credentials": 'true'
},
"body": json.dumps(body)
}
return response