added logging
This commit is contained in:
parent
0c01c20880
commit
0388bf7ad2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
telegram-notify.conf
|
telegram-notify.conf
|
||||||
|
telegram-notify.log
|
||||||
|
187
telegram-notify
187
telegram-notify
@ -10,16 +10,24 @@ import json
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
from telepot.loop import MessageLoop
|
from telepot.loop import MessageLoop
|
||||||
from watchdog.observers import Observer
|
from watchdog.observers import Observer
|
||||||
from watchdog.events import PatternMatchingEventHandler
|
from watchdog.events import PatternMatchingEventHandler
|
||||||
|
|
||||||
version = 0.1
|
version = 0.1
|
||||||
|
|
||||||
|
|
||||||
def writeconfig():
|
def writeconfig():
|
||||||
global configParser
|
if not os.access(configFile, os.W_OK):
|
||||||
with open('telegram-notify.conf', 'wb') as configfile:
|
print 'WARNING: Config file (%s) is not writable ...' % configFile
|
||||||
configParser.write(configfile)
|
logger.warning('Config file (%s) is not writable ...' % configFile)
|
||||||
|
return False
|
||||||
|
|
||||||
|
with open(configFile, 'wb') as theFile:
|
||||||
|
configParser.write(theFile)
|
||||||
|
logger.info('Config file (%s) updated ...' % configFile)
|
||||||
|
|
||||||
|
|
||||||
def handle(msg):
|
def handle(msg):
|
||||||
global configParser
|
global configParser
|
||||||
@ -105,25 +113,28 @@ def handle(msg):
|
|||||||
|
|
||||||
|
|
||||||
def processFile(filename):
|
def processFile(filename):
|
||||||
global chat_ids
|
logger.info('Processing %s' % filename)
|
||||||
if re.match(r'.*jpg$',filename):
|
if re.match(r'.*jpg$',filename):
|
||||||
for c in chat_ids:
|
for c in chat_ids:
|
||||||
try:
|
try:
|
||||||
|
logger.debug('sending %s as picture to %s' % (filename,c['username']))
|
||||||
r=bot.sendPhoto(c['id'], open(filename, 'rb'))
|
r=bot.sendPhoto(c['id'], open(filename, 'rb'))
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
logger.debug('removed %s' % filename)
|
||||||
except:
|
except:
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
if re.match(r'.*txt$',filename):
|
if re.match(r'.*txt$',filename):
|
||||||
for c in chat_ids:
|
for c in chat_ids:
|
||||||
try:
|
try:
|
||||||
with open(filename,'r') as f:
|
with open(filename,'r') as f:
|
||||||
|
logger.debug('sending %s as text to %s' % (filename,c['username']))
|
||||||
r=bot.sendMessage(c['id'], f.read())
|
r=bot.sendMessage(c['id'], f.read())
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
logger.debug('removed %s' % filename)
|
||||||
except:
|
except:
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FilesChangedHandler(PatternMatchingEventHandler):
|
class FilesChangedHandler(PatternMatchingEventHandler):
|
||||||
patterns = ["*.jpg", "*.txt"]
|
patterns = ["*.jpg", "*.txt"]
|
||||||
|
|
||||||
@ -147,88 +158,136 @@ class FilesChangedHandler(PatternMatchingEventHandler):
|
|||||||
self.process(event)
|
self.process(event)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def checkFiles():
|
def checkFiles():
|
||||||
|
spool_dir = configParser.get('general', 'spool_dir')
|
||||||
|
logger.info('Checking %s for new files ...' % spool_dir)
|
||||||
l=os.listdir(spool_dir)
|
l=os.listdir(spool_dir)
|
||||||
for f in l:
|
for f in l:
|
||||||
processFile("%s/%s" % (spool_dir,f) )
|
processFile("%s/%s" % (spool_dir,f) )
|
||||||
|
|
||||||
|
|
||||||
configFile = '/etc/telegram-notify/telegram-notify.conf'
|
def setDefaults():
|
||||||
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
global configFile
|
||||||
configFile = './telegram-notify.conf'
|
global defaults
|
||||||
|
configFile = '/etc/telegram-notify/telegram-notify.conf'
|
||||||
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
||||||
configFile = ''
|
configFile = './telegram-notify.conf'
|
||||||
|
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
||||||
|
configFile = ''
|
||||||
|
logFile = '/var/log/telegram-notify.log'
|
||||||
|
if not os.path.isfile(logFile) or not os.access(logFile, os.W_OK):
|
||||||
|
logFile = ''
|
||||||
|
defaults = {
|
||||||
|
'log_file': logFile,
|
||||||
|
'log_level': 'info',
|
||||||
|
'daemon': False,
|
||||||
|
'token': '',
|
||||||
|
'pair_pin': '1234',
|
||||||
|
'spool_dir': '/var/spool/telegram-notify'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Daemon for Telegram notification management')
|
def parseCmdLine():
|
||||||
parser.add_argument('-c', '--config', nargs='?',
|
global configFile
|
||||||
help='start with specified config file')
|
|
||||||
parser.add_argument('-V', '--version', action='store_true',
|
|
||||||
help='show program version and quit')
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
if args.version:
|
parser = argparse.ArgumentParser(description='Daemon for Telegram notification management')
|
||||||
print 'telegram-notify version %s - Copyright (C) 2018 by Paolo Asperti.' % version
|
parser.add_argument('-c', '--config', nargs='?',
|
||||||
sys.exit(0)
|
help='start with specified config file (default: telegram-notify.conf in /etc/ or in current directory)')
|
||||||
|
parser.add_argument('-V', '--version', action='store_true',
|
||||||
|
help='show program version and quit')
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.config:
|
if args.version:
|
||||||
if not os.path.isfile(args.config):
|
print 'telegram-notify version %s - Copyright (C) 2018 by Paolo Asperti.' % version
|
||||||
print 'specified config file doesn\'t exists or is not a file'
|
sys.exit(0)
|
||||||
|
|
||||||
|
if args.config:
|
||||||
|
if not os.path.isfile(args.config):
|
||||||
|
print 'specified config file doesn\'t exists or is not a file'
|
||||||
|
sys.exit(1)
|
||||||
|
if not os.access(args.config, os.R_OK):
|
||||||
|
print 'specified config file is not readable'
|
||||||
|
sys.exit(1)
|
||||||
|
configFile = args.config
|
||||||
|
|
||||||
|
|
||||||
|
def readConfigFile():
|
||||||
|
global configParser
|
||||||
|
if configFile == '':
|
||||||
|
print 'no config file available'
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if not os.access(args.config, os.R_OK):
|
|
||||||
print 'specified config file is not readable'
|
if not os.access(configFile, os.W_OK):
|
||||||
|
print 'WARNING: specified config file is not writable'
|
||||||
|
|
||||||
|
configParser = ConfigParser.SafeConfigParser(defaults=defaults)
|
||||||
|
configParser.read(configFile)
|
||||||
|
|
||||||
|
|
||||||
|
def initLogger():
|
||||||
|
global logger
|
||||||
|
|
||||||
|
logFile = configParser.get('general', 'log_file')
|
||||||
|
if logFile == '':
|
||||||
|
print 'no log file available'
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if not os.access(args.config, os.W_OK):
|
logLevel = configParser.get('general', 'log_level')
|
||||||
# TODO: logging
|
logging.basicConfig(filename=logFile,level=logLevel,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
print 'WARNING: specified config file is not wriable'
|
logger = logging.getLogger(__name__)
|
||||||
configFile = args.config
|
|
||||||
|
|
||||||
if configFile == '':
|
|
||||||
print 'no config file available'
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: logging
|
def checkSpoolDir():
|
||||||
print 'INFO: using config file: %s' % configFile
|
spool_dir = configParser.get('general', 'spool_dir')
|
||||||
configParser = ConfigParser.RawConfigParser()
|
if not os.path.isdir(spool_dir):
|
||||||
configParser.read(configFile)
|
print "spool directory (%s) doesn't exists or is not a directory!" % spool_dir
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
spool_dir = configParser.get('general', 'spool_dir')
|
if not os.access(spool_dir, os.W_OK):
|
||||||
if not os.path.isdir(spool_dir):
|
print "spool directory (%s) is not writable!" % spool_dir
|
||||||
print "spool directory (%s) doesn't exists or is not a directory!" % spool_dir
|
sys.exit(1)
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if not os.access(spool_dir, os.W_OK):
|
|
||||||
print "spool directory (%s) is not writable!" % spool_dir
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
chat_ids=[]
|
def initBot():
|
||||||
print "Allowed users: "
|
global bot
|
||||||
for p in configParser.sections():
|
global chat_ids
|
||||||
if re.match('^id-.*',p):
|
chat_ids=[]
|
||||||
chat_id={
|
logger.info('Allowed users:')
|
||||||
'username': configParser.get(p,'username'),
|
for p in configParser.sections():
|
||||||
'firstname': configParser.get(p,'firstname'),
|
if re.match('^id-.*',p):
|
||||||
'lastname': configParser.get(p,'lastname'),
|
chat_id={
|
||||||
'id': int(re.sub(r'^id-','',p))
|
'username': configParser.get(p,'username'),
|
||||||
}
|
'firstname': configParser.get(p,'firstname'),
|
||||||
chat_ids.append(chat_id)
|
'lastname': configParser.get(p,'lastname'),
|
||||||
print(" - (%s) %s %s" % (chat_id['username'],chat_id['firstname'],chat_id['lastname']) )
|
'id': int(re.sub(r'^id-','',p))
|
||||||
|
}
|
||||||
|
chat_ids.append(chat_id)
|
||||||
|
logger.debug(" - (%s) %s %s" % (chat_id['username'],chat_id['firstname'],chat_id['lastname']) )
|
||||||
|
|
||||||
token = configParser.get('general', 'token')
|
token = configParser.get('general', 'token')
|
||||||
bot = telepot.Bot(token)
|
bot = telepot.Bot(token)
|
||||||
|
|
||||||
MessageLoop(bot, handle).run_as_thread()
|
MessageLoop(bot, handle).run_as_thread()
|
||||||
print 'I am listening ...'
|
logger.info('Loop started. I am listening ...')
|
||||||
|
|
||||||
checkFiles()
|
checkFiles()
|
||||||
|
|
||||||
observer = Observer()
|
spool_dir = configParser.get('general', 'spool_dir')
|
||||||
observer.schedule(FilesChangedHandler(), path=spool_dir)
|
observer = Observer()
|
||||||
observer.start()
|
observer.schedule(FilesChangedHandler(), path=spool_dir)
|
||||||
|
observer.start()
|
||||||
|
|
||||||
|
|
||||||
|
setDefaults()
|
||||||
|
parseCmdLine()
|
||||||
|
readConfigFile()
|
||||||
|
initLogger()
|
||||||
|
checkSpoolDir()
|
||||||
|
initBot()
|
||||||
|
|
||||||
|
daemon = configParser.get('general', 'daemon')
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(10)
|
|
||||||
checkFiles()
|
checkFiles()
|
||||||
|
if re.match('false', daemon, re.IGNORECASE):
|
||||||
|
sys.exit(0)
|
||||||
|
time.sleep(10)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
[general]
|
[general]
|
||||||
daemon = false
|
daemon = true
|
||||||
token = aaaaaaaaaaaaaaaaaaaaaaaaa
|
token = aaaaaaaaaaaaaaaaaaaaaaaaa
|
||||||
pair_pin = 1234
|
pair_pin = 1234
|
||||||
spool_dir = /var/spool/telegram-notify
|
spool_dir = /var/spool/telegram-notify
|
||||||
|
log_file = /var/log/telegram-notify.log
|
||||||
|
log_level = INFO
|
||||||
|
Loading…
Reference in New Issue
Block a user