def lambda_handler(event, context):
    utils.log_lambda(event, context)

    body, bucket, _ = utils.read_s3_from_event(event)

    message = body['Body']
    cmd, ticker, count = re.split('\W+', message)

    stock_info = get_info(ticker)
    symbol = stock_info['Symbol']
    current_price = stock_info['LastPrice']

    recipient = body['From']

    portfolio_key = 'portfolio/{}.json'.format(recipient)
    try:
        portfolio = utils.read_s3(bucket, portfolio_key)
        current_portfolio = defaultdict(list, portfolio)
    except:
        current_portfolio = defaultdict(list)

    current_portfolio[symbol].append({'count': count, 'price': current_price})

    config.S3_CLIENT.put_object(Bucket=bucket, Key=portfolio_key, Body=json.dumps(current_portfolio))

    total_shares = sum(int(d['count']) for d in current_portfolio[symbol])

    sender = body['To']
    message = 'You purchased {} shares of {} @ ${}. You have a total of {} shares.'.format(count, symbol, current_price, total_shares)
    utils.send_sms(message, sender, recipient, bucket)
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 lambda_handler(event, context):
    utils.log_lambda(event, context)

    body, bucket, _ = utils.read_s3_from_event(event)

    recipient = body['From']
    sender = body['To']

    message = 'Thanks for using hotstockbling. Enter a ticker symbol or use HELPME, DETAILS, BUY, or SELL.'
    utils.send_sms(message, sender, recipient, bucket)
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))
def lambda_handler(event, context):
    body, bucket, key = utils.read_s3_from_event(event)
    recipient, sender = key.split('/')[-1].split('-')

    texter = twilio.rest.TwilioRestClient(os.environ['TWILIO_ACCOUNT_ID'], os.environ['TWILIO_AUTH_TOKEN'])
    texter.messages.create(body=body['message'], to=recipient, from_=sender)