Skip to content

Instantly share code, notes, and snippets.

@axpence
Created February 23, 2016 08:42
Show Gist options
  • Select an option

  • Save axpence/49dbd102f607ec3e3921 to your computer and use it in GitHub Desktop.

Select an option

Save axpence/49dbd102f607ec3e3921 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import json
import requests
import re
import boto3
from random import choice
from string import ascii_uppercase
import os
def get_random_string():
return ''.join(choice(ascii_uppercase) for i in range(15))
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
# event = {
# "body": "",
# "fromNumber": "+18016362120",
# "toNumber": "+15802154669",
# "image": "https://api.twilio.com/2010-04-01/Accounts/ACdff8314110d3934d88963262be901fda/Messages/MM5cd3d82f0db646c99836b22085e6629c/Media/ME583e142923ebaa96ceae68ed64df248c",
# "numMedia": "1",
# "allParams": "{path={}, querystring={AccountSid=ACdff8314110d3934d88963262be901fda, ApiVersion=2010-04-01, Body=, From=+18016362120, FromCity=PROVO, FromCountry=US, FromState=UT, FromZip=84111, MediaContentType0=text/x-vcard, MediaUrl0=https://api.twilio.com/2010-04-01/Accounts/ACdff8314110d3934d88963262be901fda/Messages/MM5cd3d82f0db646c99836b22085e6629c/Media/ME583e142923ebaa96ceae68ed64df248c, MessageSid=MM5cd3d82f0db646c99836b22085e6629c, NumMedia=1, NumSegments=1, SmsMessageSid=MM5cd3d82f0db646c99836b22085e6629c, SmsSid=MM5cd3d82f0db646c99836b22085e6629c, SmsStatus=received, To=+15802154669, ToCity=LAWTON, ToCountry=US, ToState=OK, ToZip=73505}, header={Accept=*/*, Cache-Control=max-age=259200, CloudFront-Forwarded-Proto=https, CloudFront-Is-Desktop-Viewer=true, CloudFront-Is-Mobile-Viewer=false, CloudFront-Is-SmartTV-Viewer=false, CloudFront-Is-Tablet-Viewer=false, CloudFront-Viewer-Country=US, Via=1.1 b01831623dd4f0e4e4bccb0793c852ec.cloudfront.net (CloudFront), X-Amz-Cf-Id=QWawBa550Msv8WIjJjkdtl2lKUU8bRDkYAfdGahbJhg0YYNTNVHNVA==, X-Forwarded-For=52.71.252.191, 54.240.144.82, X-Forwarded-Port=443, X-Forwarded-Proto=https, X-Twilio-Signature=4U0ivsZxYtxluNNCB2Vfl7JvLqU=}}"
# }
all_params = event['allParams']
# media_url_l = re.findall(r'(?<=MediaUrl[0-9]=).*?(?=,)',all_params)
# content_type_l = re.findall(r'(?<=MediaContentType[0-9]=).*?(?=,)',all_params)
# num_media = int(re.findall(r'(?<=NumMedia=).*?(?=,)',all_params)[0])#match on NumMedia= until ','
media_url_l = []
content_type_l = []
num_media = 0
if 'image' in event:
if event['image'] != "":
media_url_l = [event['image']]
num_media = 1
content_type_l = ["text/x-vcard"]
return_object = {}
for i in range(0,num_media):
media_link = media_url_l[i]
media_content_type = content_type_l[i]
r = requests.get(media_link)
data_str = r.content
key_name = get_random_string()
s3 = boto3.resource('s3')
tmp_dir_key_name = '/tmp/'+key_name+'.tmp'
open(tmp_dir_key_name,'w').write(data_str)
bucket_name = 'eventz-vcards'
print('putting key='+key_name+' in bucket='+bucket_name +" for media_content_type="+media_content_type+" and media_link="+media_link)
s3.Object(bucket_name, key_name).put(Body=open(tmp_dir_key_name, 'rb'),ContentType=media_content_type)
os.remove(tmp_dir_key_name)
s3_url = "https://s3-us-west-2.amazonaws.com/"+bucket_name+"/" + key_name
lambdaClient = boto3.client('lambda')
response = lambdaClient.invoke(
FunctionName='heartbeat-scala-AddContact',
Payload=json.dumps({'vcardUrl' : s3_url})
)
if response['StatusCode'] < 200 and response['StatusCode'] > 299 and 'error' not in return_object.keys():
return_object['error'] = str(response['FunctionError'])
else:
return_object['error'] = None
return_object['inputEvent'] = event
return_object['action'] = "SET_STATE_PROPERTIES"
if return_object['error'] != None:
return_object['messageBody'] = "Sorry! \nWe couldn't add your contact card to our database."
else:
return_object['messageBody'] = "Awesome!\nWe've saved your contact card to our database.."
return_object['messageBody'] = return_object['messageBody'] + "\n\n1) Check schedule\n2) Search for attendee contact info\n3) Share your contact info with attendees\n\nchoose a number above"
print('return_object='+json.dumps(return_object) )
return return_object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment