How I Use A Telegram Bot To Track My Expenses – Telegram Group

How I Use A Telegram Bot To Track My Expenses

That’s right — I don’t use a mobile app to track my spending. Most of them require too many steps to add 1 new expenditure item. (Maybe I just haven’t found the right mobile app yet, and you can recommend one to me!) As such, I’ve created my own spending tracker using Telegram bot api and Python, and still use it to this day. In this article, we’ll run through how we can create a Telegram bot to help track our finances.

An Overview Of How This Works

There are 2 players here — 1) our Telegram bot and 2) A separate Telegram channel where we record our expenditure.

  • Whenever we spend money on something, we record it in our spending channel (not the Telegram bot)
  • Whenever we want to get our spending insights, we forward the spending record for the month to our Telegram bot, which will then give us the insights.

Format of Recording Expenditure (Expenditure Record Channel)

Dec 2021
1, lunch 5, drinks 2, dinner 8
2, lunch 6.5, dinner 8
3, groceries 30
4, nothing 0
5, pizza 12, dinner 7

This is the message that we 1) store in our expenditure record Telegram channel and 2) forward to our Telegram bot. The first line refers to the month, while the subsequent lines refer to expenditure for the days of the month. For each line, entries are separated by commas — the first entry refers to the day of the month, while the rest refer to expenditure for various items.

This format works for me, but might not work for you — feel free to change it up to whichever format you’re comfortable with.

What our Telegram Bot Replies

Period: Dec 2021
Total Spent: 78.5
Average per day: 15.7[(1, 15.0), (2, 14.5), (3, 30.0), (4, 0.0), (5, 19.0)]

This will be what our Telegram bot replies us with after we forward the above message to it. The last line refers to the total amount spent for each day, and I chose this format so I can see everything easily in 1 message without having to scroll. If this format does not work for you, feel free to change it to whatever your like!

An Example

Note: You’ll need either 1) A computer that’s on 24/7 to run your Telegram bot or 2) A cloud server you can ssh into (AWS works here)

1) Creating A Private Telegram Channel To Record Your Expenditure

There’ll be no Telegram bot connected to this channel, and this channel is solely used to record our expenditure. Let’s name this channel My Expenditure or whatever you wish to call it.

2) Creating A Telegram Bot + Getting The Token

  1. Search for BotFather in your Telegram Search bar
  2. Type /newbot, which will create a new Telegram bot
  3. Give your Telegram bot a name & nickname
  4. Take note of the token (censored in red). We will need this later.

3) Coding A Basic Telegram Bot In Python

  1. Install python-telegram-bot using pip
pip install python-telegram-bot # Windows
pip3 install python-telegram-bot # MacOS

2. Create 1 Python script (.py file) — we only need a single Python script here.

3. Add the following code into your Python script

from telegram.ext import *TOKEN = "INSERT TOKEN HERE (found in BotFather's chat)"def route(update, context):
text = update.message.text
update.message.reply_text(text)updater = Updater(TOKEN, use_context=True)
updater.dispatcher.add_handler(MessageHandler(Filters.text, route))
updater.start_polling()print()
print("Your telegram bot is running!")
print()updater.idle()

Note: this code is not complete yet

4. Run the code and you should see this:

5. Send something to your Telegram bot. Your Telegram bot should reply you with whatever you sent to it.

What we are doing here is to verify that our Telegram bot is 1) able to receive the messages that we send and 2) able to reply us. Once we have verified this, we can move on to adding functionality to calculate our spending insights.

4) Adding Functionality To Calculate Spending Insights To Our Telegram Bot

In the previous code block, this line is the line that makes our Telegram, bot reply to us. Here, text essentially refers to the stuff that we send our bot. Now, we need to write a function to process text and calculate our spending insights:

Our Input Text

text = """Dec 2021
1, lunch 5, drinks 2, dinner 8
2, lunch 6.5, dinner 8
3, groceries 30
4, nothing 0
5, pizza 12, dinner 7"""

Our input is essentially our spending record for the entire month, and is equivalent to the above text.

The Updated (Final) Code

Run your Python script, try to send some expenditure records to your Telegram bot and see if it replies you with the expected output.

5. Deploying Your Python Telegram Bot

Even if you run your Telegram bot locally on your computer, you can send a message to it, and your Python code should be able to handle it for you. If you want your Telegram bot to work 24/7, you need to let your code run 24/7. Here, we can either run this on a computer and keep it on 24/7, or we can choose to use AWS EC2 or some other cloud server.

Once we set up our EC2 server (or whichever server you’re using), we just need to copy our Python script to our server, run it and leave it. Make sure to close the SSH connection to your server and test if your Telegram bot still works.

Conclusion

This is probably the longest article I’ve ever written lol. If this article provided value and you wish to support me as a writer, do consider signing up for a Medium membership — It’s $5 a month, and you get unlimited access to stories on Medium. If you sign up using my link, I’ll earn a small commission.

Click here for unlimited Medium access

If you wish to get notified whenever I publish, do consider joining my email list.

Get an email whenever Zlliu publishes.

Get an email whenever Zlliu publishes. By signing up, you will create a Medium account if you don't already have one…

zl-liu.medium.com

Ten articles before and after

How To: Create a Java Telegram Bot – Telegram Group

How To: Deploy Java Telegram Bot to Heroku – Telegram Group

Serverless Telegram bot with Kotlin, Firebase and Google Cloud Functions – Telegram Group

TRX1 Dev Blog #11 (November 2021) – Telegram Group

Telegram Bot 跟我想的不一樣. 第一天進公司時,Slack就被邀進同事們訂便當的群組,在上面會宣布今天訂那一家便… – Telegram Group

CryptoChat: Un chatbot hecho con rasa sobre criptomonedas – Telegram Group

data-rh=”true”>Share your Ref link to your friends:
t.me/metamusk_airdrop_bot?start=21FF1F54D851B44DBAB310ABA260150F
? Metamusk is a native token issued to support Elon Musk and his great projects on the way to… – Loiscwai – Medium – Telegram Group

The Indra Capital Ecosystem — Part II— IndraX, YieldWallet.io and Airavat – Telegram Group

What is telegram messenger?. Telegram is a messenger application… – Telegram Group

ZipBot: Telegram Bot que Compacta Arquivos – Telegram Group