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:
objectThe peer that sent the message
-
jid¶ the peer jid
Type: str
-
nick¶ the nickname
Type: str
-
is_admin¶ Trueif the peer is a bot adminType: bool
-
is_groupchat¶ Trueif the peer wrote to the bot is in a MUCType: 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:
objectThe 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¶ Trueif bot accept to be included in rooster by any usersType: bool
-
accept_muc_invite¶ Trueif bot accept invites to MUC rooms by any usersType: 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
- peer (
-
disconnect()¶ Disconnect from the server
-
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
- to_jid (
-
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
- to_jid (
-
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
- to_jid (
-
classmethod
start()¶ Start bot
Loads config file, connects the bot to the server, setups error and quit handlers, start
GLib.MainLooploop.
-
-
xchatbot.store(name, data)¶ store data as pickle value to file
Parameters: - name (
str) – Filename, without extension - data (
str) – Python data to store
- name (
-
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
defaultparameter is returnedReturn type: str- name (
-
xchatbot.private(func)¶ Decorator to mark command private for admin user