From 70f6d8188a31b9d6f8d9ffc4201737f0db64f5f0 Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Thu, 13 Aug 2020 02:19:27 +0200 Subject: [PATCH] Add @at_start to trigger actions when plugins initialize fixes #132 --- docs/decorators.rst | 18 ++++++++++++++++++ docs/plugins.rst | 8 ++++++++ mmpy_bot/bot.py | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/docs/decorators.rst b/docs/decorators.rst index a8ede839..3a4ec90d 100644 --- a/docs/decorators.rst +++ b/docs/decorators.rst @@ -30,6 +30,17 @@ Direct messages:: +Actions just after plugin initilization :: + + from mmpy_bot.bot import at_start + + + @at_start + def hello(client): + client.connect_websocket() + client.ping() + + Real case --------- @@ -61,3 +72,10 @@ So now we can close access to some functions on plugins:: def sms_to(message, name): # some code message.reply('Sms was sent to %s' % name) + + + @at_start + def hello(client): + team = client.api.get_team_by_name("TESTTEAM") + channel = client.api.get_channel_by_name("bot_test", team["id"]) + client.channel_msg(channel["id"], "Hello, feels good to be alive!!") diff --git a/docs/plugins.rst b/docs/plugins.rst index 400069da..ae6356f4 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -11,6 +11,7 @@ To write a new plugin, simply create a function decorated by ``mmpy_bot.bot.resp - A function decorated with ``respond_to`` is called when a message matching the pattern is sent to the bot (direct message or @botname in a channel/group chat) - A function decorated with ``listen_to`` is called when a message matching the pattern is sent on a channel/group chat (not directly sent to the bot) +- A function decorated with ``at_start`` is called as soon as the plugin is initialized (when the bot starts) .. code-block:: python @@ -38,6 +39,13 @@ To write a new plugin, simply create a function decorated by ``mmpy_bot.bot.resp # Message is sent on the channel # message.send('I can help everybody!') + @at_start + def hello(client): + # Note that contrary to respond_to and listen_to @at_start + # receives a client object and not a message object + team = client.api.get_team_by_name("TESTTEAM") + channel = client.api.get_channel_by_name("bot_test", team["id"]) + client.channel_msg(channel["id"], "Hello, feels good to be alive!!") To extract params from the message, you can use regular expression: diff --git a/mmpy_bot/bot.py b/mmpy_bot/bot.py index 7e3c352d..f1f7d9fe 100644 --- a/mmpy_bot/bot.py +++ b/mmpy_bot/bot.py @@ -35,6 +35,7 @@ def __init__(self): def run(self): self._plugins.init_plugins() + self._plugins.trigger_at_start(self._client) self._dispatcher.start() _thread.start_new_thread(self._keep_active, tuple()) _thread.start_new_thread(self._run_jobs, tuple()) @@ -58,6 +59,7 @@ class PluginsManager(object): 'respond_to': {}, 'listen_to': {} } + _at_start = [] def __init__(self, plugins=None): self.plugins = plugins or [] @@ -107,6 +109,13 @@ def get_plugins(self, category, text): if not has_matching_plugin: yield None, None + def trigger_at_start(self, client): + for func in self._at_start: + try: + func(client) + except Exception as err: + logger.exception(err) + class Matcher(object): """This allows us to map the same regex to multiple handlers.""" @@ -132,3 +141,14 @@ def respond_to(regexp, flags=0): def listen_to(regexp, flags=0): return get_wrapper('listen_to', regexp, flags) + + +def at_start(): + def wrapper(func): + PluginsManager._at_start.append(func) + logger.info( + 'registered %s plugin "%s"', + "at_start", func.__name__) + return func + + return wrapper