Skip to main content

XMPP bots for humans

Project description

xbotlib

PyPI version Build Status

XMPP bots for humans

status: experimental

A friendly lightweight wrapper around slixmpp for writing XMPP bots in Python. The goal is to make writing and running XMPP bots easy and fun. xbotlib is a single file implementation which can easily be understood and extended. It provides a small API surface which reflects the slixmpp way of doing things. The xbotlib source code and ideas are largely borrowed/stolen/adapted/reimagined from the XMPP bot experiments that have gone on and are still going on in Varia.

We're lurking in xbotlibtest@muc.vvvvvvaria.org if you want to chat or just invite your bots for testing.

Install

$ pip install xbotlib

Example

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.

from xbotlib import Bot

class EchoBot(Bot):

    def direct(self, message):
        return self.reply(message.text, to=message.sender)

    def group(self, message):
        return self.reply(message.content, room=message.room)

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 section for more.

Read more in the API reference for how to write your own bots.

See more examples on git.vvvvvvaria.org.

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 function. If you want to respond to a group chat message, you write a group function. That's it for the basics.

Bot.direct(message)

Respond to direct messages.

Arguments:

  • message: received message (see SimpleMessage below for available attributes)

Bot.group(message)

Respond to a message in a group chat.

Arguments:

  • message: received message (see SimpleMessage below for available attributes)

Bot.serve(request)

Serve requests via the built-in web server.

Arguments:

  • request: the web request

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:

Working with your bot

Documentation

Add a help = "my help" to your Bot class like so.

class MyBot(Bot):
    help = "My help"

See more in the commands section on how to use this.

Commands

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, here are the supported commands.

  • @uptime: how long the bot has been running
  • @help: the help text for what the bot does

There are also more general status commands which all bots respond to.

  • @bots: status check on who is a bot in the group chat

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.

Avatars

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.

Configuration

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 following order: command-line > configuration file > environment.

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.

Here is an example of a working configuration.

[echobot]
account = echobot@vvvvvvaria.org
password = ...thepassword...
nick = echobot
rooms = test1@muc.example.com, test2@muc.example.com

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).

usage: bot.py [-h] [-d] [-a ACCOUNT] [-p PASSWORD] [-n NICK]
              [-av AVATAR] [-ru REDIS_URL] [-r ROOMS [ROOMS ...]]
              [--no-auto-join]

XMPP bots for humans

optional arguments:
  -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
  -av AVATAR, --avatar AVATAR
                        Avatar for the bot account
  -ru REDIS_URL, --redis-url REDIS_URL
                        Redis storage connection URL
  -r ROOMS [ROOMS ...], --rooms ROOMS [ROOMS ...]
                        Rooms to automatically join
  --no-auto-join        Disable automatically joining rooms when invited
  -pt PORT, --port PORT
                        The port to serve from
  -t TEMPLATE, --template TEMPLATE
                        The template to render

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
  • XBOT_AVATAR: The bot avatar icon
  • XBOT_REDIS_URL: Redis key store connection URL
  • XBOT_ROOMS: The rooms to automatically join
  • XBOT_NO_AUTO_JOIN: Disable auto-joining on invite
  • XBOT_PORT: The port to serve from

Persistent storage

File system

Just use your local file system as you would in any other Python script. Please note that when you deploy your bot, you might not have access to this local filesystem in the same location. For remote server deployments Redis can be more convenient.

Redis key/value storage

xbotlib supports using Redis as a storage back-end. It is simple to work with because the interface is exactly like a dictionary. You can quickly run Redis locally using Docker (docker run --network=host --name redis -d redis) or if you're on a Debian system you can also sudo apt install -y redis.

You can configure the connection URL using the command-line interface, configuration or environment. Here is an example using the environment.

$ export XBOT_REDIS_URL=redis://localhost:6379/0

And you access the interface via the self.db attribute.

def direct(self, message):
    self.db["mykey"] = message.text

You should see INFO Successfully connected to storage when your bot initialises. Please see the redis-py API documentation for more.

Loading Plugins

You can specify a plugins = [...] on your bot definition and they will be automatically loaded.

class MyBot(Bot):
    plugins = ["xep_0066"]

Serving HTTP

Your bot will automatically be running a web server at port 8080 when it is run. If you're running your bot locally, just visit 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 function.

xbotlib provides a small wrapper API for Jinja2 which allows you to easily template and generate HTML. The web server is provided by aiohttp.

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.

<h1>{{ letter }}</h1>
from string import ascii_letters

def serve(self, request):
    letter = choice(ascii_letters)
    rendered = self.template.render(letter=letter)
    return self.respond(body=rendered)

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 storage. Your serve function can read from the database or file system and then respond with generated HTML from there.

Having your bot avaible on the web is useful for doing healthchecks with something like statping so you be sure that your bot is up and running.

Deploy your bots

See bots.varia.zone.

Roadmap

See the issue tracker.

Changes

See the CHANGELOG.md.

License

See the LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xbotlib-0.14.1.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

xbotlib-0.14.1-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file xbotlib-0.14.1.tar.gz.

File metadata

  • Download URL: xbotlib-0.14.1.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/4.19.0-13-amd64

File hashes

Hashes for xbotlib-0.14.1.tar.gz
Algorithm Hash digest
SHA256 32c9aeff9bddeed22cd656214bdc0dd9d2c6882d16151e321f5f73a0a081372c
MD5 567bf469e370671e07e2a6fcef9b1d87
BLAKE2b-256 7fe6f3050150c5a377679be331d4f5b547d123e77b2b92d0ebca3129244e0767

See more details on using hashes here.

File details

Details for the file xbotlib-0.14.1-py3-none-any.whl.

File metadata

  • Download URL: xbotlib-0.14.1-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/4.19.0-13-amd64

File hashes

Hashes for xbotlib-0.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aed3734c449bfa618faddd16cb9e12ea159293a5c777f0a2706241c5f4472da0
MD5 5caec32d46221ce5ec109667381d4610
BLAKE2b-256 a8d0a1b6d92b0721dbecd49606da20ce23fd6644787f6120984588a313a678d3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page