Telegram Bot to Control a Light Bulb using Python

Rithu
Analytics Vidhya
Published in
4 min readSep 12, 2020
Photo from SCMP

In this project, we’ll create a Telegram Bot using python-telegram-bot and deploy it to Heroku. It is also programmed to control a feed on Adafruit. The program sends values of 1 and 0 to the feed and turns the status indicator on or off.

Before starting

You will have to sign up for the following services:

  • a Heroku account, which is a free cloud platform
  • a Telegram account, on which we will build our bot
  • an Adafruit account

Commands

This bot takes 3 commands: /start ,/lighton and /lightoff

  • Command /start will return a message instructing the user to give the appropriate inputs
  • /lighton will display the message “Light has been turned ON”. It will also return an image of a lit bulb and will send a value to Adafruit indicating that its ON.
  • /lightoff will display the message “Light has been turned OFF”. It will also return an image of an unlit bulb and will send a value to Adafruit indicating that its OFF.

If any unknown command is given, the bot will reply with the message indicating that it hasn’t understood your command.

Creating the Bot on Telegram

Create a bot on the Telegram app on your phone. Bots on Telegram can be created using BotFather. The process is shown below. Commands are prefixed with a / . After creating your bot, save the token given carefully. This is required to access the Telegram Bot HTTP API. Also, click on the link given (t.me/your-bot) to access your bot.

Creating a bot on BotFather

Getting the API keys from Adafruit

Your keys from Adafruit is required to access its API and send values to the required feed. This can, in turn, be programmed to be connected to an Arduino, which can receive the values you send.

Adafruit IO Keys

Along with this, create a feed and a dashboard on Adafruit and set it to ‘public’.

Coding the Bot Using Python

The bot is coded with Python. Apart from this, a Procfileand a requirements.txt file should be created on GitHub. The Procfile is required to create a dyno for your app on Heroku. The requirements file is required for Heroku to install the necessary libraries to run Python and Adafruit.

Check out my GitHub repository for comments and other files!

Deploying the app on Heroku

Source

Create a new account or use an existing one to build an app in Heroku. Name your app suitably and click next. Here, we will connect our GitHub account to Heroku as the ‘Deployment Method’ instead of using Heroku CLI. Search your repository and connect it. Also, enable ‘Automatic Deploys’.

Go to the ‘Settings’ page and set your ‘Config Vars’ to values containing your Adafruit username, password, and Telegram API token. Create a ‘Procfile’ on your connected GitHub repo and refresh the ‘Overview’ tab page on Heroku. On doing this, you will see that it has recognized your ‘Procfile’ under ‘Dyno formation’. Click on ‘Configure Dynos’ and set it to ON.

With these steps, your app should start building and get deployed successfully.

Results

Telegram Bot

The bot responds to the commands appropriately and also sends the values to Adafruit through Heroku.

Values sent to Adafruit through the cloud

Thank you for reading!

--

--