def send_text(self, chat_id, text): LoggingService.info('Sending to %s: %s' % (chat_id, text)) data = {'chat_id': chat_id, 'text': "(" + self.configuration.pi_name + ")" + os.linesep +text} request = requests.post(self.configuration.url + self.configuration.token + '/sendMessage', data=data) if not request.status_code == 200: return False return request.json()['ok']
def connect(self): while True: try: LoggingService.info('checking for updates') self.check_updates(self._load_data()) time.sleep(self.configuration.interval) except KeyboardInterrupt: break LoggingService.info('EXIT')
def send_video(self, chat_id, file_name): LoggingService.info('Sending to %s: %s' % (chat_id, file_name)) data = {'chat_id': chat_id} files = {'document': open(file_name, 'rb')} request = requests.post(self.configuration.url + self.configuration.token + '/sendDocument', data=data, files=files) return request.json()['ok']
def send_text(self, chat_id, text): LoggingService.info('Sending to %s: %s' % (chat_id, text)) data = { 'chat_id': chat_id, 'text': "(" + self.configuration.pi_name + ")" + os.linesep + text } request = requests.post(self.configuration.url + self.configuration.token + '/sendMessage', data=data) if not request.status_code == 200: return False return request.json()['ok']
def check_updates(self, data): # Message gets accepted if # (1) Sender is authorized # (2) Correct recipient for update in data: self.offset = update['update_id'] from_id = update['message']['chat']['id'] name = update['message']['chat']['first_name'] if from_id not in self.configuration.allowed_access: LoggingService.info("Unauthorized access") return self.send_text(from_id, UnauthorizedCommand(None).process(from_id)) continue message = update['message']['text'] parameters = (self.offset, name, from_id, message) LoggingService.info('Message (id%s) from %s (id%s): "%s"' % parameters) body = self.check_recipient_params(message) if body is None: LoggingService.info("Different recipient") continue command = CommandFactory.create_command(body) try: return_message = command.process(from_id) self.send_message(from_id, return_message) except Exception as inst: LoggingService.exception(inst) return self.send_text(from_id, str(inst))
def check_updates(self, data): # Message gets accepted if # (1) Sender is authorized # (2) Correct recipient for update in data: self.offset = update['update_id'] from_id = update['message']['chat']['id'] name = update['message']['chat']['first_name'] if from_id not in self.configuration.allowed_access: LoggingService.info("Unauthorized access") return self.send_text( from_id, UnauthorizedCommand(None).process(from_id)) continue message = update['message']['text'] parameters = (self.offset, name, from_id, message) LoggingService.info('Message (id%s) from %s (id%s): "%s"' % parameters) body = self.check_recipient_params(message) if body is None: LoggingService.info("Different recipient") continue command = CommandFactory.create_command(body) try: return_message = command.process(from_id) self.send_message(from_id, return_message) except Exception as inst: LoggingService.exception(inst) return self.send_text(from_id, str(inst))
def startObservation(self, callback): configuration = Configuration() configuration.load(ConfigurationEnum.Pir) GPIO.setmode(GPIO.BCM) GPIO.setup(configuration.pir_pin, GPIO.IN) GPIO.add_event_detect(configuration.pir_pin, GPIO.RISING, callback, bouncetime=300) LoggingService().info("Start observation.")
def _load_data(self): data = {'offset': self.offset + 1, 'limit': 5, 'timeout': 0} try: request = requests.post(self.configuration.url + self.configuration.token + '/getUpdates', data=data, timeout=2) except Exception as e: LoggingService.info('Error getting updates') LoggingService.info(e) return [] if not request.status_code == 200: LoggingService.info("Request failed with code " + str(request.status_code)) return [] LoggingService.info(request.json()) if not request.json()['ok']: return [] return request.json()['result']
from configuration.Configuration import Configuration from configuration.ConfigurationEnum import ConfigurationEnum from domain_services.logging_service.LoggingService import LoggingService from domain_services.telegram_service.TelegramService import TelegramService from domain_services.motion_detection_service.MotionDetectionService import MotionDetectionService from handlers.MotionDetectionHandler import MotionDetectionHandler import pexpect if __name__ == '__main__': configuration_telegram = Configuration() configuration_telegram.load(ConfigurationEnum.Telegram) LoggingService.info("Start telegram service") telegram_service = TelegramService(configuration_telegram) configuration_wifi = Configuration() configuration_wifi.load(ConfigurationEnum.Ip) motion_detection_handler = MotionDetectionHandler(configuration_wifi, telegram_service) motion_detection_service = MotionDetectionService() motion_detection_service.startObservation(motion_detection_handler.process) telegram_service.connect()