xchatbot module

xchatbot - the Xtensible xmpp Chat Bot

Build an XMPP bot extending the base class XChatBot

Example

A simple bot with one public command “echo” and one private to admin command “hello” “help” command is autogenerated.

from xchatbot import XChatBot

class MyBot(XChatBot):
    def cmd_echo(self, peer, *args):
        "Echo back what you typed"
        msg = "You said: " + " ".join(args)
        peer.send(msg)

    @private
    def cmd_hello(self, peer, name):
        "<name> - Private command for admin user"
        peer.send("Welcome " + name)

if __name__ == "__main__":
    MyBot.start()
class xchatbot.Peer(bot, jid, nick, is_groupchat=False)

Bases: object

The peer that sent the message

jid

the peer jid

Type:str
nick

the nickname

Type:str
is_admin

True if the peer is a bot admin

Type:bool
is_groupchat

True if the peer wrote to the bot is in a MUC

Type:bool
send(message)

Send a message to the peer

Note

If peer is in a groupchat, the nickname is prepended to the message:

{nick}: {message}
Parameters:message (str) – The message
class xchatbot.XChatBot(jidparams)

Bases: object

The Xtensible xmpp Chat Bot

options

options loaded from config file

Type:dict
jid

bot JID

Type:nbxmpp.protocol.JID
muc_nick

bot nick in MUC rooms

Type:str
admin_jid

admin user JID

Type:str
accept_presence

True if bot accept to be included in rooster by any users

Type:bool
accept_muc_invite

True if bot accept invites to MUC rooms by any users

Type:bool
logger

configured logger to be used by the bot subclass

Type:logging.Logger
connect()

Connect to the server

default(peer, *args)

Default command

This is called when the peer sends a command not defined in the bot.

Parameters:
  • peer (Peer) – The remote peer
  • *args – arguments of the command
disconnect()

Disconnect from the server

do_help(peer)

Send help message to the peer

Parameters:peer (Peer) – The remote peer
enter_muc(muc_jid)

Join a multiuser conference

Parameters:muc_jid (str) – MUC JID
get_password(cb, _mech)

Get password

By default, calls cb() with password from config file.

Parameters:cb (func(str)) – callback to call with the password
quit()

Disconnecty the bot and quit

Note

Deprecated. Use disconnect()

register_on_disconnect(handler)

Register handler that will be called on disconnect

send_groupchat_to(to_jid, text)

Send a message to a MUC by JID

Parameters:
  • to_jid (str) – MUC JID
  • text (str) – Message text
send_message(message)

Send a message

Parameters:message (nbxmpp.protocol.Message) – the message to send
send_message_to(to_jid, text)

Send a chat message to a JID

Parameters:
  • to_jid (str) – Recipient JID
  • text (str) – Message text
send_received(to_jid, message_id)

Send a message delivery receipt

Will mark the message as received by the bot in user’s client. It’s sent automatically when incoming messages are parsed.

Parameters:
  • to_jid (str) – Recipient JID
  • message_id (str) – Message id
classmethod start()

Start bot

Loads config file, connects the bot to the server, setups error and quit handlers, start GLib.MainLoop loop.

xchatbot.store(name, data)

store data as pickle value to file

Parameters:
  • name (str) – Filename, without extension
  • data (str) – Python data to store
xchatbot.load(name, default)

Load pickled data from file.

Parameters:
  • name (str) – Filename to load, without extension
  • default (any) – Python data returned if file is not found
Returns:

Loaded data

If file is not found, the value of default parameter is returned

Return type:

str

xchatbot.private(func)

Decorator to mark command private for admin user