add subcommands list-bots, add-bot, del-bot
This commit is contained in:
parent
413d879fae
commit
c8f2e12bd6
|
@ -32,6 +32,7 @@ class BotModule(ABC):
|
|||
self.can_be_disabled = True
|
||||
self.name = name
|
||||
self.logger = logging.getLogger("module " + self.name)
|
||||
self.subcommands = dict()
|
||||
|
||||
def matrix_start(self, bot):
|
||||
"""Called once on startup
|
||||
|
@ -100,4 +101,4 @@ class BotModule(ABC):
|
|||
self.enabled = True
|
||||
|
||||
def disable(self):
|
||||
self.enabled = False
|
||||
self.enabled = False
|
||||
|
|
|
@ -38,15 +38,27 @@ class MatrixModule(BotModule):
|
|||
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.known_bots = []
|
||||
self.bot = None
|
||||
self.parser = MatterBridgeParser()
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
bot.must_be_admin(room, event)
|
||||
|
||||
# todo: add subcommand to add administrators
|
||||
# todo: add subcommand to add known matterbridge bot
|
||||
args = event.body.split()
|
||||
|
||||
if len(args) == 2:
|
||||
if args[1] == 'list-bots':
|
||||
await self.list_bots(room)
|
||||
if len(args) > 2:
|
||||
if args[1] == 'add-bot':
|
||||
self.add_bot(args[2])
|
||||
elif args[1] == 'del-bot':
|
||||
self.del_bot(args[2])
|
||||
|
||||
# todo: needs a mechanism to hook into admin check of the bot
|
||||
pass
|
||||
|
||||
def help(self):
|
||||
return "parses matterbridge messages and delegate the parsed message to the bot"
|
||||
|
@ -60,10 +72,21 @@ class MatrixModule(BotModule):
|
|||
super().matrix_stop(bot)
|
||||
bot.remove_callback(self.dispatch_cb)
|
||||
|
||||
def get_settings(self):
|
||||
data = super().get_settings()
|
||||
data['known_bots'] = self.known_bots
|
||||
return data
|
||||
|
||||
def set_settings(self, data):
|
||||
super().set_settings(data)
|
||||
if data.get('known_bots'):
|
||||
self.known_bots = data['known_bots']
|
||||
|
||||
async def dispatch_cb(self, room, event):
|
||||
|
||||
# todo: only accept messages from matterbridge bot
|
||||
# like event.sender in self.known_matterbridge_bots
|
||||
if event.sender not in self.known_bots:
|
||||
self.logger.debug(f"{event.sender} is not a known bot. skip processing.")
|
||||
return
|
||||
|
||||
# no content at all?
|
||||
if len(event.body) < 1:
|
||||
|
@ -74,7 +97,27 @@ class MatrixModule(BotModule):
|
|||
if matter_message is None:
|
||||
return
|
||||
|
||||
self.logger.info(f"room: {room.name} protocol: {matter_message.protocol} user: {matter_message.username} - dispatch matterbridge message to bot")
|
||||
self.logger.info(
|
||||
f"room: {room.name} protocol: {matter_message.protocol} user: {matter_message.username} - dispatch matterbridge message to bot")
|
||||
|
||||
# dispatch. changing the body of the event triggers message_cb of the bot
|
||||
event.body = matter_message.message
|
||||
|
||||
def add_bot(self, bot_name):
|
||||
self.logger.info("Add bot %s to known bots", bot_name)
|
||||
self.known_bots.append(bot_name)
|
||||
self.bot.save_settings()
|
||||
|
||||
def del_bot(self, bot_name):
|
||||
if bot_name in self.known_bots:
|
||||
self.logger.info("Delete bot %s from list of known bots", bot_name)
|
||||
self.known_bots.remove(bot_name)
|
||||
self.bot.save_settings()
|
||||
else:
|
||||
self.logger.warning("%s not in list of known bots. skip delete.", bot_name)
|
||||
|
||||
async def list_bots(self, room):
|
||||
|
||||
await self.bot.send_text(room, f'Known Matterbridge Bots: {len(self.known_bots)}')
|
||||
for bot_name in self.known_bots:
|
||||
await self.bot.send_text(room, bot_name)
|
||||
|
|
Loading…
Reference in New Issue