def is_spliwise_connected(update): key = str(update.effective_user.id) print_app_log( logger, update, f"Command: {update.message.text} Checking for {key} in redis") return redis.hexists(key, 'oauth_token') and redis.hexists(key, 'oauth_token_secret')
def cancel_create_expense(update, context): print_app_log(logger, update, "Canceling the create expense") query = update.callback_query context.user_data[STATE_COMPLETE] = True context.bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text='No expense created')
def get_all_expenses(update): friends_with_expenses = splitwise.get_friends_with_expenses() print_app_log(logger, update, "In get all expenses") output = f'{splitwise.get_lend_expenses(friends_with_expenses)}' \ f'\n\n{splitwise.get_borrowed_expenses(friends_with_expenses)}' return output
def help(update: Update, context: CallbackContext): print_app_log(logger, update, "Running help command") help_text = '<b>The following commands are available:</b>\n\n' help_text += ''.join([ f'<b>{command}:</b> <i>{commands[command]}</i>\n' for command in commands ]) # send the generated help page update.message.reply_text(help_text, parse_mode=ParseMode.HTML)
def set_access_token(logger, update, context): key = str(update.effective_user.id) if is_spliwise_connected(update): print_app_log(logger, update, f"Found user_id: {key} in redis") splitwise.setAccessToken(redis.hgetall(key)) else: print_app_log(logger, update, f"Not able to found user_id: {key} in redis") raise Exception
def list_expense(update: Update, context: CallbackContext): try: set_access_token(logger, update, context) print_app_log(logger, update, "Listing all expenses") output = get_all_expenses(update) print_app_log(logger, update, "Listed all expenses") update.message.reply_text(output, parse_mode=ParseMode.HTML) except Exception: send_account_not_connected(update, context)
def create_expense(update, context): try: set_access_token(logger, update, context) print_app_log(logger, update, "Initializing create expense") friends = splitwise.getFriends() reply_markup = InlineKeyboardMarkup( get_keyboard_layout(splitwise, friends, column_size=3)) update.message.reply_text('Create new expense with', reply_markup=reply_markup) return TAKE_INPUT except Exception: send_account_not_connected(update, context)
def create_new_expense(update, context): query = update.callback_query friend_id, name, amount, description = context.user_data[NEW_EXPENSE] print_app_log( logger, update, f"creating new expense with {name}, of amount {amount} and description {description}" ) self_id = splitwise.getCurrentUser().getId() splitwise.create_expense_object(self_id, friend_id, amount, description) context.user_data[STATE_COMPLETE] = True context.bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text='New expense created!')
def received_input(update, context): try: query = update.callback_query amount, description = update.message.text.split(" ") try: amount = float(amount) except ValueError as e: raise InvalidAmountError(amount) if amount <= 0: raise InvalidAmountError(amount) if description is None or description == "": raise InvalidDescriptionError(description) print_app_log( logger, update, f"Input received correctly Amount: {amount}, Description: {description}" ) context.user_data[NEW_EXPENSE].extend([amount, description]) id, name, amount, description = context.user_data[NEW_EXPENSE] update.message.reply_text( f'Expense to be created with <b>{name}</b> with amount <b>₹{amount}</b>' f' and description <b>{description}</b>', parse_mode=ParseMode.HTML) text = 'Are you sure you want to proceed?' confirm(update, context, text, True) return CONFIRM except InvalidAmountError as e: print_app_log(logger, update, f"Invalid Amount: {e.amount} entered in create expense") update.message.reply_text( f"Invalid Amount: {e.amount} entered. Please input again.") return TYPING_REPLY except InvalidDescriptionError as e: print_app_log( logger, update, f"Invalid Description: {e.description} entered in create expense") update.message.reply_text( f'Invalid Description: {e.description} entered in create expense. Please input again.' ) return TYPING_REPLY except ValueError as e: # when only one string is provided print_app_log(logger, update, f"Invalid input") update.message.reply_text(f'Invalid input. Please input again.\n\n' f'Valid Format:\n' f'Amount Description') return TYPING_REPLY
def create_settlement(update, context): query = update.callback_query name, amount, friend_id = context.user_data[SETTLE_EXPENSE] print_app_log(logger, update, f"Settling expense with {name} of amount {amount}") self_id = splitwise.getCurrentUser().getId() splitwise.create_expense_object( self_id, friend_id, amount, 'Settling the expense' ) context.user_data[STATE_COMPLETE] = True context.bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text='Expense settled!' )
def take_friend_input(update, context): query = update.callback_query friend_id = int(query.data) _id_name_mapping = splitwise.get_id_name_mapping() _id_amount_mapping = splitwise.get_id_amount_mapping() name = _id_name_mapping[friend_id] amount = _id_amount_mapping[friend_id] print_app_log(logger, update, f"Confirming to settle expense with {name} of amount {amount}") context.user_data[SETTLE_EXPENSE] = [name, amount, friend_id] text = f'Settle balance with {name} of ₹{amount}?' confirm(update, context, text, False) return CONFIRM
def take_input(update, context): print_app_log(logger, update, "Waiting for user to enter input for creating expense") query = update.callback_query friend_id = int(query.data) _id_name_mapping = splitwise.get_id_name_mapping() name = _id_name_mapping[friend_id] context.user_data[NEW_EXPENSE] = [friend_id, name] context.bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text=f'Enter amount and description for expense with <b>{name}.</b>\n' f'Use space to separate values.', parse_mode=ParseMode.HTML) return TYPING_REPLY
def settle_expense(update, context): try: set_access_token(logger, update, context) print_app_log(logger, update, "Initializing settle expense") friends_with_expenses = splitwise.get_friends_with_expenses() borrowed_friends = [friend for friend in friends_with_expenses if float( friend.getBalances()[0].getAmount()) < 0] reply_markup = InlineKeyboardMarkup( get_keyboard_layout(splitwise, borrowed_friends)) update.message.reply_text( 'Settle with', reply_markup=reply_markup ) return TAKE_FRIEND_INPUT except Exception: send_account_not_connected(update, context)
def connect(update: Update, context: CallbackContext): if is_spliwise_connected(update): print_app_log( logger, update, "Trying to connect, but the splitwise account is already connected" ) update.message.reply_text( f"You have already connected your Splitwise account.\n" f"Run /reconnect command if you want to connect to your account again." ) return else: print_app_log(logger, update, "Connecting to splitwise account") url, secret = splitwise.getAuthorizeURL() context.user_data['secret'] = secret update.message.reply_text( "Please click on this link, and Authorize the application.") update.message.reply_text(url)
def reconnect(update: Update, context: CallbackContext): if is_spliwise_connected(update): key = str(update.effective_user.id) print_app_log(logger, update, f"Reconnecting user_id: {key}") redis.delete(key) update.message.reply_text( f"Successfully disconnected from your previous account.\n") connect(update, context) return else: print_app_log( logger, update, "Reconnecting to splitwise account, but the account is not connected" ) update.message.reply_text( f"Your account is not connected.\n" f"Run /connect to connect your Splitwise account.")
def start(update: Update, context: CallbackContext): try: tokens = context.args[0].split('-') oauth_token, oauth_token_secret = tokens access_token = splitwise.getAccessToken(oauth_token, context.user_data['secret'], oauth_token_secret) # dictionary with oauth_token and oauth_token_secret as keys, # these are the real values for login purposes splitwise.setAccessToken(access_token) key = str(update.effective_user.id) value = access_token redis.hmset(key, value) print_app_log( logger, update, f"Storing user_id: {key} and access_token: {value} in redis") print_app_log(logger, update, "Splitwise account connected successfully") update.message.reply_text( emojize( "Splitwise account connected successfully.\nNow manage your money effectively! :moneybag: ", use_aliases=True)) except IndexError: print_app_log(logger, update, "Started the conversation") update.message.reply_text(f""" Hello <b>{update.effective_user.first_name}</b>! Welcome to Splitwize Bot 🤖! I'll be managing your Splitwise account. Run /help command to check what all things I can do. Run /connect command to connect your Splitwise account. """, parse_mode=ParseMode.HTML) except Exception: send_account_not_connected(update, context)