This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
xbotlib/README.md

439 lines
14 KiB
Markdown
Raw Normal View History

2021-01-10 13:10:39 +00:00
# xbotlib
2021-01-10 18:23:21 +00:00
[![PyPI version](https://badge.fury.io/py/xbotlib.svg)](https://badge.fury.io/py/xbotlib)
2021-01-10 18:24:20 +00:00
[![Build Status](https://drone.autonomic.zone/api/badges/decentral1se/xbotlib/status.svg?ref=refs/heads/main)](https://drone.autonomic.zone/decentral1se/xbotlib)
2021-01-10 18:23:21 +00:00
2021-01-10 13:10:39 +00:00
## XMPP bots for humans
2021-01-10 18:17:10 +00:00
> status: experimental
2021-01-10 15:54:55 +00:00
2021-01-10 18:17:10 +00:00
A friendly lightweight wrapper around
[slixmpp](https://slixmpp.readthedocs.io/) for writing XMPP bots in Python. The
2021-01-10 18:35:39 +00:00
goal is to make writing and running XMPP bots easy and fun. `xbotlib` is a
[single file implementation](./xbotlib.py) which can easily be understood and
2021-01-24 00:13:43 +00:00
extended. The `xbotlib` source code and ideas are largely borrowed from the
XMPP bot experiments going on in
2021-01-15 17:22:24 +00:00
[Varia](https://git.vvvvvvaria.org/explore/repos?tab=&sort=recentupdate&q=bots).
2021-01-15 17:21:41 +00:00
2021-01-16 21:26:22 +00:00
- [Install](#install)
2021-01-24 12:45:16 +00:00
- [Getting Started](#getting-started)
2021-01-16 21:30:25 +00:00
- [Working with your bot](#working-with-your-bot)
2021-01-16 21:32:02 +00:00
- [Documentation](#documentation)
- [Commands](#commands)
- [Avatars](#avatars)
- [Configuration](#configuration)
2021-01-24 12:49:03 +00:00
- [Using the `.conf` configuration file](#using-the-conf-configuration-file)
2021-01-16 21:32:02 +00:00
- [Using the command-line interface](#using-the-command-line-interface)
- [Using the environment](#using-the-environment)
- [Storage back-end](#storage-back-end)
2021-01-16 21:32:02 +00:00
- [Loading Plugins](#loading-plugins)
- [Serving HTTP](#serving-http)
- [Invitations](#invitations)
2021-01-24 12:45:16 +00:00
- [API Reference](#api-reference)
2021-01-24 12:49:03 +00:00
- [Bot.direct(message)](#bot-direct-message)
- [Bot.group(message)](#bot-group-message)
- [Bot.serve(request)](#bot-serve-request)
- [Bot.routes()](#bot-routes)
- [Bot.setup()](#bot-setup)
2021-01-24 12:45:16 +00:00
- [SimpleMessage](#simplemessage)
- [Bot](#bot)
- [Chatroom](#chatroom)
- [More Examples](#more-examples)
2021-01-16 21:26:22 +00:00
- [Deploy your bots](#deploy-your-bots)
- [Roadmap](#roadmap)
- [Changes](#changes)
2021-01-16 21:30:25 +00:00
- [License](#license)
2021-01-26 22:02:25 +00:00
- [Contributing](#contributing)
2021-01-16 21:26:22 +00:00
2021-01-10 13:10:39 +00:00
## Install
```sh
$ pip install xbotlib
```
2021-01-24 12:45:16 +00:00
## Getting Started
2021-01-10 13:10:39 +00:00
2021-01-24 00:13:43 +00:00
Put the following in a `echo.py` file. This bot echoes back whatever message
you send it in both direct messages and group messages. In group chats, you
2021-01-24 12:45:16 +00:00
need to message the bot directly (e.g. `echobot: hi`) (see [the commands
section](#commands) for more).
2021-01-13 13:08:44 +00:00
```python
2021-01-16 21:26:22 +00:00
from xbotlib import Bot
2021-01-13 13:08:44 +00:00
class EchoBot(Bot):
def direct(self, message):
self.reply(message.text, to=message.sender)
2021-01-16 21:26:22 +00:00
def group(self, message):
self.reply(message.content, room=message.room)
2021-01-24 00:13:43 +00:00
EchoBot()
2021-01-13 13:08:44 +00:00
```
And then `python echo.py`. You will be asked a few questions in order to load
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.
2021-01-24 12:45:16 +00:00
That's it! If you want to go further, continue reading [here](#working-with-your-bot).
2021-01-16 21:30:25 +00:00
## Working with your bot
2021-01-24 12:45:16 +00:00
The following sections try to cover all the ways you can configure and extend
your bot using `xbotlib`. If anything is unclear, please let us know [on the
chat](#chatroom).
2021-01-16 21:30:25 +00:00
### Documentation
Add a `help = "my help"` to your `Bot` class like so.
```python
class MyBot(Bot):
2021-01-24 12:45:16 +00:00
help = """This is my cool help.
I can list some commands too:
@foo - some command
"""
```
2021-01-24 12:45:16 +00:00
Your bot will then respond to `mybot: @help` invocations. This string can be a
multi-line string with indentation. `xbotlib` will try to format this sensibly
for showing in chat clients.
See more in the [commands](#commands) section for more.
2021-01-16 21:30:25 +00:00
### Commands
2021-01-15 10:27:50 +00:00
2021-01-16 21:26:22 +00:00
Using `@<command>` in direct messages and `<nick>, @<command>` (the `,` is
optional, anything will be accepted here and there doesn't seem to be a
consensus on what is most common way to "at" another user in XMPP) in group chats,
2021-01-15 10:27:50 +00:00
here are the supported commands.
2021-01-16 21:27:25 +00:00
- `@uptime`: how long the bot has been running
- `@help`: the help text for what the bot does
2021-01-15 17:24:07 +00:00
There are also more general status commands which all bots respond to.
2021-01-16 21:27:25 +00:00
- `@bots`: status check on who is a bot in the group chat
2021-01-15 17:24:07 +00:00
2021-01-18 19:48:01 +00:00
These commands will be detected in any part of the message sent to the bot. So
you can write `echobot, can we see your @uptime`, or `I'd love to know which @bots are here.`
2021-01-16 21:30:25 +00:00
### Avatars
2021-01-15 10:27:50 +00:00
By default, `xbotlib` will look for an `avatar.png` (so far tested with `.png`
but other file types may work) file alongside your Python script which contains
your bot implementation. You can also specify another path using the `--avatar`
option on the command-line interface. The images should ideally have a height
of `64` and a width of `64` pixels each.
2021-01-24 12:45:16 +00:00
### Configuration
2021-01-16 21:26:22 +00:00
All the ways you can pass configuration details to your bot. There are three
ways to configure your bot, the configuration file, command-line interface and
the environment. Use whichever one suits you best. The values are loaded in the
2021-01-23 20:40:51 +00:00
following order: command-line > configuration file > environment. This means
you can override everything from the command-line easily.
2021-01-16 21:30:25 +00:00
#### Using the `.conf` configuration file
If you run simply run your Python script which contains the bot then `xbotlib`
will generate a configuration for you by asking a few questions. This is the
simplest way to run your bot locally.
2021-01-23 22:28:32 +00:00
- **account**: the account of the bot
- **password**: the password of the bot account
- **nick**: the nickname of the bot
- **avatar**: the avatar of the bot (default: `avatar.png`)
- **redis_url**: the Redis connection URL
2021-01-24 17:39:39 +00:00
- **rooms**: a list of rooms to automatically join (comma separated)
2021-01-23 22:28:32 +00:00
- **no_auto_join**: disable auto-join when invited (default: `False`)
- **template**: the port to serve from (default: `index.html.j2`)
- **serve**: turn on the web server (default: `False`)
- **port**: the port to serve from (default: `8080`)
- **storage**: storage back-end (default: `file`)
- **output**: path to the output directory (default: `./`)
2021-01-16 21:08:59 +00:00
2021-01-16 21:30:25 +00:00
#### Using the command-line interface
Every bot accepts a number of comand-line arguments to load configuration. You
can use the `--help` option to see what is available (e.g. `python bot.py --help`).
2021-01-23 20:40:51 +00:00
- **-h, --help**: show this help message and exit
- **-d, --debug**: enable verbose debug logs
- **-a ACCOUNT, --account ACCOUNT**: account for the bot account
- **-p PASSWORD, --password PASSWORD**: password for the bot account
- **-n NICK, --nick NICK**: nickname for the bot account
2021-01-23 22:28:32 +00:00
- **-av AVATAR, --avatar AVATAR**: avatar for the bot account (default: `avatar.png`)
2021-01-23 20:40:51 +00:00
- **-ru REDIS_URL, --redis-url REDIS_URL**: redis storage connection URL
- **-r ROOMS [ROOMS ...], --rooms ROOMS [ROOMS ...]**: Rooms to automatically join
2021-01-23 22:28:32 +00:00
- **-naj, --no-auto-join**: disable automatically joining rooms when invited (default: `False`)
- **-pt PORT, --port PORT**: the port to serve from (default: `8080`)
- **-t TEMPLATE, --template TEMPLATE**: the template to render (default: `index.html.j2`)
- **-s, --serve**: turn on the web server (default: `False`)
- **-st {file,redis}, --storage {file,redis}**: choice of storage back-end (default: `file`)
- **-o OUTPUT, --output OUTPUT**: path to the output directory (default: `./`)
2021-01-16 21:26:22 +00:00
2021-01-16 21:30:25 +00:00
#### Using the environment
`xbotlib` will try to read the following configuration values from the
environment if it cannot read them from a configuration file or the
command-line interface. This can be useful when doing remote server
deployments.
- **XBOT_ACCOUNT**: The bot account
- **XBOT_PASSWORD**: The bot password
- **XBOT_NICK**: The bot nickname
2021-01-23 22:28:32 +00:00
- **XBOT_AVATAR**: The bot avatar icon (default: `avatar.png`)
- **XBOT_REDIS_URL**: Redis key store connection URL
- **XBOT_ROOMS**: The rooms to automatically join
2021-01-23 22:28:32 +00:00
- **XBOT_NO_AUTO_JOIN**: Disable auto-joining on invite (default: `False`)
- **XBOT_TEMPLATE**: the template to render (default: `index.html.j2`)
- **XBOT_SERVE**: Turn on the web server (default: `False`)
- **XBOT_PORT**: The port to serve from (default: `8080`)
- **XBOT_STORAGE**: choice of storage back-end (default: `file`)
- **XBOT_OUTPUT**: path to the output directory (default: `./`)
### Storage back-end
2021-01-15 20:13:17 +00:00
In order to store data you can make use of the `self.db` attribute of the `Bot`
class. It is a Python dictionary which will be saved to disk automatically for
you as a `<nick>.json` in your current working directory. The name and path to
this file can be configured using the output option (e.g. `python bot.py --output /var/www/html`)
```python
def group(self, message):
if not message.room in self.db.keys():
self.db[message.room] = "visited"
2021-01-15 20:13:17 +00:00
```
If you want to inspect the database when the bot is not running, you can look
in the file directly.
2021-01-15 20:13:17 +00:00
```bash
2021-01-24 00:13:43 +00:00
$ cat <nick>.json
2021-01-15 20:13:17 +00:00
```
For more advanced use cases, `xbotlib` also supports [Redis](https://redis.io/)
as a storage back-end. You'll need to configure this (e.g. `--storage redis`)
as the default uses the filesystem approach mentioned above. The same `self.db`
will then be passed as a Redis connection object. You will also need to install
additional dependencies using `pip install xbotlib[redis]`.
2021-01-15 20:13:17 +00:00
2021-01-16 21:30:25 +00:00
### Loading Plugins
2021-01-16 19:37:25 +00:00
You can specify a `plugins = [...]` on your bot definition and they will be
automatically loaded when you start your bot.
2021-01-16 19:37:25 +00:00
```python
class MyBot(Bot):
plugins = ["xep_0066"]
```
2021-01-23 22:29:16 +00:00
See [here](https://slixmpp.readthedocs.io/xeps.html) for the list of supported plugins.
### Serving HTTP
Firstly, you'll need to install additional dependencies.
```bash
$ pip install xbotlib[web]
```
2021-01-23 20:40:51 +00:00
Your bot will run a web server if you configure it to do so. Use the `--serve`
option on the command-line, the `serve = True` configuration option or the
`XBOT_SERVE=True` environment variable.
If you're running your bot locally, just visit
[0.0.0.0:8080](http://0.0.0.0:8080) to see. The default response is just some
placeholder text. You can write your own responses using the
[Bot.serve](#bot-serve-request) function.
`xbotlib` provides a small wrapper API for
[Jinja2](https://jinja.palletsprojects.com/en/2.11.x/) which allows you to
easily template and generate HTML. The web server is provided by
[aiohttp](https://docs.aiohttp.org/).
The default template search path is `index.html.j2` in the current working
directory. This can be configured through the usual configuration entrypoints.
Here's a small example that renders a random ASCII letter and uses a stylesheet.
2021-01-17 19:23:01 +00:00
2021-01-23 20:40:51 +00:00
> index.html.j2
2021-01-17 19:23:01 +00:00
```jinja
<html>
<head>
<style> h1 { color: red; } </style>
</head>
<body>
<h1>{{ letter }}</h1>
</body>
</html>
2021-01-17 19:23:01 +00:00
```
2021-01-23 20:40:51 +00:00
> bot.py
```python
2021-01-17 19:23:01 +00:00
from string import ascii_letters
2021-01-24 12:45:16 +00:00
async def serve(self, request):
2021-01-17 19:23:01 +00:00
letter = choice(ascii_letters)
rendered = self.template.render(letter=letter)
2021-01-23 20:40:51 +00:00
return self.respond(rendered)
```
Please note the use of the `return` keyword here. The `serve` function must
2021-01-24 12:45:16 +00:00
return a response that will be passed to the web server. Also, the `async`
keyword. This function can handle asynchronous operations and must be marked as
such. You can return any content type that you might find on the web (e.g.
HTML, XML, JSON, audio and maybe even video) but you must specify the
`content_type=...` keyword argument for `respond`.
See the [list of available content
types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#types)
for more.
If you want to pass data from your `direct`/`group` functions to the `serve`
function, you'll need to make use of [some type of persistent
2021-01-24 00:13:43 +00:00
storage](#storage-back-end). Your `serve` function can read from the storage
2021-01-24 12:45:16 +00:00
back-end and then respond. This is usually as simple as accessing the `self.db`
attribute.
### Invitations
As long as the `--no-auto-join` option is not set (via the configuration file
or environment also), then your bot will automatically join any room it is
invited to. Rooms that your bot has been invited to will be stored in the
`.xbotlib/data.json` file. If your bot is turned off or fails for some reason
then it will read this file when turned back on to see what rooms it should
re-join automatically. The `data.json` file can be edited freely by hand.
2021-01-24 12:45:16 +00:00
### API Reference
When writing your own bot, you always sub-class the `Bot` class provided from
`xbotlib`. Then if you want to respond to a direct message, you write a
[direct](#botdirectmessage) function. If you want to respond to a group chat
message, you write a [group](#botgroupmessage) function. That's it for the
basics.
#### Bot.direct(message)
Respond to direct messages.
Arguments:
- **message**: received message (see [SimpleMessage](#simplemessage) below for available attributes)
#### Bot.group(message)
Respond to a message in a group chat.
Arguments:
- **message**: received message (see [SimpleMessage](#simplemessage) below for available attributes)
#### Bot.serve(request)
Serve requests via the built-in web server.
See [this section](#serving-http) for more.
Arguments:
- **request**: the web request
2021-01-24 12:47:16 +00:00
#### Bot.routes()
2021-01-24 12:45:16 +00:00
Register additional routes for web serving.
See [this section](#serving-http) for more.
This is an advanced feature. Use `self.web.add_routes`.
```python
from aiohttp.web import get
def routes(self):
self.web.add_routes([get("/foo", self.foo)])
```
#### Bot.setup()
Run some setup logic before starting your bot.
#### SimpleMessage
A simple message interface.
Attributes:
- **text**: the entire text of the message
- **content**: the text of the message after the nick
- **sender**: the user the message came from
- **room**: the room the message came from
- **receiver**: the receiver of the message
- **nick**: the nickname of the sender
- **type**: the type of message
- **url**: The URL of a sent file
#### Bot
> Bot.reply(message, to=None, room=None)
Send a reply back.
Arguments:
- **message**: the message that is sent
- **to**: the user to send the reply to
- **room**: the room to send the reply to
> Bot.respond(response, content_type="text/html")
Return a response via the web server.
Arguments:
- **response**: the text of the response
- **content_type**: the type of response
Other useful attributes on the `Bot` class are:
2021-01-24 17:42:53 +00:00
- **self.db**: The [storage back-end](#storage-back-end)
2021-01-24 12:45:16 +00:00
## Chatroom
We're lurking in
[xbotlibtest@muc.vvvvvvaria.org](xmpp:xbotlibtest@muc.vvvvvvaria.org?join) if
you want to chat or just invite your bots for testing.
## More Examples
See more examples on [git.vvvvvvaria.org](https://git.vvvvvvaria.org/explore/repos?q=xbotlib&topic=1).
2021-01-16 21:08:59 +00:00
## Deploy your bots
See [bots.varia.zone](https://bots.varia.zone/).
2021-01-10 15:51:20 +00:00
## Roadmap
2021-01-10 18:38:19 +00:00
See the [issue tracker](https://git.autonomic.zone/decentral1se/xbotlib/issues).
2021-01-10 15:51:20 +00:00
## Changes
See the [CHANGELOG.md](./CHANGELOG.md).
## License
See the [LICENSE](./LICENSE.md).
2021-01-26 22:02:25 +00:00
## Contributing
Any and all contributions most welcome! Happy to hear how you use the library
2021-01-26 22:02:58 +00:00
or what could be improved from a usability perspective.
To test, install [Tox](https://tox.readthedocs.io/en/latest/) (`pip install tox`) and run `tox` to run the test suite locally.