Skip to content

Instantly share code, notes, and snippets.

@yakutozcan
Created June 18, 2017 12:35
Show Gist options
  • Save yakutozcan/7f6fea92ed05b1d704c234b4158cc70a to your computer and use it in GitHub Desktop.
Save yakutozcan/7f6fea92ed05b1d704c234b4158cc70a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf8 -*-
#Gerekli kütüphaneleri ekliyoruz
from gpiozero import MotionSensor
import telepot
import picamera
import time
from datetime import timedelta
# Hareket sensörünün takılı olduğu pin
pir = MotionSensor(4)
chat_listen = {}
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
# Gelen komutları terminalde görüntülüyoruz
print 'Gelen Komut: %s' % command
# İşlemleri yapacak olduğumuz komutlar /Komut şeklinde tanımlı
if command == '/HareketDinlemeyeBasla':
if chat_id not in chat_listen:
chat_listen[chat_id] = True
bot.sendMessage(chat_id, "Dinlemeye Basladi")
elif chat_listen[chat_id]:
bot.sendMessage(chat_id, "Zaten Dinleniyor")
else:
chat_listen[chat_id] = True
bot.sendMessage(chat_id, "Dinleme Yeniden Basladi")
elif command == '/HareketDinlemeyiDurdur':
if chat_id not in chat_listen:
chat_listen[chat_id] = False
bot.sendMessage(chat_id, "Dinleme Etkin Değil")
elif chat_listen[chat_id]:
chat_listen[chat_id] = False
bot.sendMessage(chat_id, "Dinleme Durduruldu")
else:
bot.sendMessage(chat_id, "Dinleme Etkin Değildi")
elif command == '/NeZamandirCalisiyorsun':
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds=uptime_seconds))
bot.sendMessage(chat_id, uptime_string)
elif command == '/ResimCek':
camera = picamera.PiCamera()
camera.capture('capture.jpg')
camera.close()
bot.sendPhoto(chat_id, photo=open('capture.jpg', 'rb'))
elif command == '/Yardim':
bot.sendMessage(chat_id, 'Komut Bulunamadi')
bot.sendMessage(chat_id, 'Kayitli Komutlar \n'
'/NeZamandirCalisiyorsun \n'
'/HareketDinlemeyiDurdur \n'
'/HareketDinlemeyeBasla \n'
'/ResimCek')
else:
bot.sendMessage(chat_id, 'Komut Bulunamadi')
bot.sendMessage(chat_id, 'Kayitli Komutlar \n'
'/Yardım \n'
'/NeZamandirCalisiyorsun \n'
'/HareketDinlemeyiDurdur \n'
'/HareketDinlemeyeBasla \n'
'/ResimCek')
def notify_motion():
notify("Hareket Algilandi")
def notify_no_motion():
notify("Hareketli Durdu")
def notify(msg):
for chat_id, listening in chat_listen.items():
if listening:
# Mesaj ve resim gönderme işlemleri burada yapılıyor
bot.sendMessage(chat_id, msg)
camera = picamera.PiCamera()
camera.capture('capture.jpg')
camera.close()
bot.sendPhoto(chat_id, photo=open('capture.jpg', 'rb'))
pir.when_motion = notify_motion
pir.when_no_motion = notify_no_motion
# BotFather dan aldığımız anahtarı buraya yazıyoruz.
bot = telepot.Bot('423321162:AAGjAE4hLHGALrDmjLcO8LllaPhZS6P9uRk')
bot.message_loop(handle)
print 'Uygulama Calisti'
while True:
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment