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

Build a Shipment Tracking Tool using a Telegram Bot
Photo by Kendall Henderson / Unsplash

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

Article originally published on Medium.

Retailers and FMCG companies are facing increasing pressure to improve their supply chain, increase their 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 coming from different 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.

I. Limitation of TMS & Tracking Tools


Example

You are a medium-size retailer operating in a market where the transportation industry is not mature.

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 own 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 are facing 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 of your devices. With more than 500 million users worldwide, Telegram has an open API and protocol free for everyone. With this API we can build our own 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 to interact with a Telegram Bot?

Add him and start a conversation :)

IV. Create your Tracking Assistant Bot


Standard Operating Model (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
Edit: You can find a Youtube version of this article with animations in the link below.

V. Demo: Shipment Delivery confirmation


Scenario

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)
🔗
You can find the full code in my GitHub repository: Link

VII. Design your Telegram Bot


1. 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)

2. Installing Python packages

Package to manage your telegram bot

pip install python-telegram-bot

3. Import Libraries and enable logging

TOKEN: shared by @ BotFather when you created your bot
APP_NAME: address of your Heroku application (see steps below)

4. 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 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)

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

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

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

6. Host your bot on the cloud

Heroku is a cloud-based service where you can host your web application; the free formula of hosting provides enough resources to run your app.

Procfilerequirements.txt

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

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

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 comments and driver comments in case of late delivery?
  • Seals management in case of shipping sensitive goods

About Me

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

References

Many details were skipped in this article to make it concise and easy to read. You can find detailed instructions in the excellent articles listed below.

[1] — Dzaky Widya, Learn to build your first bot in Telegram with Python, freecodecamp.org

[2]— Haohui, How to Deploy a Telegram Bot using Heroku for FREE, Medium

[3] — Ng Wai Foong, How to Create Telegram Bot in Python, Medium

[4]Telegram API Documentation