Build a Shipment Tracking Tool using a Telegram Bot

Design a Telegram Bot that will interact with truck drivers to track your shipments and provide real-time visibility of your transportation performance

Samir Saci
Towards Data Science

--

Build a Shipment Tracking Tool using a Telegram Bot
(Image by Author)

Are you a retailer or FMCG company struggling to meet supply chain demands?

Distribution Centers can help, but effective shipment tracking is key to success.

Implementing a simple, cheap, and easy-to-use solution can reduce admin workload, gain real-time visibility, and more.

This article will show you how to build a Shipment Tracking Tool using a Telegram Bot.

Introduction

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.

However, it should be coupled with efficient tracking of the shipments to provide stores with accurate ETAs, ensure transparency of delivery processes and measure performance.

💌 New articles straight in your inbox for free: Newsletter
📘 Your complete guide for Supply Chain Analytics: Analytics Cheat Sheet

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. Telegram
2. What's a Bot?
3. 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. Generative AI: GPT-Powered Tracking Bot
2. Next Steps

Edit: You can find a Youtube version of this article with animations in the link below.

I. 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.

II. 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
Telegram Bot: your best ally to improve your logistics performance (Image by Ichi.pro)

III. What is a Telegram Bot?

Telegram

Telegram is a secure, convenient messaging app that’s cross-platform, meaning it works on all your devices.

With over 500 million users worldwide, Telegram has an open API and protocol that is free for everyone. 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 :)

IV. Create your Tracking Assistant Bot

Standard Operating Procedure(SOP)

For the first version of our bot, let us take a light version of SOP to record delivery time.

Confirm Unloading at Destination

  1. STORE TEAM finished unloading your shipment from the truck
  2. DRIVER shares Shipment Number, GPS location and a picture of the shipment with your Telegram BOT to confirm delivery
  3. Telegram BOT sends a delivery notification by email to your logistics team with the information listed above

V. 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

DRIVER starts the process | BOT asks for GPS location — (Image by Author)

Step 2: DRIVER shares its GPS Location (= Store Location)

DRIVER share its GPS Location | BOT asks for the delivery number — (Image by Author)

Step 3: DRIVER shares a delivery number

DRIVER share its Delivery Number | BOT asks for shipment picture — (Image by Author)

Step 4: DRIVER shares a picture of the shipment

DRIVER share shipment photo | BOT stop the process and send an email (Image by Author)

Step 5: Your logistics teams receive a shipment confirmation

BOT send delivery confirmation by mail to the transportation team (Image by Author)

💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.

You can find the full code in my GitHub repository: Link
My portfolio with other projects: Samir Saci

VI. Design your Telegram Bot

Create your bot with another bot :)

@ BotFather is a Telegram bot that will help you create your own bot

  1. Search @ BotFather on Telegram and send him ‘/start’
  2. Answer ‘/newbot’ message and follow instructions to set up a name and a username
  3. Your bot is now ready, with an API token created — please keep a copy of this token!
Create your Telegram in three steps with @ BotFather (Image by Author)

Installing Python packages

Package to manage your telegram bot

pip install python-telegram-bot

Import Libraries and enable logging

Import Libraries and Init your Telegram Bot

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

Build a Shipment Tracking Tool using a Telegram Bot
Shipment Information Collection Process handled by ConversationHandler Function of Telegram API (Image by Author)
Conversation Handler creates a sequence of instructions for the driver with the cancel function

Example of photo function

  1. photo_file: extract the last photo uploaded in the messages written by the user
  2. link1: link created by Telegram API where we can extract photo file id
  3. link2: web page created by Telegram API that hosts this photo
  4. mail function: send an email with delivery_number, gps_location and embedded photo using HTML
Example of photo function

For more details regarding Telegram API, I strongly suggest checking the list of reference links at the end of the article.

Mail Function

After collecting the Delivery Number, GPS Location and shipment picture, your bot will send a notification by email to your transportation team using 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 where you can host your web application; the free hosting formula provides enough resources to run your app.

Procfile
requirements.txt

Two files listed above are needed to set up your app on Heroku.

You can find detailed instructions on deploying your Telegram bot on Heroku in this good medium article by Haohui (Link).

Conclusion

Generative AI: Telegram Bot Powered by GPT

In November 2022, OpenAI released the first version of ChatGPT.

This became the opportunity to improve the user experience of data-driven products using Large Language Models.

Supply Chain Control Tower Agent with LangChain SQL Agent [Article Link] — (Image by Author)

I started with this article, sharing my explorative journey of LLMs used to create an autonomous “Supply Chain Control Tower”.

The architecture of this GPT-powered Control Tower — (Image by Author)

The LangChain Agent can answer operational questions by querying a database, analysing the results and using GPT intelligence to interpret and interact with users.

💡 Idea: Create a GPT-powered Tracking Bot

  • Connect the bot to the Transportation Management System
  • Store the data created in the bot in an SQL database
  • Add a LangChain Agent on top of it

If you need more details on how to implement this kind of solution,

Next Steps

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

Do you have any ideas on functionalities that could be added?

Please drop a comment, and let’s try to build a better version!

About Me

Let’s connect on Linkedin; I am a Supply Chain Engineer using data analytics to improve logistics operations and reduce costs.

If you are interested in Data Analytics and Supply Chain, have a look at my website

--

--

Top Supply Chain Analytics Writer — Follow my journey using Data Science for Supply Chain Sustainability 🌳 and Productivity ⌛