Practical 5
Aim:
       Controlling Raspberry Pi with Telegram.
Additional Hardware Required:
  1. LED
  2. Breadboard
  3. Resistor
  4. Jumper wires
Software required:
On Mobile Phone: Telegram
Steps
1: Download Telegram from playstore on your android phone.
2: Install Telegram.
2:   Open Telegram app in your system or mobile
3: Click On Start Messaging Button
4: Enter your mobile number to register with telegram service.
5: Search for name "BotFather"
6: Click on "BotFather
7: To Start "BotFather" type /start in message
8: Now type /newbot in message . and then give name to your BOT and
Username also.
9: Obtain access token
This same token we are supposed to use in our code in raspberry pi to
connect to our BOT.
Set up On Raspberry Pi:
1: Install "Python Package Index" and Telepot using :
python3 -m pip config set global.break-system-packages true
sudo apt install python3-pip
pip3 install telepot
2: Test your bot using python3 IDLE and type:
import telepot
bot = telepot.Bot('Bot Token')
bot.getMe()
If it prints your bot details means everything is correct.
If not then token is wrong.
Write the following code in Pyhton 2 IDLE and save it as
‘mybot.py’
import sys
import time
import random
import datetime
import telepot
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
def handle(msg):
  chat_id = msg['chat']['id']
  command = msg['text']
  print 'Got command:', command
  if command == 'on':
     bot.sendMessage(chat_id, "LED on")
     GPIO.output(11,GPIO.HIGH)
  elif command =='off':
     bot.sendMessage(chat_id, "LED off")
     GPIO.output(11,GPIO.LOW)
  elif command == 'stop':
      exit()
try:
       bot = telepot.Bot('Bot Token')
       bot.message_loop(handle)
       print 'I am listening...'
    while 1:
        time.sleep(10)
except TelegramError:
    print ' '
Note: do not forget to replace ‘Bot Token’ with your token received from
telegram.
Output:
Connections:
Output on python IDLE:
Output on Telegram (Mobile phone):