From 68e3c454af2908bd69126b5a82aba179dd2aa370 Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Tue, 10 Dec 2019 19:03:19 +0200 Subject: [PATCH] Added cron module --- README.md | 23 +++++++++++++++++++++-- bot.py | 5 ++--- modules/cron.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 modules/cron.py diff --git a/README.md b/README.md index 205d822..da4fd6f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Simple example module that just echoes what user said. Aviation weather metar service access. -### TAF +### TAF Aviation weather TAF service access. @@ -39,7 +39,7 @@ When credentials.json is present, you must authenticate the bot to access calend Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=907.... ``` -Open the link and authenticate as needed. A new file token.pickle will be created in the directory and bot will read it in future. +Open the link and authenticate as needed. A new file token.pickle will be created in the directory and bot will read it in future. Now the bot should be usable. @@ -54,6 +54,21 @@ Commands: * !googlecal del [calendar id] - Delete calendar from room * !googlecal calendars - List calendars in this room +### Cron + +Can schedule things to be done. + +Commands: + +* !cron daily [hour] [command] - Run command on start of hour +* !cron list - List commands in this room +* !cron clear - Clear command s in this room + +Examples: + +* !cron daily 19 "It is now 19 o clock" +* !cron daily 8 "!googlecal today" + ## Bot setup * Create a Matrix user @@ -108,5 +123,9 @@ Functions: * matrix_stop - Called once before exit * async matrix_poll - Called every 10 seconds * help - Return one-liner help text +* get_settings - Must return a dict object that can be converted to JSON and sent to server +* set_settings - Load these settings. It should be the same JSON you returned in previous get_settings You only need to implement the ones you need. See existing bots for examples + +Settings are stored in Matrix account data. diff --git a/bot.py b/bot.py index 18ba0bf..9847953 100755 --- a/bot.py +++ b/bot.py @@ -57,7 +57,6 @@ class Bot: module_settings[modulename] = moduleobject.get_settings() except: traceback.print_exc(file=sys.stderr) - print('Collected module settings:', module_settings) data = { self.appid: self.version, 'module_settings': module_settings} self.set_account_data(data) @@ -153,8 +152,8 @@ class Bot: response = requests.get(ad_url) if response.status_code == 200: return response.json() - elif response.status_code != 200: - print('Getting account data failed:', response, response.json()) + print('Getting account data failed:', response, response.json()) + return None def init(self): self.client = AsyncClient(os.environ['MATRIX_SERVER'], os.environ['MATRIX_USER']) diff --git a/modules/cron.py b/modules/cron.py new file mode 100644 index 0000000..f6917ff --- /dev/null +++ b/modules/cron.py @@ -0,0 +1,46 @@ +import shlex +from datetime import datetime + +class MatrixModule: + daily_commands = dict() # room_id -> command json + last_hour = datetime.now().hour + + async def matrix_message(self, bot, room, event): + args = shlex.split(event.body) + args.pop(0) + if len(args) == 3: + if args[0] == 'daily': + dailytime = int(args[1]) + dailycmd = args[2] + if not self.daily_commands.get(room.room_id): + self.daily_commands[room.room_id] = [] + self.daily_commands[room.room_id].append({ 'time': dailytime, 'command': dailycmd }) + bot.save_settings() + await bot.send_text(room, 'Daily command added.') + elif len(args) == 1: + if args[0] == 'list': + await bot.send_text(room, 'Daily commands on this room: ' + str(self.daily_commands.get(room.room_id))) + elif args[0] == 'clear': + self.daily_commands.pop(room.room_id, None) + bot.save_settings() + await bot.send_text(room, 'Cleared commands on this room.') + + def help(self): + return('Runs scheduled commands') + + def get_settings(self): + return { 'daily_commands': self.daily_commands } + + def set_settings(self, data): + if data.get('daily_commands'): + self.daily_commands = data['daily_commands'] + + async def matrix_poll(self, bot, pollcount): + if self.last_hour != datetime.now().hour: + self.last_hour = datetime.now().hour + + for room_id in self.daily_commands: + commands = self.daily_commands[room_id] + for command in commands: + if int(command['time']) == self.last_hour: + await bot.send_text(bot.get_room_by_id(room_id), command['command'])