diff --git a/README.md b/README.md index 4cee841..aeccd6b 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/examples/echo.py b/examples/echo.py index 7ecb052..9b63c08 100644 --- a/examples/echo.py +++ b/examples/echo.py @@ -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() diff --git a/examples/whisper.py b/examples/whisper.py index 19fcb56..6b26900 100644 --- a/examples/whisper.py +++ b/examples/whisper.py @@ -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() diff --git a/xbotlib.py b/xbotlib.py index d9507ca..44a5148 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -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")