Telegram Bot to Control a Light Bulb using Python

Rithu
Rithu
Sep 12, 2020 · 4 min read
Image for post
Image for post
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:

Commands

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

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.

Image for post
Image for post
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.

Image for post
Image for post
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

Image for post
Image for post
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

Image for post
Image for post
Image for post
Image for post
Telegram Bot

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

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Values sent to Adafruit through the cloud

Thank you for reading!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Rithu

Written by

Rithu

Student

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Rithu

Written by

Rithu

Student

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store