In this article, we shall talk about interfacing the Telegram app with a product called PiRelay.
First, let’s get to know the main components required in this project, the Telegram app.
Telegram app is free chat application being used by a large community of people to stay connected with their loved ones. This app is available on all mobile operating system stores like Android, iOs, and Windows.
It has over 100 million downloads on play store and people claim it to be faster and more functional than other similar applications. One special feature of this application is that they support bots which means this smartphone application can be used by Humans as well as by an intelligent machine termed as ‘bots’ here.
The second major component is the PiRelay. It is a Raspberry Pi based relay shield that you can use to switch high voltage/current devices. The relays can be connected to switch an electrical circuit, perfect for controlling things like lights, fans, motors, doors, air conditioning or any other electronic device that you might want to control.
Another main component of this article is the Raspberry Pi. It is the brain or the mechanism that will control the interfacing of the PiRelay and the Telegram app. Raspberry Pi is an ARM architecture and open-source Linux based Operating System that has helped us a lot in getting our projects online in no time. It is famously known as the credit card sized computer that fits all pockets for making intelligent projects using various computer programming languages. So far Raspberry Pi has acquired a large portion of the digital market.
Here, in this project, we need to learn how to train Raspberry Pi to act as a machine (bot for Telegram). After this setup is ready, anyone can chat with the Raspberry Pi like a normal chatting application, similar to where people (humans) chat with each other and even share Photos Pictures Documents and Audio files. We can even train it to be our own personal assistant. So let’s start to learn about how to build a Raspberry Pi based Telegram bot or interfacing our PiRelay with Telegram.
We need to gather the required materials first before we start interfacing. The list of required materials is:
After having gathered all the essential materials, we proceed towards step 1.
Step 1: Install the Telegram app on mobile Go to your phones play store, and install Telegram. Telegram is available for Android, IOS and even for Windows platform. So just go ahead and download your Telegram application. Just like all application, there will be a small Sign Up procedure to start using Telegram, continue with it until you reach your home screen. Refer to the image below.
Step2: Talk to Bot Father The next step would be to search for BotFather and request the Bot Father to create us a new Bot. To do this, check on the top right corner of the Home screen there will be a search icon, click on it to search for the name “botfather”. Botfather is an AI-based Bot by itself, it will guide you to create a new bot for you.
Click on start and select ‘/newbot’ after you receive an auto-generated reply, as shown in the picture below. Now, the bot will ask for a few details like name of your Bot and the username of the bot. Fill those details and remember the username as we will need them in the future.
Step3: Getting your token for access Now, we must name our bot. Because we are discussing PiRelay, let’s name it as PiRelay and the username as PiRelayBot. After this process, the botfather will give you a Token for access. This is like the password for your bot, people can control and program your bot using this token key. So keep it safe and do not share it with anyone. Once you have received this token key it is time to move on to Raspberry Pi.
Step4: Telepot for installing Telegram on Raspberry Pi We shall use the ‘Python’ package to use the Telegram Bot in Raspberry Pi. It is called ‘Telepot’. We need to install this package on Raspberry Pi by using the following commands on the Linux terminal.
sudo pip3 install telepot
This will import Telepot into Raspberry. Now, we can use this package in our python program to communicate with our Telegram Bot.
Step 5: Programming your Raspberry Pi The bot that we just created on Telegram is an empty draft, it cannot do any process on its own unless we teach it what and how to do things. This teaching can be done through the Raspberry Pi and Python script. In this tutorial, we can program the bot to perform some basic actions like sending a message, photo, audio, and document. So when you say a particular command it will respond with a particular action.
Now, we are clear with the basics of this project. Next, we can proceed towards connecting our PiRelay with the Raspberry Pi. We shall now write programmable code for interfacing the Telegram App and the relays. We have already downloaded the teleport inside our Raspberry Pi, so now we just have to import the libraries as shown below.
import time, datetime import PiRelay import telepot from telepot.loop import MessageLoop
After this, we shall create objects for the four relays using the following codes.
relay1 = PiRelay.Relay("RELAY1") relay2 = PiRelay.Relay("RELAY2") relay3 = PiRelay.Relay("RELAY3") relay4 = PiRelay.Relay("RELAY4")
There will be a chat id and command for each message we send from the mobile. This chat id is required by the program to reply back to the sender. So we save the chat id and message as shown below.
chat_id = msg['chat']['id'] command = msg['text']
In the ‘command’ variable, whatever message we send from the phone using the Telegram, will be saved as a string. We will use the code shown below to switch ‘ON/OFF’ the relays and you shall receive the string message as the action is performed.
if 'ON' in command: message = "Turned ON " if 'Relay 1' in command: message = message + "Relay 1" relay1.on() elif 'Relay 2' in command: message = message + "Relay 2" relay2.on() elif 'Relay 3' in command: message = message + "Relay 3" relay3.on() elif 'Relay 4' in command: message = message + "green " relay4.on() elif 'all' in command: message = message + "all Relay's" relay1.on() relay2.on() relay3.on() relay4.on() telegram_bot.sendMessage (chat_id, message)
if 'OFF' in command: message = "Turned OFF " if 'Relay 1' in command: message = message + "Relay 1" relay1.off() if 'Relay 2' in command: message = message + "Relay 2" relay2.off() if 'Relay 3' in command: message = message + "Relay 3" relay3.off() if 'Relay 4' in command: message = message + "Relay 4" relay4.off() if 'all' in command: message = message + "all Relay's " relay1.off() relay2.off() relay3.off() relay4.off() telegram_bot.sendMessage (chat_id, message)
The complete library of this project is available on GitHub