Docs docs docs

This commit is contained in:
Luke Murphy 2021-01-10 16:51:20 +01:00
parent 9767c7337e
commit aefcfbab9c
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
4 changed files with 61 additions and 17 deletions

View File

@ -14,8 +14,8 @@ $ pip install xbotlib
from xbotlib import Bot
class EchoBot(Bot):
def react(self, msg):
self.reply(to=message.sender, body=message.body)
def reply_direct_chat(self, message):
self.send_direct_chat(to=message.sender, body=message.body)
MyBot()
```
@ -28,3 +28,47 @@ And then `python echo.py`.
- **[WhisperBot](./examples/whisper.py)**: Pseudo-anonymous whispering in group chats
See the [examples](./examples/) directoy for all listings.
## API Reference
Your bot always sub-classes the `Bot` class provided from `xbotlib`. All
underling functions can be extended. For example, if you want to enable more
plugins or add different functionality. If something feels awkward then please
raise a ticket for that. See the one file [source](./xbotlib.py) here.
### send_direct_chat
Send back a response to a direct chat message.
Arguments:
- **to**: who to send it to (can be a user or a room)
- **body**: the message to send
### send_group_chat
Send back a response to a group chat message.
Arguments:
- **to**: who to send it to (can be a user or a room)
- **body**: the message to send
## Roadmap
- The library only handles reactions. The bots can only send messages when they
receive a message. It would be nice to allow for sending messages at specific
times.
- Extend the `bot.conf` to allow for multiple bot configurations.
- Sort out something for how to deploy them. It's easy to run them locally but
hard to run them on server. Maybe something can be done for that as well.
## Changes
See the [CHANGELOG.md](./CHANGELOG.md).
## License
See the [LICENSE](./LICENSE.md).

View File

@ -8,9 +8,9 @@ class EchoBot(Bot):
"""
def react(self, message):
def reply_direct_chat(self, message):
"""Send back what we get."""
self.reply(to=message.sender, body=message.body)
self.send_direct_chat(to=message.sender, body=message.body)
EchoBot()

View File

@ -16,19 +16,15 @@ class WhisperBot(Bot):
So, I might write it like so.
whisper:myroom@muc.foo.com:hey, i actually really like avril lavigne!
whisper:myroom@muc.foo.com:i love the music of avril lavigne
"""
def react(self, message):
def reply_direct_chat(self, message):
"""Receive direct messages and pass them to group chats."""
if message.type == "groupchat":
return
if "whisper" in message.body:
_, room, whisper = message.body.split(":")
body = f"*whispers* {whisper}"
self.reply(to=room, body=body, type="groupchat")
self.send_group_chat(to=room, body=f"*whispers* {whisper}")
WhisperBot()

View File

@ -20,7 +20,7 @@ class EasyMessage:
@property
def sender(self):
return self.message["from"]
return self.message["from"].bare
@property
def receiver(self):
@ -90,7 +90,7 @@ class Bot(ClientXMPP):
def message(self, message):
"""Handle message event."""
if message["type"] in ("chat", "normal"):
self.react(EasyMessage(message))
self.reply_direct_chat(EasyMessage(message))
def session_start(self, event):
"""Handle session_start event."""
@ -107,7 +107,7 @@ class Bot(ClientXMPP):
"""Handle groupchat_message event."""
if message["type"] in ("groupchat", "normal"):
if message["mucnick"] != self.config["bot"]["nick"]:
self.react(EasyMessage(message))
self.reply_group_chat(EasyMessage(message))
def register_xmpp_plugins(self):
"""Register XMPP plugins that the bot supports."""
@ -124,6 +124,10 @@ class Bot(ClientXMPP):
except KeyboardInterrupt:
pass
def reply(self, to, body, type="chat"):
"""Send a message."""
self.send_message(mto=to, mbody=body, mtype=type)
def send_direct_chat(self, to, body):
"""Reply to a direct chat message."""
self.send_message(mto=to, mbody=body, mtype="chat")
def send_group_chat(self, to, body):
"""Reply to a group chat message."""
self.send_message(mto=to, mbody=body, mtype="groupchat")