def start(bot, update): ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[(n / 10 % 10 != 1) * (n % 10 < 4) * n % 10::4]) chat_id = update.message.chat_id user_id = update.message.from_user.id user_obj = bot.getChat(chat_id) ancestor_key = ndb.Key('Account', user_id or '*notitle*') curr_user = Account.query_users(ancestor_key).fetch() if not curr_user: account = create_entity_using_keyword_arguments(user_obj, user_id) account.key = ndb.Key('Account', user_id) account.put() # ancestor_key = ndb.Key('Account',user_id or '*notitle*') curr_user = Account.query_users(account.key).fetch() bot.sendMessage( chat_id=chat_id, text="Welcome to the Booking Bot, {}.\n How many people?".format( user_obj["first_name"])) else: user = ancestor_key.get() user.entries += 1 user.put() bot.sendMessage( chat_id=chat_id, text= """Welcome again to the Booking Bot {}.\nThis is your {} time here. \nHow many people?""".format(user_obj["first_name"], ordinal(user.entries)))