def receive_picture_alert(sender, **kwargs): event=kwargs['data'] from motioncontrol.models import AlertSubscription als = AlertSubscription.objects.filter(cam=event.cam,enabled=True,alert_motion=True,pause=False) for a in als: if a.channel == 'telegram': from telegrambot.wrapper import Bot img = event.img() if img: b = Bot(settings.TELEGRAM_BOT_TOKEN) fp = BytesIO() img.save(fp,'JPEG') fp.seek(0) b.sendPhoto(a.destination, fp, caption='motion alert %s' % event.cam.name) #cache.set("%s-%s-%s" % (__name__,tipo,self.user.user_id),True,30) print("Picture Alert")
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()
def ready(self): from django.conf import settings if 'runserver' in sys.argv: from telegrambot.wrapper import Bot b = Bot(settings.TELEGRAM_BOT_TOKEN) if not settings.TELEGRAM_USE_WEBHOOK: b.post('setWebhook',{'url':''}) print("Telegram WebHook Disabled") Timer(10,b.getUpdates).start() if settings.TELEGRAM_USE_WEBHOOK: from telegrambot.wrapper import Bot b = Bot(settings.TELEGRAM_BOT_TOKEN) b.setWebhook() import telegrambot.signals import telegrambot.connectors
def sendAlert(self,event,tipo): from telegrambot.wrapper import Bot if tipo == 'motion': if not cache.get("%s-%s-%s" % (__name__,tipo,self.user.user_id)): b = Bot(settings.TELEGRAM_BOT_TOKEN) fp = BytesIO() event.snapshot().save(fp,'JPEG') fp.seek(0) b.sendPhoto(self.user.user_id, fp, caption='motion alert %s' % self.camera.name) cache.set("%s-%s-%s" % (__name__,tipo,self.user),True,30) if tipo == 'picture': img = event.img() if img: if not cache.get("%s-%s-%s" % (__name__,tipo,self.user.user_id)): b = Bot(settings.TELEGRAM_BOT_TOKEN) fp = BytesIO() event.img().save(fp,'JPEG') fp.seek(0) b.sendPhoto(self.user.user_id, fp, caption='picture alert %s' % self.camera.name) cache.set("%s-%s-%s" % (__name__,tipo,self.user.user_id),True,30)
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 webhook(request): b = Bot(settings.TELEGRAM_BOT_TOKEN) return HttpResponse(b.webhook(request))