Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #!/usr/bin/python
  2. from gpiozero import MotionSensor
  3. import telepot
  4. import picamera
  5. import time
  6. from datetime import timedelta
  7.  
  8. pir = MotionSensor(4)
  9. chat_listen = {}
  10. # chat_listen_switch = False
  11.  
  12. def handle(msg):
  13. #    global chat_listen_switch
  14.     chat_id = msg['chat']['id']
  15.     command = msg['text']
  16.  
  17.     print 'Got command: %s' % command
  18.  
  19.     if command == '/start':
  20. #        chat_listen_switch = True
  21.         if chat_id not in chat_listen:
  22.             chat_listen[chat_id] = True
  23.             bot.sendMessage(chat_id, "Started Listen Motion")
  24.         elif chat_listen[chat_id]:
  25.             bot.sendMessage(chat_id, "Already Listening")
  26.         else:
  27.             chat_listen[chat_id] = True
  28.             bot.sendMessage(chat_id, "Restarted Listen Motion")
  29.         print "%s said it want to be notified" % chat_id
  30.  
  31.     elif command == '/stop':
  32. #        chat_listen_switch = False
  33.         if chat_id not in chat_listen:
  34.             chat_listen[chat_id] = False
  35.             bot.sendMessage(chat_id, "Wasn't Listening Motion")
  36.         elif chat_listen[chat_id]:
  37.             chat_listen[chat_id] = False
  38.             bot.sendMessage(chat_id, "Stopped Listening")
  39.         else:
  40.             bot.sendMessage(chat_id, "Wasn't Listen Motion")
  41.         print "%s said it doesn't want to be notified" % chat_id
  42.  
  43.     elif command == '/uptime':
  44.         with open('/proc/uptime', 'r') as f:
  45.             uptime_seconds = float(f.readline().split()[0])
  46.             uptime_string = str(timedelta(seconds=uptime_seconds))
  47.             bot.sendMessage(chat_id, uptime_string)
  48.             print "%s said it want the uptime" % chat_id
  49.  
  50.     elif command == '/getimage':
  51.        with picamera.PiCamera() as camera:
  52.         camera.resolution = (2592, 1944)
  53.         camera.start_preview()
  54.         time.sleep(2)
  55.             camera.capture('capture.jpg')
  56.             camera.close()
  57.             bot.sendPhoto(chat_id, photo=open('capture.jpg', 'rb'))
  58.             print "%s said it want the uptime" % chat_id
  59.     else:
  60.             bot.sendMessage(chat_id, 'Command not found')
  61.             print "%s Command not found" % chat_id
  62.  
  63.  
  64. def notify_motion():
  65.     notify("Motion Detected")
  66.  
  67.  
  68. def notify_no_motion():
  69.     notify("Motion Stopped")
  70.  
  71.  
  72. def notify(msg):
  73.     for chat_id, listening in chat_listen.items():
  74. #        if chat_listen_switch:
  75.         if listening:
  76.             bot.sendMessage(chat_id, msg)
  77.             #Get the photo
  78.        with picamera.PiCamera() as camera:
  79.             camera.resolution = (2592, 1944)
  80.             camera.start_preview()
  81.             time.sleep(2)
  82.             camera.capture('capture.jpg')
  83.             camera.close()
  84.             bot.sendPhoto(chat_id, photo=open('capture.jpg', 'rb'))
  85.  
  86. pir.when_motion = notify_motion
  87. pir.when_no_motion = notify_no_motion
  88.  
  89. bot = telepot.Bot('454902990:AAG3C-v7THMpVqcWvAM7HDedOKC1O4QdM2U')
  90. bot.message_loop(handle)
  91. print 'I am listening ...'
  92.  
  93. while True:
  94.     time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement