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.

Install

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

from xbotlib import EchoBot

EchotBot()

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 a echobot.conf file in the same working directory for further use. See the configuration section for more.

Here's the code for the EchoBot.

class EchoBot(Bot):
    def direct(self, message):
        self.reply(message.body, to=message.sender)

    def group(self, message):
        if "echobot" in message.body:
            self.reply(message.body.split(":")[-1], room=message.room)

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

All examples

  • EchoBot: Sends back what you sent it
  • WhisperBot: Anonymous whispering in group chats

See xbotlib.py for all example bots.

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.

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)

SimpleMessage

A simple message interface.

Attributes:

  • body: the body of the message
  • sender: the user the message came from
  • room: the room the message came from
  • receiver: the receiver of the message
  • nickname: the nickname of the sender
  • type: the type of message (chat or groupchat)

Documenting your bot

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

  • !uptime: how long the bot has been running
  • !help: the end-user provided help text of what the bot does

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.

Configure your bot

All the ways you can pass configuration details to your bot.

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.

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

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

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.9.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

xbotlib-0.9.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xbotlib-0.9.0.tar.gz
  • Upload date:
  • Size: 18.9 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.9.0.tar.gz
Algorithm Hash digest
SHA256 b09201a430e9a75a678c34df44af73dfd349a30b383c90aae16732c54e68021f
MD5 877022f31a213a22ce48711b4f6a707b
BLAKE2b-256 97a369c7178407c6a615b9032e483e7874109b87637fb84827b1ba58a4e17821

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xbotlib-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 18.3 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.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4ad07a1b235867abcfa9b638fa56127221ef0aa42c1b6336a7470985176ddbb
MD5 d4275f152a964ed914b0c5d631429e81
BLAKE2b-256 1223612d559a3eb2d0b989244719834969f82ff89b1d05ba8339c5d1c9f93e38

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