code package

Submodules

code.bot module

File contains bot message handlers and their associated functions

code.bot.category_add(message)

Handles the command ‘categoryAdd’ and then displays a message prompting the user to enter the category name. The function ‘receive_new_category’ is called next.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.category_delete(message)

Handles the command ‘categoryDelete’. Lists all categories from which the user can choose a category to delete.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.category_list(message)

Handles the command ‘categoryList’. Lists all categories.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.command_add(message)

Handles the command ‘add’. Lists the categories from which the user can select. The function ‘post_category_selection’ is called next. :param message: telebot.types.Message object representing the message object

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.command_budget(message)

Handles the commands ‘budget’. Gets input for budget and stores it.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.command_delete(message)

Handles the ‘delete’ command. The user is then given 3 options, ‘day’, ‘month’ and ‘All” from which they can choose. An error message is displayed if there is no transaction history. If there is transaction history, the execution is passed to the function ‘process_delete_argument’.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.command_display(message)

Handles the command ‘display’. If the user has no transaction history, a message is displayed. If there is transaction history, user is given choices of time periods to choose from. The function ‘display_total’ is called next.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.create_header(user)

Creates the header for the calender

Parameters

user – the user

Returns

the header row

code.bot.csv_callback(call)

This function is used to handle the callback with the data received from the user pressing a category option for the transactions that the bot read from the csv file but did not know how to categorize.The callback object contains the category the user choose for that particular transaction.

Parameters

call – telegram.CallbackQuery representing the callback object

Type

object

Returns

None

code.bot.display_total(message)

Receives the input time period and displays the transaction summary for the corresponding time period.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit1(message)

Handles the command ‘edit’ and then displays a message explaining the format. The function ‘edit_list2’ is called next.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit3(message)

Receives the user’s input corresponding to what they want to edit, and then transfers the execution to the function according to the choice.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit_cat(message)

This function is called if the user chooses to edit the category of a transaction. This function receives the new category and updates the transaction.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit_cost(message)

This function is called if the user chooses to edit the amount of a transaction. This function receives the new amount and updates the transaction.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit_date(message)

This function is called if the user chooses to edit the date of a transaction. This function receives the new date and updates the transaction.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.edit_list2(message)

Parses the input from the user message, and finds the appropriate transaction. Asks the user whether they want to update the date, value, or category of the transaction, and then passes control to edit3 function

Parameters

message – the message sent of the transaction

Returns

None

code.bot.get_calendar_buttons(user)

Gets the calendar buttons for each numeric day

Parameters

user – the user to add to

Returns

the rows of calendar buttons

code.bot.get_chart(message)

Handles the chart command. When the user runs this command the bot will create a piechart of all the transactions and their categories and post that to the chat

Parameters

message

Returns

None

code.bot.get_users()

Reads data and returns user list as a Dict

Returns

users

Return type

dict

code.bot.handle_budget_document_csv(message)

This function is called if the user inputs a csv file that contains their budget in a csv format with column names date, description, and debit. The function reads the csv file and then for transactions that the bot does not know how to categorize, it sends a message to the user asking how they would like for that transaction to be categorized.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.handle_confirmation(message, records_to_delete)

Deletes the transactions in the previously chosen time period if the user chooses ‘yes’.

Parameters
  • message – telebot.types.Message object representing the message object

  • records_to_delete – the records to remove

Type

object

Returns

None

code.bot.handler_callback(callback, user)

A method for handling callbacks

Parameters
  • user – user object

  • callback – callback from telebot.types.CallbackQuery

Returns

datetime.date object if some date was picked else None

code.bot.is_add_callback(query)
code.bot.is_csv_callback(query)

Callback to identify if the button pressed was from the csv function

Parameters

query – the button pressed

Returns

if the button pressed relates to the csv

code.bot.post_amount_input(message, date_of_entry)
Receives the amount entered by the user and then adds to transaction history. An error is displayed if the entered

amount is zero. Else, a message is shown that the transaction has been added.

Parameters
  • date_of_entry – user entered date

  • message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.post_budget_input(message)

Receives the amount entered by the user and then adds it to the monthly_budget attribute of the user object. An error is displayed if the entered amount is zero. Else, a message is shown that the budget has been added. :param message: telebot.types.Message object representing the message object.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.post_category_selection(message, date_to_add)

Receives the category selected by the user and then asks for the amount spend. If an invalid category is given, an error message is displayed followed by command list. IF the category given is valid, ‘post_amount_input’ is called next.

Parameters
  • message – telebot.types.Message object representing the message object

  • date_to_add – the date of the purchase

Type

object

Returns

None

code.bot.post_date_selection(message)

Once a date is selected, this function is called and queries the user to enter a category

Parameters

message – the message sent after the user clicks a button

Returns

None

code.bot.process_delete_argument(message)

