def handle_coffee(request_body, text): coffees = text.count(u'\u2615') if coffees == 0: return '' name = myutils.extract_user_first_name(request_body) uid = myutils.extract_user_id(request_body) date = myutils.extract_date(request_body) smilies = [u'\ud83d\ude43',u'\ud83d\ude0f',u'\ud83d\ude31',u'\ud83d\ude21'] key = getCoffeeKey(uid, date) queryCoffee = getCoffeeAmount(key) logging.info('key: ' + key) logging.info('queryCoffee.timesDrank: ' + str(queryCoffee)) logging.info('Coffees to add: {}'.format(coffees)) amount_drank = coffees if queryCoffee is not None: amount_drank += queryCoffee.timesDrank coffeeDrank = CoffeeStore(key=ndb.Key('CoffeeStore', key),timesDrank=amount_drank) coffeeDrank.put() if amount_drank == 3: str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '\nIt\'s your last one !' elif amount_drank > 3: str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '\nPlease don\'t drink anymore...' else: str_to_reply = name + ' drank ' + str(amount_drank) + ' coffee out of ' + str(3) + '.' str_to_reply += ' ' + smilies[min(amount_drank-1,len(smilies)-1)] logging.info('reply: ' + str_to_reply) return str_to_reply
def handle_foods(request_body, text): uid = myutils.extract_user_id(request_body) user_obj = userstore.get_max_calories(uid) reply = [] max_calories = 0 if user_obj == None: reply.append('Sorry, you didn\'t set max calories. Please use /set_max_calories command.') else: max_calories = user_obj.max_calories # Create a dict of foods where the custom foods override the default foods # where necessary. foods = get_foods(0) custom_foods = get_foods(uid) for food in custom_foods.items(): foods[food[0]] = food[1] calories_consumed = 0 logging.info(foods) logging.info(text) logging.info(len(text)) len_text = len(text) # Iterate through the message and try to find emojis for i in range(0, len_text): # Emojis can be one character long or two, so first check if it is # a single character emoji, if not try two characters. calories_consumed += get_food_calorie(text[i], foods) # Try two characters calories_consumed += get_food_calorie(text[i]+text[i+1], foods) if i < len_text-1 else 0 # If no food found, maybe it was only text or not food tracking at all. if calories_consumed == 0: return '' date = myutils.extract_date(request_body) key = getCaloriesKey(uid, date) calories_obj = getCalories(key) if calories_obj is not None: calories_consumed += calories_obj.calories update_obj = CalorieStore(key=ndb.Key('CalorieStore', key), calories=calories_consumed) update_obj.put() reply.append('Calories counted {}/{}'.format(calories_consumed, max_calories)) return '\n'.join(reply)
def update_coffee(request_body, params): logging.info('Inside updateCoffee...') if len(params) != 1 or not myutils.is_number(params[0]): logging.info('Params: {}'.format(params)) return 'Didn\'t fully understand. Should be like: /coffeeupd 2' name = myutils.extract_user_first_name(request_body) uid = myutils.extract_user_id(request_body) date = myutils.extract_date(request_body) amount = int(params[0]) key = getCoffeeKey(uid, date) coffeeDrank = CoffeeStore(key=ndb.Key('CoffeeStore', key),timesDrank=amount) coffeeDrank.put() return '{}, the coffee amount was updated.'.format(name)
def handle_water(request_body, text): waters = text.count(u'\U0001f376') if waters == 0: return '' uid = myutils.extract_user_id(request_body) date = myutils.extract_date(request_body) key = getWaterKey(uid, date) queryWater = getWaterAmount(key) logging.info('key: ' + key) logging.info('queryWater.timesDrank: ' + str(queryWater)) logging.info('Waters to add: {}'.format(waters)) amount_drank = waters if queryWater is not None: amount_drank += queryWater.timesDrank waterDrank = WaterStore(key=ndb.Key('WaterStore', key), timesDrank=amount_drank) waterDrank.put() return 'Water drank today: {} cups'.format(amount_drank)