Turn ON and OFF the light using Telegram bot using Python and Heroku
Requirements:
- Telegram app
- Google colad(to code in python or you can use your own IDE)
- Heroku(cloud service)
- Adafruit_IO(cloud interface)
Introduction
In this blog i ’am gonna show you how you can control the light using telegram bot and python programming language.
1️⃣:First you need to set up the bot using the telegram app
- open the telegram app and select search, type Botfather and select it
2.now once use open it type “/start”.
3.Now we need to name the bot.so ,first we need to type “/newbot” and then give bot the name you want and then give it an username.
4.In the above image you can see the bot has successfully been created and note down the api key given to you .
2️⃣:Now once we have created the bot now we need to write the python code to automate it.
▶Before we start programing we need to install some libraries
pip install adafruit-io
pip install python-telegram-bot
▶Before we start coding we need to create the feed and dashboard status indicator. Creating the feed in adafruit-io can be done through python ,this can be seen below in the code.
▶Now i will show you how to create feed and indicator. First of all create a account in adafruit-io.
▶Once you have created the account now you have to go to feeds section and create one.
▶Now we have create the status indicator in dashboard
▶Now we need to note down the adafruit-io key
▶Now we go for code.
import the required libraries
from Adafruit_IO import Client, Feed, Datafrom telegram.ext import Updater, CommandHandler, Filters, MessageHandlerimport requestsimport os
▶here we take the input of all the required adafruit-io keys and the telegram api key which we noted down.
#creation the feed(this should be done only once)…..if you want to create the feed automatically from code then run this part of code separately or else you can create#feed = Feed(name='light-bot-feed')#result = aio.create_feed(feed)#adafruit_io user name and active keyADAFRUIT_IO_USERNAME = os.getenv('ADAFRUIT_IO_USERNAME')ADAFRUIT_IO_KEY = os.getenv('ADAFRUIT_IO_KEY')aio = Client('ADAFRUIT_IO_USERNAME','ADAFRUIT_IO_KEY')Telegram_token = os.getenv('Telegram_token')
▶here in this part of the code we will define each function for every command we give for the bot to take the action. you can just go through the comments in the code its very easy to understand.
#this function is used reply when you start the botdef start(bot, update):bot.send_message(chat_id = update.effective_chat.id, text="Welcome!")bot.send_message(chat_id = update.effective_chat.id, text="if you like to turn on the light then type 'Turn on the light' or if you would like to turn off the lights then type 'Turn off the light'")#this function is used reply when we input apart from the requirementdef wrong_message(bot, update):bot.send_message(chat_id=update.effective_chat.id, text="Oops, I unable to understand that. Please try again!")#this function is used to send data to adafruit_io feed mentioneddef send_data_adafruit(value1):value = Data(value=value1)value_send = aio.create_data('light-bot-feed',value)#function to turn on the lightdef turn_on_light(bot, update):chat_id = update.message.chat_idbot.send_message(chat_id, text="Turning on the light")bot.send_photo(chat_id, photo='http://scienceblog.cancerresearchuk.org/wp-content/uploads/2015/08/Lightbulb_hero2.jpg')send_data_adafruit(1)#function to turn off the lightdef turn_off_light(bot, update):chat_id = update.message.chat_idbot.send_message(chat_id, text="Turning off the light")bot.send_photo(chat_id=update.effective_chat.id,photo='https://ak.picdn.net/shutterstock/videos/1027638404/thumb/1.jpg?ip=x480')send_data_adafruit(0)def text_given(bot, update):text = update.message.textif text == 'Start':start(bot,update)elif text == 'Turn on the light':turn_on_light(bot,update)elif text == 'Turn off the light':turn_off_light(bot,update)else:wrong message(bot,update)
▶once we are done with the function part now we write final part of the code to call those functions
ud = Updater('Telegram_token')dip = ud.dispatcherdip.add_handler(MessageHandler(Filters.text, text_given))ud.start_polling()ud.idle()
▶now we are done with the code. Now we need to deploy it in the Heroku cloud platform.
Here before we deploy it on Heroku, we need to write the python code in GitHub. The required files are given below.
Procfile file contains “worker: python file-name.py” .you need to create the file with no file type and type the give line in that file and commit changes, the requirements.txt contains all the libraries we imported other than inbuilt libraries.
once your done with the GitHub part now we will go to Heroku part
3️⃣Create a account in Heroku(Heroku is a free cloud service platform) and then create a app in it shown below.
once created ,connect your GitHub account
now enable automatic deploys.
▶once this is done we have to activate the dyno (i.e. the Procfile which we created in GitHub).
after activating the dyno now the model will deploy automatically because we have already activated it.
As we can see here the app has been released and now you can open telegram and chat with bot (with the predefine commands we have mentioned) .
You can see the bot replies with the image and text as we specified.
This is all it.
You have made a telegram light control bot.
For full code refer to my GitHub page mentioned below.