Build a Shipment Tracking Tool with a Telegram Bot with Python
Design a Telegram Bot with Python that interacts with truck drivers, tracks your shipments and provides real-time visibility.
Retailers and FMCG companies face increasing pressure to improve their supply chain, increase flexibility and reduce costs.
Using Distribution Centers (DC) is an effective solution, as they allow a single location to stock a vast range of products from suppliers close to the final customer.
How can you automate transportation monitoring using Python?
However, it should be coupled with efficient shipment tracking to ensure transparency of delivery processes and measure performance.
In this article, we will design and deploy a transportation tracking tool using a Telegram bot built with Python.
I. Limitation of TMS & Tracking Tools
1. Scenario
2. What kind of obstacles can you face?
II. What do we need here?
III. What is a Telegram Bot?
1. Definition
2. How do you interact with a Telegram Bot?
IV. Create your Tracking Assistant Bot
1. Standard Operating Procedure (SOP)
V. Demo: Shipment Delivery confirmation
1. Step 1: DRIVER opens telegram and starts a discussion with BOT
2. Step 2: DRIVER shares its GPS Location (= Store Location)
3. Step 3, 4 and 5: Confirmation of the delivery with pictures
VI. Design your bot
VII. Conclusion
1. Add a Container Loading Optimization Module
Limitation of TMS & Tracking Tools
Scenario
You are a medium-sized retailer operating in a market where the transportation industry is immature.
You would like to implement a Transport Management System (TMS) proposed by an external provider to get full visibility and real-time tracking of your shipments.
What kind of obstacles can you face?
- System Interface: you have multiple carriers with different systems
- Maturity of Carriers: they manage their fleet with manual processes (Excel)
- Local Specifics: carriers do not operate their fleet (sub-contracting)
- Limited Capacity to Negotiate: because your volumes are low compared to other customers, carriers refuse to adapt their processes and use your system to record status
If you face these obstacles, the solution presented in this article will probably answer your needs.
What do we need here?
A simple, cheap and easy-to-implement solution to track your shipments that
- Does not require additional IT development for your carriers
- Easily integrates into the current transportation processes
- Reduces admin workload for your logistics team
- Does not impact drivers’ productivity
- Does not require additional equipment
- Provides visibility, real-time tracking and transparency
What is a Telegram Bot?
Telegram
Telegram is a cross-platform, secure, convenient messaging app that works on all devices.
With over 500 million users worldwide, Telegram has an open API and free protocol.
With this API, we can build our tools, including a bot.
What’s a Bot?
A bot is a 3rd party application that can interact with users by sending them messages, commands and inline requests. With Telegram, you can design and control your Telegram bot by sending HTTPS requests to their bot API.
Your bot can send/receive messages, photos, videos and GPS locations — enough to build our shipment tracking tool.
How do you interact with a Telegram Bot?
Add him and start a conversation :)
Create your Tracking Assistant Bot
Standard Operating Procedure(SOP)
For the first version of our bot, let's use a light version of SOP to record delivery time.
Confirm Unloading at Destination
- STORE TEAM finished unloading your shipment from the truck
- DRIVER shares Shipment Number, GPS location and a picture of the shipment with your Telegram BOT to confirm delivery
- Telegram BOT sends a delivery notification by email to your logistics team with the information listed above
Demo: Shipment Delivery confirmation
Your shipment has been unloaded in your store. DRIVER wants to send delivery confirmation before leaving for his next destination.
Step 1: DRIVER opens telegram and starts a discussion with BOT
Step 2: DRIVER shares its GPS Location (= Store Location)
Step 3: DRIVER shares a delivery number
Step 4: DRIVER shares a picture of the shipment
Step 5: Your logistics teams receive a shipment confirmation
🏫 Discover 70+ case studies using Python and VBA to automate manual tasks 🤖 and optimize your supply chain 🚚 in this Cheat Sheet
Find the source code in this repository 👇
Design your Telegram Bot
Create your bot with another bot :)
@ BotFather is a Telegram bot that will help you create your own bot
- Search @ BotFather on Telegram and send him ‘/start’
- Answer ‘/newbot’ message and follow the instructions to set up a name and a username
- Your bot is now ready, and an API token has been created. Please keep a copy of this token!
Installing Python packages
Package to manage your telegram bot
pip install python-telegram-bot
Import Libraries and enable logging
TOKEN: shared by @ BotFather when you created your bot
APP_NAME: address of your Heroku application (see steps below)
Main function with Conversation handler
To simplify the process for the DRIVER, your BOT will ask for information in sequence using the ConversationHandler function of the Telegram API
Example of photo function
- photo_file: extract the last photo uploaded in the messages written by the user
- link1: link created by Telegram API where we can extract photo file id
- link2: web page created by Telegram API that hosts this photo
- mail function: send an email with delivery_number, gps_location and embedded photo using HTML
For more details regarding Telegram API, check the list of reference links at the end of the article.
Mail Function
The transportation team will be notified by email after collecting the delivery number, GPS location, and shipment picture.
The bot uses the smtplib Python package and Gmail account.
You can find the mail function in the script mail.py.
Host your bot on the cloud.
Heroku is a cloud-based service that allows you to host your web application.
The free hosting provides enough resources to run your app.
Procfile
requirements.txt
Two files listed above are needed to set up your app on Heroku.
Conclusion
This is a very light version to show the potential of what can be done with a Telegram Bot.
We can improve it by adding
- Real-Time GPS Location Tracking
- Store records in a database /OR/Google sheet
- Add scanning function (Bar Code/QR Code) to avoid manual entry of delivery number by DRIVER.
- A function to get store and driver comments in case of late delivery?
- Seals management in case of shipping sensitive goods
What about maximizing trucks’ filling rate?
Containers Loading Optimization with Python
As freight costs increase, you are under more pressure than ever to maximize the use of your trucks and containers.
The picture above shows two containers loaded with two different strategies.
- The container on the right side used an optimized algorithm
- The left one has two pallets on the side that could not be loaded.
Could we implement the right solution in our telegram bot?
Yes, this algorithm built with Python using the 2d-knapsack problem maximizes the number of pallets loaded.
It can be easily included in the bot's workflow to help drivers maximize the trucks’ filling rate.
For more details, have a look at this article 👇
Can we use this tool for last-mile delivery optimization?
Last-mile delivery is a major operational challenge for e-commerce companies as it is costly and complex.
Optimize E-Commerce Last-Mile Delivery with Python
If you travel to first and second-tier cities of China, you will find small warehouses called customer service centres (客户服务中心) located in each neighbourhood.
Courrier drivers deliver final customers from there.
How can you use Python to optimize the routing from these centres?
In another article, I designed a solution to reduce costs and ensure a uniform workload distribution to each driver.
The bot can include this to allocate deliveries to drivers and create optimal routes.
For more information, check the article linked below 👇
About Me
Let’s connect on Linkedin and Twitter, I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.
For consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me via Logigreen Consulting.
If you are interested in Data Analytics and Supply Chain, look at my website.
💌 New articles straight in your inbox for free: Newsletter
📘 Your complete guide for Supply Chain Analytics: Analytics Cheat Sheet