def lambda_handler(event, context): utils.log_lambda(event, context) body, bucket, _ = utils.read_s3_from_event(event) message = body['Body'] stocks = re.split(r'\W+', message) for stock in stocks: if stock == 'overview': continue stock_info = {} try: stock_info = stock_api.get_info(stock) message = format_ticker_message(stock_info) except Exception as e: logger.exception('Could not get stock ticker info') message = str(e) recipient = body['From'] sender = body['To'] utils.send_sms(message, sender, recipient, bucket) if stock_info: key = 'parsed-overview/{}-{}-{}'.format(recipient, stock_info['Symbol'], body['MessageSid']) config.S3_CLIENT.put_object(Bucket=bucket, Key=key, Body=json.dumps(body))
def read_s3(bucket, key): try: response = config.S3_CLIENT.get_object(Bucket=bucket, Key=key) raw_body = response['Body'].read() logger.info('Processing data: {}'.format(raw_body)) body = json.loads(raw_body) except Exception: logger.exception('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) raise return body
def lambda_handler(event, context): utils.log_lambda(event, context) body, bucket, key = utils.read_s3_from_event(event) message = body['Body'] requested_command = None for command in ('helpme', 'details', 'buy', 'sell'): if message.lower().startswith(command): requested_command = command break requested_command = requested_command or 'overview' new_key = key.replace('raw', requested_command, 1) try: config.S3_CLIENT.put_object( Key=new_key, Bucket=bucket, Body=json.dumps(body), ContentType='application/json', ) except Exception: logger.exception('Could not upload data to {}'.format(new_key)) raise # FIXME: If this is the first time the user has contacted us, send them the help message if False: help_key = key.replace('raw', 'help', 1) try: config.S3_CLIENT.put_object( Key=help_key, Bucket=bucket, Body=json.dumps(body), ContentType='application/json', ) except Exception: logger.exception('Could not start help message workflow {}'.format(help_key))