Controlling Light using Telegram Bot
In this project, we are creating a Telegram Bot using a BotFather and deploy it using Heroku. To control the Adafruit IO feed values 1 and 0 we programmed it using Google Colaboratory (i.e to link Telegram bot and Adafruit IO).
Keywords: Telegram Bot, Adafruit IO, Google Colaboratory, Heroku
Requirements
Before starting this project, you have a account in the following services.
- Telegram Account (on which your bot is created)
- Google Colaboratory (to write the python code)
- Adafruit IO (to feed the values )
- Heroku ( a cloud platform)
- Github
Step 1:- Getting a Adafruit IO Key
It’s a cloud service. Display your data in real-time, online. Adafruit.io can handle and visualize multiple feeds of data. Feeds are the core of the Adafruit IO system.
Login/ Sign up in Adafruit IO and get your IO key, as shown in image ( you will find Adafruit IO Key on the top right corner of your screen). Copy and save the USERNAME and ACTIVE_KEY for further use.
Step 2:- Creating a Telegram Bot
Create a Bot on Telegram app of your phone or a system. Here we created a Telegram Bot using a Botfather.
What is Bot ?
A bot is a software application that is programmed to do certain tasks. Bots are automated, which means they run according to their instructions without a human user needing to start them up. Bots often imitate or replace a human user’s behavior. Typically they do repetitive tasks, and they can do them much faster than human users could.
Bots usually operate over a network; more than half of Internet traffic is bots scanning content, interacting with webpages, chatting with users, or looking for attack targets
Bots can be:
Chatbots: Bots that simulate human conversation by responding to certain phrases with programmed responses.
Web crawlers (Googlebots): Bots that scan content on webpages all over the Internet.
Social bots: Bots that operate on social media platforms.
Malicious bots: Bots that scrape content, spread spam content, or carry out credential stuffing attacks.
- The procedure of a creating a bot is shown in image.
- In bot every commands starts with a prefix
/
. - After successfully creating a bot, you will get a API (Application programming interface)link. Copy and save this API link for further use.
- To access a newly created bot, click on this link (t.me/write your bot name).
Step 3:- Coding the Bot using Python language
In this step we are bridging a interface between Adafruit IO and Telegram Bot using python programming. The gist of the code is shown below :
Or else click on my Github Repository to access all files regarding this project.
Step 4:- Deploying the app on Heroku
Heroku is a cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. It takes away all the pain of installing software, maintaining it, monitoring the software for required updates, setting up your deployments, etc.
- Login/Sign up a account in Heroku.
- Create a new app and give a suitable name and select the region.
- Create a
Procfile
,requirements.txt
,Bot_project.py
andREADME.md
file in github repository and connect your github repository with heroku as Deployment method. Also, enable ‘Automatic Deploys’. - Go to the ‘Settings’ page and set your ‘Config Vars’ to values containing your
ADAFRUIT_IO_USERNAME
,ADAFRUIT_IO_KEY
, andTELEGRAM_TOKEN
. - In overview section, select ‘Dyno formation’ and click on ‘Configure Dynos’ and set it as ON.
- Now your deployment is successfully completed, you can see that by click on “View logs”.
Step 5:- Output Results
Now you can see that, when the command /lighton
or Turn on the light
is given to the bot , the bot returns the statement Light is switched ON
and displays the glow bulb image and the value 1 is sent to the Adafruit feed.
Later the command /lightoff
or Turn off the light
is given to the bot , the bot returns the statement Light is switched OFF
and the value 0 is sent to the Adafruit feed.
Observe the below graph, when value 1 is sent to the feed the graph goes in upward direction (rise) and when the value 0 is sent to the feed the graph goes in downward direction (fall).
This is how we create a light controlling bot with Telegram. I hope you like this project. Thank your for reading.
Ten articles before and after
Telegram’da Benim de Bir Tuzum Olsun – Telegram Group
How to send photos to Telegram in Ruby – Telegram Group
data-rh=”true”>Explainer: UK News Quiz, a Telegram Bot – Dror Kessler – Medium – Telegram Group
Telegram bot on the cloud. I wanted to create a bot in Telegram… – Telegram Group
Reacting to user feedback in a pet-project. Fast. – Telegram Group
data-rh=”true”>Telegram Bot и отчётность по проекту – ToBe – Medium – Telegram Group
Делаем бота для учета личных расходов на Python используя Google Spreadsheets – Telegram Group
[BEST] Tricks To Rank #1 Your Telegram Channel (2022) – Telegram Group
data-rh=”true”>Telegram channel – KYC COIN – Medium – Telegram Group