The project I’m going to describe today it’s a sort of proof of concept that will demonstrate the possibility to remote control sensors and actuators (for example a couple of relays) via Telegram.
My idea was to develop a bot, running on my Raspberry Pi, that receives commands via Telegram chats. I connected to the Raspberry a temperature/humidity sensor and a module with two relays. Here’s a short video that describes the project:
How it works
The working of the bot is shown in the following diagram:
The user, through the Telegram app installed on his smartphone, starts a chat with the “bot” account; the messages are delivered to the Telegram servers. The program running on the Raspberry can get the messages in two ways:
- polling, every tot seconds the program verifies if there are new messages and fetches them
- webhook, the program configures Telegram to dispatch the new messages to a web URL
The second solution requires a public IP address (static or a dyndns service) and some configuration (NAT) on your Internet router, I therefore chose to implement the simpler polling.
Let’s create the bot
The first step in developing your own bot, is to create it in Telegram. You create new bots or configure existing ones sending the right commands to a “built-in” bot, the BotFather:
Start the process to create a new bot sending to the BotFather the command /newbot. You’ll be prompted for the name (display name) of your new bot and for its username. If the process is successful, you’ll receive an authorization token, that is the “password” you must specify in your program to “impersonate” the bot:
- node-dht-sensor, to read the temperature and humidity values from a DHT11 sensor
- onoff, to control the Raspberry digital PINs
- node-telegram-bot, to use the Telegram APIs
The library installation is super simple thanks to the NPM (Node Package Manager). Create a new folder to save the bot code and, within that folder, type the following command:
npm install node-dht-sensor onoff node-telegram-bot
Remember to change the token in the code, typing the one generated by the BotFather for your bot:
To run the bot, type:
sudo node bot.js
The available commands are:
- /getouts, shows the actual status of the two relays
- /setout1 ON|OFF, /setout2 ON|OFF, sets one of the two outputs to ON or OFF
- /gettemp, shows the actual temperature
- /gethum, shows the actual humidity
Telegram bots are public, that means that every user can send messages to them. To be sure that my bot will answer only to me, I added in the code a check on the ID of the user that is sending commands to the bot:
If you need to retrieve your Telegram ID, you can use a small bot, this too available on Github, that simply answer each message with the original one (echo) and the sender ID:
In my example, I used a DHT11 temperature/humidity sensor and a module with two relays. I connected the two elements to the expansion PINs of my Raspberry (1 PIN for the sensor and 2 for the relays, in addition to the power supply):
In the program, I configured the correct PIN numbers:
The goal of this project was to demonstrate how it was possible to develop a bot that can receive commands via Telegram: it’s a starting point.
Telegram APIs offer a lot of advanced featured to explore, for example the ability to list the available commands, to change the bot’s avatar or to customize the app keyboard: in my example, when the user sends the /setout1 command, the bot may ask the desired status (ON|OFF) for that output customizing the app to show a keyboard with only two buttons, for the two possible choices.