A simple telegram bot that records and show spendings built using python (pyTelegramBotAPI).
To learn python! In the process create a nice tool that I wanted: quick and easy way to record expenses! Sure, there are better free and nicer looking mobile apps out there to track expenses with but sometimes it takes just too many taps/clicks to do something as simple as recording how much I ate for lunch or 'Food'. Note: This little bot is not designed or optimized to serve bazzillion or gazzilion users! It was a fun project shared among close friends and family - the amazing thing is you can take this code and customize it anyway you want it that fits your needs. Example the categories used for recording a new spending doesn't suit you, change it!
- Get api key from BotFather. Simply send
/newbotto "BotFather" in Telegram and follow the onscreen instructions to get an API key. Place this api key in"<API_KEY>"ofcoin_bot.pyfile. - Install the following dependencies:
pyTelegramBotAPI(eg. runpip install pyTelegramBotAPI) - Create an empty
data.jsonin the same directory of yourcoin_bot.py. All recorded expenses will be stored here. - Run
python3 coin_bot.py(ideally set it up in cloud or any free FaaS)
- Send
/helpcommand show help menu
- Send
/newcommand and select category - Key in amount to record spending
- Send
/showcommand and select mode (day or month)
- Send
/historycommand to view all spending history so far
data.json file is loaded on every read/write action into a global dictionary, with the following json key-value structure
"CHAT ID 1": [
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
...
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>"
],
"CHAT ID 2": [
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
...
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>"
],
"CHAT ID 2": [
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>",
...
"<DD-MMM-YYYY HH:MM>,<CategoryString>,<Amount>"
]
}Keys: String id that uniquely identifies user client. Values: String arrays comma seperated with date values, category strings, amount
The functionality to group category, sum of spending for that category, by day or month is done by
-
Defining query time or date or month
-
Enumerating dictionary key,values for query
dateFormat = '%d-%b-%Y' timeFormat = '%H:%M' monthFormat = '%b-%Y' #get all string array of ['Date','Category','amount'] for client id history = global_dictionary['CHAT_ID'] #prepare query string for specific date/time/month query = datetime.now().today().strftime(dateFormat) #enumerate key values to get a match of string array ['Date','Category','amount'] result = [value for index, value in enumerate(history) if str(query) in value] CALCULATE(result)
-
Suming up all categories
def CALCULATE(result) catSum = {} for row in result: s = row.split(',') # ['Date','Category','amount'] cat = s[1] # 'Category' # sum grouped category amounts if cat in catSum: #round up to 2 decimal catSum[cat] = round(catSum[cat] + float(s[2]),2) else: #first entry catSum[cat] = float(s[2])
This functionality is added because I wanted to have a quick overview the total amount of spending for each category (having a budget in mind) I could tell if I overspent or not.
Example: If I had spent $4 on breakfast, lunch and dinner respectively (adds up to $12) I could create a query for todays date and show a sum of all category. The result could be formatted in a simple text response from the bot as such:
Total spending for today:
CATEGORY, AMOUNT
----------------
Food: $12.0- delete (made a mistake and want to remove a spending record)
- Set a expenses goal and set notify/alert you if you are overspending
- Email spendings (eg. txt,csv or xls) to yourself





