aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjason2016-08-18 18:40:32 -0600
committerjason2016-08-18 18:44:43 -0600
commit1fce7b900a05c46c6e7196dd0d555a9df763cc27 (patch)
treea95bb814fbca9e7b7c60ad775aa76e58d710b7a7
parentd3fc36c85d4b58bfb852c86ad09f95f8e24e4db3 (diff)
downloadwarmachine-ng-1fce7b900a05c46c6e7196dd0d555a9df763cc27.tar.gz
warmachine-ng-1fce7b900a05c46c6e7196dd0d555a9df763cc27.zip
Update README.org with instructions on writing a plugin
-rw-r--r--README.org52
-rw-r--r--warmachine/addons/base.py17
2 files changed, 68 insertions, 1 deletions
diff --git a/README.org b/README.org
index a968cbf..4d8bf43 100644
--- a/README.org
+++ b/README.org
@@ -1,3 +1,5 @@
1# LocalWords: asyncio async plugin
2
1* Setting up the bot 3* Setting up the bot
2D'bolla is a no bullshit extensible IRC/Slack bot written for Python 3.5 using 4D'bolla is a no bullshit extensible IRC/Slack bot written for Python 3.5 using
3asyncio. 5asyncio.
@@ -21,6 +23,56 @@ Simply run the command:
21#+END_SRC 23#+END_SRC
22 24
23* Writing a Plugin 25* Writing a Plugin
26To write a new plugin you must inherit from
27~warmachine.addons.base.WarMachinePlugin~. This class defines an interface you
28must implement in order to interact with connections. If you override ~__init__~
29you must call ~super()~.
30** ~self._loop~
31This is given to you access to the asyncio loop. This is useful for scheduling
32tasks to be run later.
33** ~self.log~
34This 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
37plugin every time a connection receives a message. It takes the arguments
38~connection~ and ~message~. ~connection~ is the Connection object which allows
39you to interact with that chat server. ~message~ is a dictionary containing
40information 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
50You will parse ~message['message']~ for what you are interested in and take some
51action based on what was found.
52
53For example:
54
55#+BEGIN_SRC python
56async 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)~
73This method is called when a connection successfully connects and takes the
74single argument ~connection~. Use this method to do any start up initialization
75you may want to do such as create configuration files that don't exist yet.
24* Writing a Connection 76* Writing a Connection
25To write a new connection protocol you must inherit from 77To 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
5class WarMachinePlugin(object): 5class 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__))