-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_add.py
44 lines (32 loc) · 979 Bytes
/
message_add.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
import datetime
import json
import boto3
from decimal import Decimal
def handler(event, context):
dynamodb = boto3.resource("dynamodb", region_name="eu-west-1")
table = dynamodb.Table("Messages")
now = datetime.datetime.utcnow()
timestamp = Decimal((now.replace(microsecond=0) - \
datetime.datetime(1970, 1, 1)).total_seconds())
table.put_item(
Item={
"toId": event["toId"],
"to": event["to"],
"fromId": event["fromId"],
"from": event["from"],
"receivedAt": timestamp,
"message": event["message"],
}
)
return True
if __name__ == "__main__":
# Read event, context from sys.argv
args = [json.loads(arg) for arg in sys.argv[1:2]]
# Provide None for event, context if not provided
while len(args) < 2:
args.append(None)
# Print the output
print(handler(*args))