def check_onpause(): # check for pause events from io import BytesIO from motioncontrol.models import AlertSubscription for a in AlertSubscription.objects.filter(alert_nomotion=True,enabled=True,sent=False,pause=False): r = redis.StrictRedis(host=settings.MOTION_REDIS_SERVER, port=6379, db=0) res = r.get('motion-event-%s' % a.cam.slug) if not res: # idle from telegrambot.wrapper import Bot b = Bot(settings.TELEGRAM_BOT_TOKEN) b.sendMessage(a.destination,"Nessun movimento su /s_%s" % (a.cam.name.replace(' ','_'))) a.sent = True a.save() #img = a.cam.snapshot() #if img: #b = Bot(settings.TELEGRAM_BOT_TOKEN) #fp = BytesIO() #img.save(fp,'JPEG') #fp.seek(0) #b.sendPhoto(a.destination, fp, caption='pause alert %s' % a.cam.name) #a.sent = True #a.save() # check for resume for a in AlertSubscription.objects.filter(alert_nomotion=True,enabled=True,sent=True,pause=False): r = redis.StrictRedis(host=settings.MOTION_REDIS_SERVER, port=6379, db=0) res = r.get('motion-event-%s' % a.cam.slug) if res: # moving from telegrambot.wrapper import Bot b = Bot(settings.TELEGRAM_BOT_TOKEN) b.sendMessage(a.destination,"Movimento ripristinato su /s_%s" % (a.cam.name.replace(' ','_'))) a.sent = False a.save()
def ifttthook(request,user_id,cmd,param): if not settings.MOTION_TELEGRAM_PLUGIN: return HttpResponse('telegram plugin disabled') from telegrambot.models import TelegramUser from telegrambot.wrapper import Bot from motioncontrol.telegram.parser import Parser from motioncontrol.models import AlertSubscription try: user = TelegramUser.objects.get(user_id=user_id) bot = Bot(settings.TELEGRAM_BOT_TOKEN) if cmd == 'pause' and param == 'all': for a in AlertSubscription.objects.filter(destination=user.user_id,enabled=True): a.pause = True a.save() bot.sendMessage(user.user_id,"Alerts disattivati via ifttt") if cmd == 'unpause' and param == 'all': for a in AlertSubscription.objects.filter(destination=user.user_id,enabled=True): a.pause = False a.save() bot.sendMessage(user.user_id,"Alerts attivati via ifttt") if param.isdigit(): c = Cam.objects.get(id=int(param)) alert,created = AlertSubscription.objects.get_or_create( channel = 'telegram', destination = user.user_id, cam = c, alert_motion = True if cmd.endswith('motion') else False, alert_nomotion = True if cmd.endswith('pause') else False, enabled = True if cmd.startswith('on') else False, pause = False ) bot.sendMessage(user.user_id,"%s alert on %s %s via ifttt" % ( 'Motion' if cmd.endswith('motion') else 'Pause', c.name, 'enabled' if cmd.startswith('on') else 'disabled' )) except: # not writing any errors for tinkerers pass return HttpResponse()