Skip to content

Commit

Permalink
Add @at_start to trigger actions when plugins initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
unode committed Aug 13, 2020
1 parent 4faa19a commit 70f6d81
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down Expand Up @@ -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!!")
8 changes: 8 additions & 0 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down
20 changes: 20 additions & 0 deletions mmpy_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -58,6 +59,7 @@ class PluginsManager(object):
'respond_to': {},
'listen_to': {}
}
_at_start = []

def __init__(self, plugins=None):
self.plugins = plugins or []
Expand Down Expand Up @@ -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."""
Expand All @@ -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

0 comments on commit 70f6d81

Please sign in to comment.