-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost.py
48 lines (39 loc) · 1.15 KB
/
post.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
from __future__ import print_function
import boto3
import uuid
import json
import os
def post(event, context):
messages_table_name = os.environ['DYNAMODB_MESSAGES_TABLE']
dynamodb_region = os.environ['DYNAMODB_MESSAGES_TABLE_REGION']
dynamodb = boto3.resource('dynamodb', region_name=dynamodb_region)
table = dynamodb.Table(messages_table_name)
message_id = str(uuid.uuid1())
body = json.loads(event['body']) if 'body' in event else None
message = body['Message'] if body is not None and 'Message' in body else 'No Message/Body found'
try:
put_response = table.put_item(
Item = {
'Id': message_id,
'MessageContent': message
}
)
except Exception as e:
err = e.response['Error']['Message']
print(err)
body = {
"error": err,
"event": event
}
return {
"statusCode": 500,
"body": json.dumps(body)
}
else:
body = {
"message_id": message_id
}
return {
"statusCode": 200,
"body": json.dumps(body)
}