diff --git a/README.md b/README.md index 4566294..b3b7274 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,9 @@ $ pip install xbotlib Put the following in a `echo.py` file. ```python -from xbotlib import Bot +from xbotlib import EchoBot -class EchoBot(Bot): - def react(self, message): - if message.type == "chat": - self.reply(message.body, to=message.sender) - -MyBot() +EchotBot() ``` And then `python echo.py`. You will be asked a few questions like which account @@ -40,19 +35,40 @@ details your bot will be using. This will generate a `bot.conf` file in the same working directory for further use. -## More Examples +Here's the code for the `EchoBot`. -- **[EchoBot](./examples/echo.py)**: Sends back what you sent it -- **[WhisperBot](./examples/whisper.py)**: Pseudo-anonymous whispering in group chats +```python +class EchoBot(Bot): + """Gives back what you sent it. -See the [examples](./examples/) directoy for all listings. + Just direct message the bot and see if you get back what you sent. It also + works in group chats but in this case you need to summon the bot using its + nickname Usually like so. + + echobot:foo + + """ + + def react(self, message): + """Send back what we get.""" + if message.type == "chat": + self.reply(message.body, to=message.sender) +``` + +## All examples + +- **EchoBot**: Sends back what you sent it +- **WhisperBot**: Pseudo-anonymous whispering in group chats + +See [xbotlib.py](./xbotlib.py) for all example bots. ## 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. Seamlessness is still a bitch but we're trying anyway. +When writing your own bot, you 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. Seamlessness is still a bitch but we're +trying anyway. > Bot.react(message) diff --git a/examples/echo.py b/examples/echo.py deleted file mode 100644 index b7b888e..0000000 --- a/examples/echo.py +++ /dev/null @@ -1,17 +0,0 @@ -from xbotlib import Bot - - -class EchoBot(Bot): - """Gives back what you sent it. - - Just direct message the bot and see if you get back what you sent. - - """ - - def react(self, message): - """Send back what we get.""" - if message.type == "chat": - self.reply(message.body, to=message.sender) - - -EchoBot() diff --git a/examples/whisper.py b/examples/whisper.py deleted file mode 100644 index 90bde15..0000000 --- a/examples/whisper.py +++ /dev/null @@ -1,30 +0,0 @@ -from xbotlib import Bot - - -class WhisperBot(Bot): - """Pseudo-anonymous whispering in group chats. - - In order to activate this bot you can invite it to your group chat. Once - invited, you can directly message the bot outside of the group chat and - tell it you want it to whisper your message into the group chat. The bot - will then do this on your behalf and not reveal your identity. This is nice - when you want to communicate with the group somewhat anonymously. - - The bot accepts messages in the following form. - - whisper:: - - So, I might write it like so. - - whisper:myroom@muc.foo.com:i love the music of avril lavigne - - """ - - def react(self, message): - """Receive direct messages and pass them to group chats.""" - if message.type == "chat" and "whisper" in message.body: - _, room, whisper = message.body.split(":") - self.reply(f"*whispers* {whisper}", room=room) - - -WhisperBot() diff --git a/xbotlib.py b/xbotlib.py index fed25c1..10b6f17 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -176,3 +176,46 @@ class Bot(ClientXMPP): def react(self, message): message = "You need to write your own `react` implementation" raise NotImplementedError(message) + + +class EchoBot(Bot): + """Gives back what you sent it. + + Just direct message the bot and see if you get back what you sent. It also + works in group chats but in this case you need to summon the bot using its + nickname Usually like so. + + echobot:foo + + """ + + def react(self, message): + """Send back what we get.""" + if message.type == "chat": + self.reply(message.body, to=message.sender) + + +class WhisperBot(Bot): + """Pseudo-anonymous whispering in group chats. + + In order to activate this bot you can invite it to your group chat. Once + invited, you can directly message the bot outside of the group chat and + tell it you want it to whisper your message into the group chat. The bot + will then do this on your behalf and not reveal your identity. This is nice + when you want to communicate with the group somewhat anonymously. + + The bot accepts messages in the following form. + + whisper:: + + So, I might write it like so. + + whisper:myroom@muc.foo.com:i love the music of avril lavigne + + """ + + def react(self, message): + """Receive direct messages and pass them to group chats.""" + if message.type == "chat" and "whisper" in message.body: + _, room, whisper = message.body.split(":") + self.reply(f"*whispers* {whisper}", room=room)