Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- import telepot
- """
- py -m venv tgbot-venv
- tgbot-venv/Scripts/activate
- pip install python-telegram-bot
- pip install telepot
- """
- import RPi.GPIO as GPIO
- def on(pin):
- GPIO.output(pin,GPIO.HIGH)
- return
- def off(pin):
- GPIO.output(pin,GPIO.LOW)
- return
- GPIO.setmode(GPIO.BOARD)
- lightPin = 11
- GPIO.setup(lightPin, GPIO.OUT)
- def handle(msg):
- chat_id = msg['chat']['id']
- command = msg['text']
- print('Recieved message is: %s' % command)
- if 'on' in command:
- message = "Turned on "
- if 'light' in command:
- message = message + "light"
- GPIO.output(lightPin, 1)
- tgbot.sendMessage (chat_id, message)
- if 'off' in command:
- message = "Turned off "
- if 'light' in command:
- message = message + "light "
- GPIO.output(lightPin, 0)
- tgbot.sendMessage(chat_id, message)
- tgbot = telepot.Bot('5276579939:AAGcVVGvTmFvrCN9u7jARjWTnh8F-XLWOxY')
- tgbot.message_loop(handle)
- print (tgbot.getMe())
- print('I am listening...')
- while 1:
- try:
- time.sleep(100)
- except KeyboardInterrupt:
- print('\n Program interrupted')
- GPIO.cleanup()
- exit()
- except:
- print('Other error or exception occured!')
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement