PubSubClient — Message bus based RPC

This module connects to a pub/sub message bus and provices RPC functionality. It broadcasts status messages and reacts to different messages. The client / server for the message bus is http://gitlab.davepedu.com/dave/pymsgbus. Under the hood, this is ZeroMQ.

Config

{
    "servers": ["127.0.0.1:7100"],
    "subscriptions": ["pyircbot_send"],
    "publish": "pyircbot_{}",
    "name": "default"
}
servers

Message bus server(s) to connect or fall back to. Currently, only use of the first is implemented.

subscriptions

List of bus channels to subscribe to

publish

Publish channel name template. Will be formatted with the sub-type of message, such as privmsg.

name

Multiple bots can connect to one bus. They can be given a name to target a single bot; all will respond to the default name.

Bus Messages

In the format of:

<channel name> <message body>

Bot connects to the bus:

pyircbot_sys default online

Bot disconnects from the bus:

pyircbot_sys default offline

Bot relaying a privmsg:

pyircbot_privmsg default [["#clonebot"], "dave-irccloud", "message text",
                          {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}]

User parts a channel:

pyircbot_part default [["#clonebot"], "dave-irccloud", "part msg",
                       {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}]

User joins a channel:

pyircbot_join default ["dave-irccloud", "#clonebot",
                       {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}]

User uses the command .seen testbot:

pyircbot_command_seen default [["#clonebot"], "dave-irccloud", "testbot",
                               {"prefix": ["dave-irccloud", "sid36094", "Clk-247B1F43.irccloud.com"]}]

Client sending a message that the bot will relay

pyircbot_send default privmsg ["#clonebot", "asdf1234"]

Example Programs

Sends the content of a text file line-by-line with a delay:

from contextlib import closing
from msgbus.client import MsgbusSubClient
import argparse
from time import sleep
from json import dumps


def main():
    parser = argparse.ArgumentParser(description="send irc art")
    parser.add_argument("-i", "--host", default="127.0.0.1", help="host to connect to")
    parser.add_argument("-p", "--port", default=7003, help="port to connect to")
    parser.add_argument("-c", "--channel", required=True, help="irc channel")
    parser.add_argument("-f", "--file", required=True, help="file containing irc lines to send")
    parser.add_argument("--delay", type=float, default=1.0, help="delay between lines (s)")
    args = parser.parse_args()

    with open(args.file) as f:
        with closing(MsgbusSubClient(args.host, args.port)) as client:
            for line in f:
                line = line.rstrip()
                print(line)
                client.pub("pyircbot_send", "default privmsg {}".format(dumps([args.channel, line])))
                sleep(args.delay)


if __name__ == '__main__':
    main()

Class Reference

class pyircbot.modules.PubSubClient.PubSubClient(bot, moduleName)[source]

Bases: pyircbot.modulebase.ModuleBase

bus_command(msg, cmd)[source]

Parse commands and publish as separate channels on the bus. Commands like .seen nick will be published to channel command_seen.

bus_listener()[source]

Listen to the bus for send messages and act on recv

busdriver(msg, cmd)[source]

Relay a privmsg to the event bus

ondisable()[source]

Disconnect to the message bus on shutdown

onenable()[source]

Connect to the message bus when the module is enabled

publish(subchannel, message)[source]

Abstracted callback for proxying irc messages to the bs :param subchannel: event type such as “privmsg” :type subchannel: str :param message: message body :type message: str