Delivery Notification
Charly's Column – Telegram API
Our columnist Charly K¸hnast doesn't want to be out when a package delivery service is standing at his front door with some goodies. When the courier rings, he can have the Telegram API send a notification to his mobile phone.
I mainly find notifications on my mobile phone annoying and have disabled most of them. But things are different when something requires my attention at home. The classic example is the doorbell: I want to know when a delivery person is standing at my front door even if I'm flooding my noise-killing headset with electric guitar music. I already solved this problem back in 2016 with a Raspberry Pi and described the setup in this column [1].
With my earlier solution, notifications reached me as Twitter direct messages, mainly because I didn't want to install a notification app just for this purpose. Twitter is already installed on my phone and provides a useful programming interface. However, Twitter is increasingly setting up hurdles for access to the API. For example, you now need to describe in a small, free text essay the purpose of your proposed use of the API. This may be understandable from the supplier's point of view, but I would prefer things to be a little simpler.
This explains why I now use the Telegram API [2]. Among other things, I can create bots that can be used to do many funny things. On top of this, I can get the bot to send me messages. You can create a bot by starting a chat with BotFather in the Telegram app (Figure 1).
Start with the /newbot
command. BotFather asks you for a name and a username for your bot. In return, you are sent a numeric ID and an access token – that is, a string that you have to pass to the API to identify yourself. It could look like this, for example:
919445171: AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA
The first few digits are your bot's numeric ID, followed by a colon and the access token. Now you have to start a chat with your bot in the app or the web interface [3]. This chat also has a numeric ID, which you need to discover. The curl
command from Listing 1 does the trick.
Listing 1
Start Telegram Chat
$ curl -X POST https://api.telegram.org/bot919445171:AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA/getUpdates
Note that you need to prefix your bot's numeric ID with the bot
keyword. For the most part, the long answer is unimportant for the purposes of this article, but it does contain a string reading "id":930313820
– this is the chat ID. Now you have all the information you need.
If you now want to automatically send a message to the bot chat, using a Bash script for example, the curl
call from Listing 2 is all it takes. The phone vibrates and a message appears on the display (Figure 2). Now, I'll never miss my deliveries again.
Listing 2
Sending a Message to the Bot Chat
$ curl -X POST 'https://api.telegram.org/bot919445171:AAFt4RTVEcI919fAI_5YGHX0A8QyQnY_fBA/sendMessage?chat_id=930313820&text="Hello world!"'
Infos
- "The sys admin's daily grind: Charly's doorbell Pi" by Charly Kühnast, Linux Magazine , issue 186, May 2016, p. 62, http://www.linux-magazine.com/Issues/2016/186/Charly-s-Column-Doorbell-Pi/(language)/eng-US
- Telegram API: https://core.telegram.org/api
- Telegram web interface: https://web.telegram.org
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.