diff --git a/README.md b/README.md index 3cef712..fa51263 100644 --- a/README.md +++ b/README.md @@ -49,15 +49,19 @@ $ pip install xbotlib ## Example -Put the following in a `echo.py` file. `xbotlib` provides a number of example -bots which you can use to get moving fast and try things out. This bot is the -simplest one we have: it simply echoes back whatever message you send it. It is -an easy way to get started. +Put the following in a `echo.py` file. This bot is pretty simple: it echoes +back whatever message you send it. It is an easy way to get started. ```python -from xbotlib import EchoBot +from xbotlib import Bot -EchotBot() +class EchoBot(Bot): + + def direct(self, message): + self.reply(message.text, to=message.sender) + + def group(self, message): + self.reply(message.content, room=message.room) ``` And then `python echo.py`. You will be asked a few questions in order to load @@ -65,37 +69,8 @@ the account details that your bot will be using. This will generate an `echobot.conf` file in the same working directory for further use. See the [configuration](#configure-your-bot) section for more. -Here's the annotated source for the `EchoBot`. - -```python -from xbotlib import Bot - -# Define a class and inherit from xbotlib.Bot -class EchoBot(Bot): - - # Respond to direct messages using the `direct` function - def direct(self, message): - # Send back the entire text of the message using `message.text` - self.reply(message.text, to=message.sender) - - # Respond to group chat messages using the `group` function - def group(self, message): - # Send back the content of the message. In this case if we were - # sent `echobot, hoi` in the group chat then the `message.content` - # would be equal to `hoi` - self.reply(message.content, room=message.room) -``` - Read more in the [API reference](#api-reference) for how to write your own bots. -A numer of other example bots are provided. - -- **EchoBot**: Sends back what you sent it -- **WhisperBot**: Anonymous whispering in group chats -- **GlossBot**: Building a shared glossary together - -See [xbotlib.py](./xbotlib.py) for the sources. - ## API Reference When writing your own bot, you always sub-class the `Bot` class provided from diff --git a/xbotlib.py b/xbotlib.py index ea0bdc6..9699b89 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -11,7 +11,6 @@ from logging import DEBUG, INFO, basicConfig, getLogger from os import environ from os.path import exists from pathlib import Path -from random import choice from sys import exit, stdout from aiohttp.web import Application, Response, get, run_app @@ -554,120 +553,3 @@ class Bot(ClientXMPP): return self.reply(cleandoc(self.help), **kwargs) except AttributeError: return self.reply("No help found 🤔️", **kwargs) - - -class EchoBot(Bot): - """Responds with whatever you send. - - Simply 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. - """ - - help = "I echo messages back 🖖️" - - def direct(self, message): - """Send back whatever we receive.""" - self.reply(message.text, to=message.sender) - - def group(self, message): - """Send back whatever receive in group chats.""" - self.reply(message.content, room=message.room) - - -class WhisperBot(Bot): - """Anonymous whispering in group chats. - - In order to activate this bot you can invite it to your group chat. Once - invited, you can start a private chat with the bot 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 anonymously. - """ - - help = "I whisper private messages into group chats 😌️" - - def direct(self, message): - """Receive private messages and whisper them into group chats.""" - self.reply(f"*pssttt...* {message.content}", room=message.room) - - -class GlossBot(Bot): - """Building a shared glossary together. - - A glossary is "an alphabetical list of terms in a particular domain of - knowledge with the definitions for those terms." - - This bot reacts to commands which insert, list or delete items from a - shared glossary when summoned in a group chat. This bot makes use of - persistent storage so the glossary is always there even if the bot goes - away. - """ - - help = """ - I help build a shared glossary - glossbot: @add - - glossbot: @rm - glossbot: @rand - glossbot: @ls - """ - - def group(self, message): - """Handle glossary commands.""" - if "@add" in message.content: - try: - parsed = self.parse_add(message) - self.add(*parsed, room=message.room) - except Exception: - response = f"Couldn't understand '{message.content}'?" - self.reply(response, room=message.sender) - elif "@rm" in message.content: - try: - parsed = message.content.split("@rm")[-1].strip() - self.rm(parsed, room=message.room) - except Exception: - response = f"Couldn't understand '{message.content}'?" - self.reply(response, room=message.sender) - elif "@rand" in message.content: - self.rand(room=message.room) - elif "@ls" in message.content: - self.ls(room=message.room) - else: - self.log.info(f"{message.text} not recognised as glossbot command") - - def parse_add(self, message): - """Parse the add command syntax.""" - try: - replaced = message.content.replace("@add", "") - return [s.strip() for s in replaced.split("-")] - except ValueError: - self.log.error(f"Failed to parse {message.content}") - - def add(self, entry, definition, **kwargs): - """Add a new entry.""" - self.db[entry] = definition - self.reply("Added ✌️", **kwargs) - - def rand(self, **kwargs): - """List a random entry.""" - if not self.db.keys(): - return self.reply("Glossary is empty 🙃️", **kwargs) - - entry = choice(self.db.keys()) - self.reply(f"{entry} - {self.db[entry]}", **kwargs) - - def ls(self, **kwargs): - """List all entries.""" - if not self.db.keys(): - return self.reply("Glossary is empty 🙃️", **kwargs) - - for entry in sorted(self.db.keys()): - self.reply(f"{entry} - {self.db[entry]}", **kwargs) - - def rm(self, entry, **kwargs): - """Remove an entry.""" - if entry not in self.db.keys(): - return self.reply(f"{entry} doesn't exist?", **kwargs) - - self.db.delete(entry) - self.reply("Removed ✌️", **kwargs)