added spooling

This commit is contained in:
Paolo Asperti 2017-12-27 22:44:11 +01:00
parent 516cc0b544
commit acf09f14b3
3 changed files with 77 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
telegram-notify.conf

13
TODO
View File

@ -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

View File

@ -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()