forked from aws-samples/lambda-apigateway-twilio-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
executable file
·92 lines (73 loc) · 2.75 KB
/
lambda_function.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
'''
Twilio Ingest Lambda handler code
Recieve an image URL from Twilio
Apply a filter to the image and store in S3
Return filtered image URL to the user
'''
import boto3
import random
import StringIO
import urllib2
from boto3.dynamodb.conditions import Key
from boto3.session import Session
from PIL import Image, ImageOps
from twilio.rest import TwilioRestClient
# create Twilio session
# Add Twilio Keys
account_sid = "account_sid"
auth_token = "auth_token"
client = TwilioRestClient(account_sid, auth_token)
# create an S3 & Dynamo session
s3 = boto3.resource('s3')
session = Session()
# Add Dynamo Region and Table
dynamodb = boto3.resource('dynamodb', '_region_')
table_users = dynamodb.Table('table_name')
def sample_filter(im):
'''
A simple filter to be applied to the image
'''
black = "#000099"
white= "#99CCFF"
filter_image = ImageOps.colorize(ImageOps.grayscale(im), black, white)
return filter_image
def lambda_handler(event, context):
message = event['body']
from_number = event['fromNumber']
pic_url = event['image']
num_media = event['numMedia']
# check if we have their number
response_dynamo = table_users.query(KeyConditionExpression=Key('fromNumber').eq(from_number))
# a new user
if response_dynamo['Count'] == 0:
if len(message) == 0:
return "Please send us an SMS with your name first!"
else:
name = message.split(" ")
table_users.put_item(Item={'fromNumber': from_number, 'name': name[0]})
return "We've added {0} to the system! Now send us a selfie! ".format(name[0])
else:
name = response_dynamo['Items'][0]['name']
if num_media != '0':
# get photo from s3
twilio_pic = urllib2.Request(pic_url, headers={'User-Agent': "Magic Browser"})
image = urllib2.urlopen(twilio_pic)
# Apply an Image filter
im_buffer = image.read()
im = Image.open(StringIO.StringIO(im_buffer))
im = sample_filter(im)
# Add to S3 Bucket
bucket = "s3_bucket_name"
key = "ingest-images/" + str(from_number.replace('+', '')) + "/" + str(random.getrandbits(50)) + ".png"
resp_url = "https://s3.amazonaws.com/{0}/{1}".format(bucket, str(key))
twilio_resp = "Hi {0}, your S3 link: ".format(name) + resp_url
# build meta data
m_data = {'fromNumber': from_number, 'url': resp_url, 'name': name}
output = StringIO.StringIO()
im.save(output, format="PNG")
im_data = output.getvalue()
output.close()
s3.Bucket(bucket).put_object(Key=key, Body=im_data, ACL='public-read', ContentType='image/png', Metadata=m_data)
else:
twilio_resp = "No image found, try sending a selfie!"
return twilio_resp