Remove example bots
Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/29.
This commit is contained in:
118
xbotlib.py
118
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 <entry> - <definition>
|
||||
glossbot: @rm <entry>
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user