Added cron module

This commit is contained in:
Ville Ranki 2019-12-10 19:03:19 +02:00
parent 9431d7a267
commit 68e3c454af
3 changed files with 69 additions and 5 deletions

View File

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

3
bot.py
View File

@ -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())
return None
def init(self):
self.client = AsyncClient(os.environ['MATRIX_SERVER'], os.environ['MATRIX_USER'])

46
modules/cron.py Normal file
View File

@ -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'])