From acf09f14b31432be39955edbedeeb0711623a02d Mon Sep 17 00:00:00 2001 From: paspo Date: Wed, 27 Dec 2017 22:44:11 +0100 Subject: [PATCH] added spooling --- .gitignore | 1 + TODO | 13 +++++++++- telegram-notify.py | 65 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8557253 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +telegram-notify.conf diff --git a/TODO b/TODO index e82bf36..f2545c4 100644 --- a/TODO +++ b/TODO @@ -7,4 +7,15 @@ - user settings (activation time, etc) - command extension (/record for recording audio) - unpair other usernames -- directory monitoring, for message spooling +- catch MaxRetryError? +- support for md files in spool + +dependencies: +- python-watchdog +- python-configparser +- telepot??? +- datetime forse gia' incluso +- json forse gia' incluso +- re forse gia' incluso +- random forse gia' incluso +- time forse gia' incluso diff --git a/telegram-notify.py b/telegram-notify.py index 3ffe474..89200e1 100644 --- a/telegram-notify.py +++ b/telegram-notify.py @@ -7,7 +7,10 @@ import ConfigParser import telepot import json import sys +import os from telepot.loop import MessageLoop +from watchdog.observers import Observer +from watchdog.events import PatternMatchingEventHandler version = 0.1 @@ -33,7 +36,7 @@ def handle(msg): is_paired = len(paired_user) > 0 # if text starts with '/' then it's a command - if re.match('/',msg['text']): + if re.match(r'^/',msg['text']): command = re.sub(r"\s.*",'',msg['text']) param = re.sub(r"^([^\s]+)\s","",msg['text']) @@ -99,6 +102,54 @@ def handle(msg): bot.sendMessage(chat_id, 'Settings:\n' ) +def processFile(filename): + global chat_ids + if re.match(r'.*jpg$',filename): + for c in chat_ids: + try: + r=bot.sendPhoto(c['id'], open(filename, 'rb')) + os.remove(filename) + except: + time.sleep(10) + if re.match(r'.*txt$',filename): + for c in chat_ids: + try: + with open(filename,'r') as f: + r=bot.sendMessage(c['id'], f.read()) + os.remove(filename) + except: + time.sleep(10) + + + +class FilesChangedHandler(PatternMatchingEventHandler): + patterns = ["*.jpg", "*.txt"] + + def process(self, event): + """ + event.event_type + 'modified' | 'created' | 'moved' | 'deleted' + event.is_directory + True | False + event.src_path + path/to/observed/file + """ + global bot + if event.event_type=='created': + processFile(event.src_path) + + def on_modified(self, event): + self.process(event) + + def on_created(self, event): + self.process(event) + + + +def checkFiles(): + l=os.listdir(spool_dir) + for f in l: + processFile("%s/%s" % (spool_dir,f) ) for arg in sys.argv: if arg=='-v' or arg=='--version': @@ -110,6 +161,9 @@ for arg in sys.argv: configParser = ConfigParser.RawConfigParser() configParser.read('telegram-notify.conf') +spool_dir = configParser.get('general', 'spool_dir') +# TODO: check if spool_dir exists and is a dir + chat_ids=[] print "Allowed users: " for p in configParser.sections(): @@ -129,5 +183,14 @@ bot = telepot.Bot(token) MessageLoop(bot, handle).run_as_thread() print 'I am listening ...' +# TODO: send message if files present in spool at start +checkFiles() + +observer = Observer() +observer.schedule(FilesChangedHandler(), path=spool_dir) +observer.start() + + while 1: time.sleep(10) + checkFiles()