lambda_function.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. '''
  2. Twilio Ingest Lambda handler code
  3. Recieve an image URL from Twilio
  4. Apply a filter to the image and store in S3
  5. Return filtered image URL to the user
  6. '''
  7. import boto3
  8. import random
  9. import StringIO
  10. import urllib2
  11. from boto3.dynamodb.conditions import Key
  12. from boto3.session import Session
  13. from PIL import Image, ImageOps
  14. from twilio.rest import TwilioRestClient
  15. # create Twilio session
  16. # Add Twilio Keys
  17. account_sid = "account_sid"
  18. auth_token = "auth_token"
  19. client = TwilioRestClient(account_sid, auth_token)
  20. # create an S3 & Dynamo session
  21. s3 = boto3.resource('s3')
  22. session = Session()
  23. # Add Dynamo Region and Table
  24. dynamodb = boto3.resource('dynamodb', '_region_')
  25. table_users = dynamodb.Table('table_name')
  26. def sample_filter(im):
  27. '''
  28. A simple filter to be applied to the image
  29. '''
  30. black = "#000099"
  31. white= "#99CCFF"
  32. filter_image = ImageOps.colorize(ImageOps.grayscale(im), black, white)
  33. return filter_image
  34. def lambda_handler(event, context):
  35. message = event['body']
  36. from_number = event['fromNumber']
  37. pic_url = event['image']
  38. num_media = event['numMedia']
  39. # check if we have their number
  40. response_dynamo = table_users.query(KeyConditionExpression=Key('fromNumber').eq(from_number))
  41. # a new user
  42. if response_dynamo['Count'] == 0:
  43. if len(message) == 0:
  44. return "Please send us an SMS with your name first!"
  45. else:
  46. name = message.split(" ")
  47. table_users.put_item(Item={'fromNumber': from_number, 'name': name[0]})
  48. return "We've added {0} to the system! Now send us a selfie! ".format(name[0])
  49. else:
  50. name = response_dynamo['Items'][0]['name']
  51. if num_media != '0':
  52. # get photo from s3
  53. twilio_pic = urllib2.Request(pic_url, headers={'User-Agent': "Magic Browser"})
  54. image = urllib2.urlopen(twilio_pic)
  55. # Apply an Image filter
  56. im_buffer = image.read()
  57. im = Image.open(StringIO.StringIO(im_buffer))
  58. im = sample_filter(im)
  59. # Add to S3 Bucket
  60. bucket = "s3_bucket_name"
  61. key = "ingest-images/" + str(from_number.replace('+', '')) + "/" + str(random.getrandbits(50)) + ".png"
  62. resp_url = "https://s3.amazonaws.com/{0}/{1}".format(bucket, str(key))
  63. twilio_resp = "Hi {0}, your S3 link: ".format(name) + resp_url
  64. # build meta data
  65. m_data = {'fromNumber': from_number, 'url': resp_url, 'name': name}
  66. output = StringIO.StringIO()
  67. im.save(output, format="PNG")
  68. im_data = output.getvalue()
  69. output.close()
  70. s3.Bucket(bucket).put_object(Key=key, Body=im_data, ACL='public-read', ContentType='image/png', Metadata=m_data)
  71. else:
  72. twilio_resp = "No image found, try sending a selfie!"
  73. return twilio_resp