diff options
Diffstat (limited to 'README.org')
| -rw-r--r-- | README.org | 52 |
1 files changed, 52 insertions, 0 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 |