spndr - I.M. based spending tracker
A simple spending tracker that can be used from an instant messenger. Built using Python (Flask), PostgreSQL and Redis. Currently supports Telegram.
Table of Contents
Requirements and Setup
Software Requirements
- OS - Ubuntu Linux
- Python 3.8.5
- PostgreSQL 12.5
- Redis 6.0.9
- Ngrok 2.3.35
Python Dependencies
- flask
- flask-bcrypt
- email-validator
- psycopg2
- redis
- requests
Setup
- Make sure you have PostgreSQL and Redis installed and running in the background.
- Start an ngrok tunnel to your localhost on your preferred port, for example, a public url to port 5000 on your local host can be created as such:
./ngrok http 5000
- Copy the https forwarding link that is generated:
ngrok by @inconshreveable (Ctrl+C to quit)
Session Status online
Account ##################### (Plan: Free)
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://############.ngrok.io -> http://localhost:5000
Forwarding https://############.ngrok.io -> http://localhost:5000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
- create a file; config.py in the repo and add the following variables to it:
- bot_token: A string containing the Telegram API key for the bot. Click this link to see how you can get an API key: https://core.telegram.org/#bot-api
- user: A string containing the username of your PostgreSQL server.
- password: A string containing the password of your PostgreSQL server.
- host: the hostname (string) for telegram to push updates to via a webhook, should be localhost for now.
- webhook_url: the url (string) obtained in step 4 from ngrok
- webhook_port: the same port (integer) used in step 4
'''
config.py
CONFIGURATION FILE FOR SPNDR
'''
bot_token = 'bot API token here'
user = 'PostgreSQL server username here'
password = 'PostgreSQL password here'
host = 'localhost'
webhook_url = 'ngrok https link here'
webhook_port = 1234 #Replace 1234 with your port number
- Run db_reset.py to initiallize the Postgres database.
- Run initiallize_webhook.py to send telegram the webhook url which is the ngrok url generated in step 4
- Run webhook_endpoint_starter.py in the background
- Run main_webhook.py
- The app is up and running! Try sending your bot a message.
Working
The operation of the bot is fairly simple. Whenever someone sends a message to the bot. Telegram forwards the message to us via the ngrok webhook url. On our localhost, the message is pushed into a Redis queue via the flask app defined in webhook_endpoint_starter.py . The message is popped out of the Redis queue and processes by main_webhook.py. Any necessary backend work is done and a reply is sent back to the sender on telegram. The above process continues in a loop.
Modules and Functionality
The app has 4 main modules that are called when main_webhook.py is run:
- api_engine: This module contains the files that are required to set up the webhook, send replies to telegram and parse received updates from telegram.
- app_engine: This module is the working brain of the app, the business logic of the app is defined here.
- db_engine: This module contains the files necessary for interfacing with PostgreSQL databases and Redis.
- replies_engine: This module contains the necessary templates for generating replies to send back to the telegram users.