Advertisement
rabirajkhadka

TelegramBot

Mar 22nd, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import sys
  2. import time
  3. import telepot
  4.  
  5. """
  6. py -m venv tgbot-venv
  7. tgbot-venv/Scripts/activate
  8. pip install python-telegram-bot
  9. pip install telepot
  10. """
  11.  
  12.  
  13. import RPi.GPIO as GPIO
  14.  
  15. def on(pin):
  16. GPIO.output(pin,GPIO.HIGH)
  17. return
  18. def off(pin):
  19. GPIO.output(pin,GPIO.LOW)
  20. return
  21.  
  22. GPIO.setmode(GPIO.BOARD)
  23. lightPin = 11
  24. GPIO.setup(lightPin, GPIO.OUT)
  25.  
  26.  
  27.  
  28. def handle(msg):
  29. chat_id = msg['chat']['id']
  30. command = msg['text']
  31.  
  32. print('Recieved message is: %s' % command)
  33.  
  34. if 'on' in command:
  35. message = "Turned on "
  36. if 'light' in command:
  37. message = message + "light"
  38. GPIO.output(lightPin, 1)
  39. tgbot.sendMessage (chat_id, message)
  40.  
  41. if 'off' in command:
  42. message = "Turned off "
  43. if 'light' in command:
  44. message = message + "light "
  45. GPIO.output(lightPin, 0)
  46. tgbot.sendMessage(chat_id, message)
  47. tgbot = telepot.Bot('5276579939:AAGcVVGvTmFvrCN9u7jARjWTnh8F-XLWOxY')
  48. tgbot.message_loop(handle)
  49. print (tgbot.getMe())
  50. print('I am listening...')
  51.  
  52. while 1:
  53. try:
  54. time.sleep(100)
  55. except KeyboardInterrupt:
  56. print('\n Program interrupted')
  57. GPIO.cleanup()
  58. exit()
  59. except:
  60. print('Other error or exception occured!')
  61. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement