Docs docs docs
This commit is contained in:
parent
9767c7337e
commit
aefcfbab9c
48
README.md
48
README.md
@ -14,8 +14,8 @@ $ pip install xbotlib
|
|||||||
from xbotlib import Bot
|
from xbotlib import Bot
|
||||||
|
|
||||||
class EchoBot(Bot):
|
class EchoBot(Bot):
|
||||||
def react(self, msg):
|
def reply_direct_chat(self, message):
|
||||||
self.reply(to=message.sender, body=message.body)
|
self.send_direct_chat(to=message.sender, body=message.body)
|
||||||
|
|
||||||
MyBot()
|
MyBot()
|
||||||
```
|
```
|
||||||
@ -28,3 +28,47 @@ And then `python echo.py`.
|
|||||||
- **[WhisperBot](./examples/whisper.py)**: Pseudo-anonymous whispering in group chats
|
- **[WhisperBot](./examples/whisper.py)**: Pseudo-anonymous whispering in group chats
|
||||||
|
|
||||||
See the [examples](./examples/) directoy for all listings.
|
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).
|
||||||
|
@ -8,9 +8,9 @@ class EchoBot(Bot):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def react(self, message):
|
def reply_direct_chat(self, message):
|
||||||
"""Send back what we get."""
|
"""Send back what we get."""
|
||||||
self.reply(to=message.sender, body=message.body)
|
self.send_direct_chat(to=message.sender, body=message.body)
|
||||||
|
|
||||||
|
|
||||||
EchoBot()
|
EchoBot()
|
||||||
|
@ -16,19 +16,15 @@ class WhisperBot(Bot):
|
|||||||
|
|
||||||
So, I might write it like so.
|
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."""
|
"""Receive direct messages and pass them to group chats."""
|
||||||
if message.type == "groupchat":
|
|
||||||
return
|
|
||||||
|
|
||||||
if "whisper" in message.body:
|
if "whisper" in message.body:
|
||||||
_, room, whisper = message.body.split(":")
|
_, room, whisper = message.body.split(":")
|
||||||
body = f"*whispers* {whisper}"
|
self.send_group_chat(to=room, body=f"*whispers* {whisper}")
|
||||||
self.reply(to=room, body=body, type="groupchat")
|
|
||||||
|
|
||||||
|
|
||||||
WhisperBot()
|
WhisperBot()
|
||||||
|
16
xbotlib.py
16
xbotlib.py
@ -20,7 +20,7 @@ class EasyMessage:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def sender(self):
|
def sender(self):
|
||||||
return self.message["from"]
|
return self.message["from"].bare
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def receiver(self):
|
def receiver(self):
|
||||||
@ -90,7 +90,7 @@ class Bot(ClientXMPP):
|
|||||||
def message(self, message):
|
def message(self, message):
|
||||||
"""Handle message event."""
|
"""Handle message event."""
|
||||||
if message["type"] in ("chat", "normal"):
|
if message["type"] in ("chat", "normal"):
|
||||||
self.react(EasyMessage(message))
|
self.reply_direct_chat(EasyMessage(message))
|
||||||
|
|
||||||
def session_start(self, event):
|
def session_start(self, event):
|
||||||
"""Handle session_start event."""
|
"""Handle session_start event."""
|
||||||
@ -107,7 +107,7 @@ class Bot(ClientXMPP):
|
|||||||
"""Handle groupchat_message event."""
|
"""Handle groupchat_message event."""
|
||||||
if message["type"] in ("groupchat", "normal"):
|
if message["type"] in ("groupchat", "normal"):
|
||||||
if message["mucnick"] != self.config["bot"]["nick"]:
|
if message["mucnick"] != self.config["bot"]["nick"]:
|
||||||
self.react(EasyMessage(message))
|
self.reply_group_chat(EasyMessage(message))
|
||||||
|
|
||||||
def register_xmpp_plugins(self):
|
def register_xmpp_plugins(self):
|
||||||
"""Register XMPP plugins that the bot supports."""
|
"""Register XMPP plugins that the bot supports."""
|
||||||
@ -124,6 +124,10 @@ class Bot(ClientXMPP):
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def reply(self, to, body, type="chat"):
|
def send_direct_chat(self, to, body):
|
||||||
"""Send a message."""
|
"""Reply to a direct chat message."""
|
||||||
self.send_message(mto=to, mbody=body, mtype=type)
|
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")
|
||||||
|
Reference in New Issue
Block a user