This function receives the choice that user inputs for delete and asks for a confirmation. ‘handle_confirmation’ is called next.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.receive_delete_category(message)

Checks whether the selected category can be deleted and calls user.delete_category if the category can be deleted.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.receive_new_category(message)

This function receives the category name that user inputs and then calls user.add_category which appends the category to the existing category list.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.show_history(message)

Handles the command ‘history’. Lists the transaction history.

Parameters

message – telebot.types.Message object representing the message object

Type

object

Returns

None

code.bot.start_and_menu_command(m)

Handles the commands ‘start’ and ‘menu’. Sends an intro text.

Parameters

m – telebot.types.Message object representing the message object

Type

object

Returns

None

code.user module

File contains functions that stores and retrieves data from the .pickle file and also handles validations

class code.user.User(userid)

Bases: object

add_category(new_category, userid)

Stores the category to category list.

Parameters
  • new_category – name of the new category

  • userid – userid string which is also the file name

Type

string

Type

string

Returns

None

add_monthly_budget(amount, userid)

Given amount and userid, edit the budget of the current user

Parameters
  • amount – budget amount

  • userid

Returns

None

add_transaction(date, category, value, userid)

Stores the transaction to file.

Parameters
  • date – date string of the transaction

  • category – category of the transaction

  • value – amount of the transaction

  • userid – userid string which is also the file name

Type

string

Type

string

Type

string

Type

string

Returns

None

create_chart(userid)

This is used to create the matplotlib piechart of all the transactions and their categories. If a category does not have any transactions, then it is not included in the piechart

Parameters

userid

Returns

filepath to the image created by matplotlib

create_rules_and_add_unknown_spending(category, description, date, value, userid)

This function is used to remember how an user categorized a certain transaction, so that the next time the bot sees the transaction the bot will be able to categorize it automatically.

Parameters
  • category – category of the transaction

  • description

  • date

  • value

  • userid

Type

string

Type

string

Type

Datetime object

Type

float

Type

string

Returns

None

deleteHistory(records=None)

Deletes transactions

Parameters

records – list of records to delete.

Type

array

Returns

None

delete_category(category, userid)

Removes the category from category list.

Parameters
  • category – name of the category to be removed

  • userid – userid string which is also the file name

Type

string

Type

string

Returns

None

display_transaction(transaction)

Helper function to turn the dictionary into a user-readable string

Parameters

transaction – dictionary of category, and each value is a dictionary of transactions of that category

Returns

final_str, which is the transactions stringifies

Return type

string

edit_transaction_category(new_category)

Updates the edited transaction with the new category.

Parameters

new_category – the new category of the transaction.

Type

string

Returns

True

Return type

bool

edit_transaction_date(new_date)

Returns the edited transaction with the new date.

Parameters

new_date – the new date of the transaction.

Type

string

Returns

transactions dict

Return type

dict

edit_transaction_value(new_value)

Returns the edited transaction with the new value.

Parameters

new_value – the new value of the transaction.

Type

string

Returns

transactions dict

Return type

dict

get_number_of_transactions()

Helper function to get the total number of transactions across all categories

Returns

number of transactions

Return type

int

get_records_by_date(date: None.datetime.date, is_month: bool)

Given a date and chat_id returns all records that match the filter If is_month is true, only matches year and month, not day

Parameters
  • date – date for filtering records

  • is_month – this parameter is true if records for a month are taken

Type

datetime.date

Type

bool

Returns

matched_dates which is the array of records for that day or month

Return type

array

monthly_total()

Calculates total expenditure for the current month

Returns

total_value - rounded amount if valid, else 0.

Return type

float

read_budget_csv(file, userid)

This function reads the csv file passed to the bot by the user into a Pandas Dataframe. It goes through each transaction, and checks if it knows how to categorize that transaction. If it does, it will add the transaction to the user history.

Parameters
  • file – csv file sent to the telegram bot

  • userid – chat id of the conversation

Returns

df pandas dataframe that contains all of the transactions that the bot could not categorize by itself

Return type

Dataframe

save_user(userid)

Saves data to .pickle file

Parameters

userid – userid string which is also the file name

Type

string

Returns

None

store_edit_transaction(existing_transaction, edit_category)

Assigns the transaction and category to be edited.

Parameters
  • existing_transaction – the transaction which the user chose to edit

  • edit_category – the existing category of the transaction

Type

string

Type

string

Returns

None

validate_date_format(text, date_format)

Converts the inputted date to the inputted date format

:param text has the date which is to be converted :type: string :param date_format has the format to which the conversion should be done :type: string :return: date, contains the formatted date :rtype: datetime.dateime

validate_entered_amount(amount_entered)

Validates that an entered amount is greater than zero and also rounds it to 2 decimal places.

Parameters

amount_entered – entered amount

Type

float

Returns

rounded amount if valid, else 0.

Return type

float