Skip to main content

IRC bot - full featured, yet extensible and customizable

Project description

https://img.shields.io/pypi/v/pmxbot.svg https://img.shields.io/pypi/pyversions/pmxbot.svg https://dev.azure.com/pmxbot/pmxbot/_apis/build/status/pmxbot.pmxbot?branchName=master https://img.shields.io/travis/pmxbot/pmxbot/master.svg Code style: Black https://readthedocs.org/projects/pmxbot/badge/?version=latest

pmxbot is bot for IRC and Slack written in Python. Originally built for internal use at YouGov, it’s been sanitized and set free upon the world. You can find out more details on the project website.

Commands

pmxbot listens to commands prefixed by a ‘!’ If it’s a command, it knows it will reply, take an action, etc. It can search the web, store quotes you, track karma, make decisions, and do just about anything else you could want. It stores logs and quotes and karma in either a sqlite or MongoDB database, and there’s a web interface for reviewing the logs and karma.

Contains

pmxbot will respond to things you say if it detects words and phrases it’s been told to recognize. For example, mention sql on rails.

Requirements

pmxbot requires Python 3. It also requires a few python packages as defined in setup.py. Some optional dependencies are installed with extras:

  • mongodb: Enable MongoDB persistence (instead of sqlite).

  • irc: IRC bot client.

  • slack: Slack bot client.

  • viewer: Enable the web viewer application.

Testing

pmxbot includes a test suite that does some functional tests written against the Python IRC server and quite a few unit tests as well. Install tox and run tox to invoke the tests.

Configuration

Configuration is based on very easy YAML files. Check out config.yaml in the source tree for an example.

Usage

Once you’ve setup a config file, you just need to call pmxbot config.yaml and it will join and connect. We recommend running pmxbot under your favorite process supervisor to make it automatically restart if it crashes (or terminates due to a planned restart).

Custom Features

Setuptools Entry Points Plugin

pmxbot provides an extension mechanism for adding commands, and uses this mechanism even for its own built-in commands.

To create a setuptools entry point plugin, package your modules using the setuptools tradition and install it alongside pmxbot. Your package should define an entry point in the group pmxbot_handlers by including something similar to the following in the package’s setup.py:

entry_points = {
    'pmxbot_handlers': [
        'plugin name = pmxbot.mymodule',
    ],
},

During startup, pmxbot will load pmxbot.mymodule. plugin name can be anything, but should be a name suitable to identify the plugin (and it will be displayed during pmxbot startup).

Note that the pmxbot package is a namespace package, and you’re welcome to use that namespace for your plugin (e.g. pmxbot.nsfw).

If your plugin requires any initialization, specify an initialization function (or class method) in the entry point. For example:

'plugin name = pmxbot.mymodule:initialize_func'

On startup, pmxbot will call initialize_func with no parameters.

Within the script you’ll want to import the decorator(s) you need to use with:

from pmxbot.core import command, contains, regexp, execdelay, execat`.

You’ll then decorate each function with the appropriate line so pmxbot registers it.

A command (!g) gets the @command decorator:

@command(aliases=('tt', 'tear', 'cry'))
def tinytear(rest):
  "I cry a tiny tear for you."
  if rest:
    return "/me sheds a single tear for %s" % rest
  else:
    return "/me sits and cries as a single tear slowly trickles down its cheek"

A response (when someone says something) uses the @contains decorator:

@contains("sqlonrails")
def yay_sor():
  karma.Karma.store.change('sql on rails', 1)
  return "Only 76,417 lines..."

Each handler may solicit any of the following parameters:

  • channel (the channel in which the message occurred)

  • nick (the nickname that triggered the command or behavior)

  • rest (any text after the command)

A more complicated response (when you want to extract data from a message) uses the @regexp decorator:

@regexp("jira", r"(?<![a-zA-Z0-9/])(OPS|LIB|SALES|UX|GENERAL|SUPPORT)-\d\d+")
def jira(client, event, channel, nick, match):
    return "https://jira.example.com/browse/%s" % match.group()

For an example of how to implement a setuptools-based plugin, see one of the many examples in the pmxbot project itself or one of the popular third-party projects:

Web Interface

pmxbot includes a web server for allowing users to view the logs, read the help, and check karma. You specify the host, port, base path, logo, title, etc with the same YAML config file. Just run like pmxbotweb config.yaml and it will start up. Like pmxbot, use of a supervisor is recommended to restart the process following termination.

pmxbot as a Slack bot (native)

To use pmxbot as a Slack bot, install with pmxbot[slack], and set slack token in your config to the token from your Bot User. Easy, peasy.

pmxbot as a Slack bot (IRC)

As Slack provides an IRC interface, it’s easy to configure pmxbot for use in Slack. Here’s how:

  1. Install with pmxbot[irc].

  2. Enable the IRC Gateway <https://slack.zendesk.com/hc/en-us/articles/201727913-Connecting-to-Slack-over-IRC-and-XMPP>.

  3. Create an e-mail for the bot.

  4. Create the account for the bot in Slack and activate its account.

  5. Log into Slack using that new account and get the IRC gateway password <https://my.slack.com/account/gateways> for that account.

  6. Configure the pmxbot as you would for an IRC server, but use these settings for the connection:

    message rate limit: 2.5 password: <gateway password> server_host: <team name>.irc.slack.com server_port: 6667

    The rate limit is necessary because Slack will kick the bot if it issues more than 25 messages in 10 seconds, so throttling it to 2.5 messages per second avoids hitting the limit.

  7. Consider leaving ‘log_channels’ and ‘other_channels’ empty, especially if relying on Slack logging. Slack will automatically re-join pmxbot to any channels to which it has been /invited.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

pmxbot-1122.9.3.tar.gz (206.7 kB view details)

Uploaded Source

Built Distribution

pmxbot-1122.9.3-py36-none-any.whl (171.6 kB view details)

Uploaded Python 3.6

File details

Details for the file pmxbot-1122.9.3.tar.gz.

File metadata

  • Download URL: pmxbot-1122.9.3.tar.gz
  • Upload date:
  • Size: 206.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pmxbot-1122.9.3.tar.gz
Algorithm Hash digest
SHA256 e3f5462787f9319605013b83414757b12ce2ebf2357838b9e2de472b04af649c
MD5 07e40a77ff1f945990ffe7b9b5e1433a
BLAKE2b-256 1be714e272769ecd82df63b0c9cf7a3bcf1fc3fd72cde627bded97e636fdde7c

See more details on using hashes here.

File details

Details for the file pmxbot-1122.9.3-py36-none-any.whl.

File metadata

  • Download URL: pmxbot-1122.9.3-py36-none-any.whl
  • Upload date:
  • Size: 171.6 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pmxbot-1122.9.3-py36-none-any.whl
Algorithm Hash digest
SHA256 f0018ad2a0253e22a407a5d86c455e49207b0d23bb99db90fdbea0c14e82a4bd
MD5 b83747d9f0495900d87bd48f3fb046a9
BLAKE2b-256 7d6bc3c861ed7e15e406906abf417ad286abecae7cf5076b0bc2271b87d5750a

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