Telegram tracking bot for sent packages.
It allows to add shipments and track them:
And it also allows to modify shipments once added:
I had ordered some stuff and I was very impatient. Then, I built this bot, which every 5 minutes sent me the updates (if any) without me having to press F5 constantlly.
In order to use this bot first clone the repo. Once done that, open the telegram-tracking-bot folder and run:
pip install -r requirements.txt
Finally run main.py using the telegram bot token as argument:
python main.py <TOKEN>
The telegram bot token must be created beforehand, following the instructions detailed here.
This app is divided into 4 files:
The first two files are self-explanatory. The last two are helper objects that have common functions to talk with the database and to connect to the API or scrape the providers websites.
I used sqlite3
since it comes with Python and this is a very small project.
The functions of this class are divided by its purpose:
- add
- get
- delete
This database doesn't natively allow concurrency, which means it can't be called from another thread, which is the case of the bot. So this option must be added:
self.conn = sqlite3.connect('database.db', check_same_thread=False)
To be completely sure there's no corruption, a lock mechanism is implemented, which looks something like this:
def add_tracknum(self, chat_id, tracknum, company, name):
"""
Adds tracknum to database
"""
with self.lock, self.conn:
self.cursor.execute(
"INSERT INTO track_nums VALUES (:user, :tracknum, :company, :name)",
{
'user': chat_id,
'tracknum': tracknum,
'company': company.lower(),
'name': name
}
)
The Providers class has the functions to scrape the OCA website (for now) and return the information parsed.
The Scheduler class creates scheduling jobs for each tracking that check every given time for new information.
The Bot class was created using the python-telegram-bot library.
- Python
- Agustin Aon - @aon