def addUser(phone_number, URL=None): if SELF_SERVICE is False: # Users must be manually added return -1 if URL is None: sendTxt("User not found. Please reply with a Google Sheets URL.", phone_number) # Get Admin workbook book = getWorkbook(ADMIN_BOOK) # Get user sheet sheet = getSheet("users", book) # Check if phone number has tried to add itself already col_values = sheet.col_values(1) # First column lists users, second column is workbook URL if phone_number in col_values: cell = sheet.find(phone_number) # Update the URL of the user's book sheet.update_cell(cell.row, cell.col + 1, URL) else: # The user just got sent a message telling them to add the URL # Add them to the db, so they will be recognized when they respond depth = len(sheet.col_values(1)) + 1 sheet.update_cell(depth, 1, phone_number)
def getUserBook(phone_number): """Takes a phone number, returns phone number's workbook""" # Get Admin workbook book = getWorkbook(ADMIN_BOOK) # Get user sheet sheet = getSheet("users", book) # Check if phone_number is a valid user col_values = sheet.col_values(1) # First column lists users, second column is workbook URL if phone_number in col_values: cell = sheet.find(phone_number) # URL is adjacent column URL = sheet.cell(cell.row, cell.col + 1).value if URL[:4] == 'http': # If 'valid' URL try: book = getWorkbook(URL) except gspread.exceptions.SpreadsheetNotFound: # Workbook is not shared sendTxt('Workbook not found. Make sure you shared it with: ' + SHARE_EMAIL, phone_number) return -1 except gspread.exceptions.NoValidUrlKeyFound: # Workbook URL is invalid sendTxt("Workbook is invalid. Please reply with a valid Google Sheets URL.", phone_number) return -1 return book elif URL == '': # Field is empty pass else: # If not an existing user addUser(phone_number) return -1
def interpreter(content=None): """Takes a message object, containing: msisdn, recipient, message, id, timestamp""" if content is None or content['message'] == '': return 0 print content['message'] # Debugging # Add 1 to the front of the phone number (Nexmo requires it for sending SMS) # Makes this app USA only for the moment :( if content['msisdn'][0] != '1': # Append it l = [] l.append('1') l.append(content['msisdn']) content['msisdn'] = ''.join(l) # Message contains administrative commands if ADMIN is content['message'][0]: pass # If it is a request from a user to add themselves if NEW_USER == content['message'].lower(): gs.addUser(content['msisdn']) # Message contains expenses elif EXPENSE is content['message'][0]: # Clean the timestamp content['timestamp'] = content['timestamp'].split()[0] # Clean and normalize the message content['message'] = content['message'][1:].lower() # Get user workbook book = gs.getUserBook(content['msisdn']) if book != -1: # Book == -1 if user does not exist sheet = gs.getSheet(content['timestamp'], book) result = gs.commit(content, sheet) if 'malformed' == result: sendTxt( "New users, txt: 'add'\nAdd expense format:\n@<category>:cost <category>:cost", content['msisdn']) # If the message is a URL, assume the user is trying to add themselves # to the system. This also allows for updating one's workbook on the fly. elif 'http' == content['message'][:4]: gs.addUser(content['msisdn'], content['message']) sendTxt("Workbook added!\n Make sure you share it with " + SHARE_EMAIL, content['msisdn'])