diff options
| author | jason | 2016-08-18 18:40:32 -0600 |
|---|---|---|
| committer | jason | 2016-08-18 18:44:43 -0600 |
| commit | 1fce7b900a05c46c6e7196dd0d555a9df763cc27 (patch) | |
| tree | a95bb814fbca9e7b7c60ad775aa76e58d710b7a7 | |
| parent | d3fc36c85d4b58bfb852c86ad09f95f8e24e4db3 (diff) | |
| download | warmachine-ng-1fce7b900a05c46c6e7196dd0d555a9df763cc27.tar.gz warmachine-ng-1fce7b900a05c46c6e7196dd0d555a9df763cc27.zip | |
Update README.org with instructions on writing a plugin
| -rw-r--r-- | README.org | 52 | ||||
| -rw-r--r-- | warmachine/addons/base.py | 17 |
2 files changed, 68 insertions, 1 deletions
| @@ -1,3 +1,5 @@ | |||
| 1 | # LocalWords: asyncio async plugin | ||
| 2 | |||
| 1 | * Setting up the bot | 3 | * Setting up the bot |
| 2 | D'bolla is a no bullshit extensible IRC/Slack bot written for Python 3.5 using | 4 | D'bolla is a no bullshit extensible IRC/Slack bot written for Python 3.5 using |
| 3 | asyncio. | 5 | asyncio. |
| @@ -21,6 +23,56 @@ Simply run the command: | |||
| 21 | #+END_SRC | 23 | #+END_SRC |
| 22 | 24 | ||
| 23 | * Writing a Plugin | 25 | * Writing a Plugin |
| 26 | To write a new plugin you must inherit from | ||
| 27 | ~warmachine.addons.base.WarMachinePlugin~. This class defines an interface you | ||
| 28 | must implement in order to interact with connections. If you override ~__init__~ | ||
| 29 | you must call ~super()~. | ||
| 30 | ** ~self._loop~ | ||
| 31 | This is given to you access to the asyncio loop. This is useful for scheduling | ||
| 32 | tasks to be run later. | ||
| 33 | ** ~self.log~ | ||
| 34 | This is a logger that you should use when logging messages. | ||
| 35 | ** ~recv_msg(channel, message)~ | ||
| 36 | ~recv_msg~ is the only required method for a plugin. It is called for every | ||
| 37 | plugin every time a connection receives a message. It takes the arguments | ||
| 38 | ~connection~ and ~message~. ~connection~ is the Connection object which allows | ||
| 39 | you to interact with that chat server. ~message~ is a dictionary containing | ||
| 40 | information about the message received. It has the following format: | ||
| 41 | |||
| 42 | #+BEGIN_SRC python | ||
| 43 | { | ||
| 44 | 'sender': 'sender nickname', | ||
| 45 | 'channel': '#channel_name or None for private messages', | ||
| 46 | 'message': 'The message that was received', | ||
| 47 | } | ||
| 48 | #+END_SRC | ||
| 49 | |||
| 50 | You will parse ~message['message']~ for what you are interested in and take some | ||
| 51 | action based on what was found. | ||
| 52 | |||
| 53 | For example: | ||
| 54 | |||
| 55 | #+BEGIN_SRC python | ||
| 56 | async def recv_msg(connection, message): | ||
| 57 | """ | ||
| 58 | A simple echo 'server'. | ||
| 59 | """ | ||
| 60 | # if the message starts with !echo, and it was said in a channel (not a | ||
| 61 | # private message)... | ||
| 62 | if message['message'].startswith('!echo') and message['channel']: | ||
| 63 | # Strip the command !echo from the message | ||
| 64 | try: | ||
| 65 | msg_to_repeat = ' '.join(message['message'].split(' ')[1:]) | ||
| 66 | except IndexError: | ||
| 67 | # There was nothing to echo | ||
| 68 | return | ||
| 69 | # Repeat the remainder of the received message | ||
| 70 | await connection.say(msg_to_repeat, message['channel']) | ||
| 71 | #+END_SRC | ||
| 72 | ** ~on_connect(connection)~ | ||
| 73 | This method is called when a connection successfully connects and takes the | ||
| 74 | single argument ~connection~. Use this method to do any start up initialization | ||
| 75 | you may want to do such as create configuration files that don't exist yet. | ||
| 24 | * Writing a Connection | 76 | * Writing a Connection |
| 25 | To write a new connection protocol you must inherit from | 77 | To write a new connection protocol you must inherit from |
| 26 | ~warmachine.connections.base.Connection~. This class defines an interface you | 78 | ~warmachine.connections.base.Connection~. This class defines an interface you |
diff --git a/warmachine/addons/base.py b/warmachine/addons/base.py index e605c2c..0a02df7 100644 --- a/warmachine/addons/base.py +++ b/warmachine/addons/base.py | |||
| @@ -3,6 +3,21 @@ import logging | |||
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | class WarMachinePlugin(object): | 5 | class WarMachinePlugin(object): |
| 6 | def __init__(self): | 6 | def __init__(self, *args, **kwargs): |
| 7 | self._loop = asyncio.get_event_loop() | 7 | self._loop = asyncio.get_event_loop() |
| 8 | self.log = logging.getLogger(self.__class__.__name__) | 8 | self.log = logging.getLogger(self.__class__.__name__) |
| 9 | |||
| 10 | def recv_msg(self, *args, **kwargs): | ||
| 11 | """ | ||
| 12 | Called when a connection receives a message. Arguments are | ||
| 13 | ``connection`` and ``message``. | ||
| 14 | |||
| 15 | ``connection`` is a :class:`warmachine.connections.base.Connection` | ||
| 16 | object used to interact with the connection to a chat server. | ||
| 17 | ``message`` is a dictionary that contains information about a message | ||
| 18 | that was received on the connection. See | ||
| 19 | :class:`warmachine.connections.base.Connection.read` for more | ||
| 20 | information. | ||
| 21 | """ | ||
| 22 | raise NotImplementedError('{} must implement `recv_msg` method'.format( | ||
| 23 | self.__class__.__name__)) | ||