python-xmpp-bot

A simple python-based XMPP bot.
git clone https://git.sr.ht/~jbauer/python-xmpp-bot
Log | Files | Refs | README | LICENSE

commit 6b0314dadf5884dadad7959d7c56a60ba661a47b
parent d12cfa1942a597d2969f471a480e0715297dc667
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Fri, 27 Nov 2020 23:46:04 -0500

Implement basic commands, MUC joining

Diffstat:
Mbot.py | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 64 insertions(+), 23 deletions(-)

diff --git a/bot.py b/bot.py @@ -7,60 +7,101 @@ import signal jabberid = "bot@xmpp.paritybit.ca" password = "secret" -receiver = "jbauer@xmpp.paritybit.ca" +receivers = ["jbauer@xmpp.paritybit.ca", "general@conference.xmpp.paritybit.ca"] def signal_handler(sig, frame): - connection.disconnect() - sys.exit(0) + try: + sys.stderr.write("\nSIGINT caught, exiting...\n") + client.disconnect() + sys.exit(0) + except: + sys.exit(0) signal.signal(signal.SIGINT, signal_handler) class Bot: - def __init__(self,jabber,remotejid): + def __init__(self, jabber): self.jabber = jabber - self.remotejid = remotejid def register_handlers(self): self.jabber.RegisterHandler('message',self.xmpp_message) + # self.jabber.RegisterDefaultHandler(self.default_handler) + + def default_handler(self, con, event): + print(event) + return def xmpp_message(self, con, event): - print("Message received") - type = event.getType() - fromjid = event.getFrom().getStripped() - if type in ['message', 'chat', None] and fromjid == self.remotejid: - sys.stdout.write(event.getBody() + '\n') - - def stdio_message(self, message): - m = xmpp.protocol.Message(to=self.remotejid,body=message,typ='chat') + eventType = event.getType() + messageDelayed = event.getTags("delay") or "" + messageBody = event.getBody() or "" + messageFrom = event.getFrom().getStripped() or "" + sys.stdout.write("==========\n") + sys.stdout.write(str(event) + '\n\n') + sys.stdout.write("DELAYED: " + str(messageDelayed == "") + '\n') + sys.stdout.write("TYPE: " + str(eventType) + '\n') + sys.stdout.write("MESG: " + str(messageBody) + '\n') + sys.stdout.write("FROM: " + str(messageFrom) + '\n') + sys.stdout.write("==========\n") + if eventType in ["chat", "groupchat"] and not messageDelayed: + if messageBody == ".tbhelp": + sys.stdout.write("CAUGHT COMMAND: .tbhelp\n") + self.command_help(messageFrom, eventType) + if messageBody == ".tbversion": + sys.stdout.write("CAUGHT COMMAND: .tbversion\n") + self.command_version(messageFrom, eventType) + elif messageBody.find("invites you to the room") != -1: + sys.stdout.write("Invite to room received. Joining: " + messageFrom + "\n") + self.join_room(messageFrom + "/testbot") + return + + def join_room(self, room): + self.jabber.sendPresence(jid=room, typ=None) + return + + def command_help(self, recipient, typ): + message = "Available commands: .tbhelp, .tbversion" + self.send_message(recipient, message, typ) + return + + def command_version(self, recipient, typ): + message = "XMPP Testbot version 0.1.0 by Jake Bauer" + self.send_message(recipient, message, typ) + return + + def send_message(self, recipient, message, typ): + m = xmpp.protocol.Message(to=recipient,body=message,typ=typ) self.jabber.send(m) - pass + return def xmpp_connect(self): - con=self.jabber.connect() + con = self.jabber.connect() if not con: - sys.stderr.write('could not connect!\n') + sys.stderr.write('Could not connect!\n') return False - sys.stderr.write('connected with %s\n'%con) + sys.stderr.write('Connected with %s\n'%con) auth=self.jabber.auth(jid.getNode(),password,resource=jid.getResource()) if not auth: - sys.stderr.write('could not authenticate!\n') + sys.stderr.write('Could not authenticate!\n') return False - sys.stderr.write('authenticated using %s\n'%auth) + sys.stderr.write('Authenticated using %s\n'%auth) self.register_handlers() return con jid = xmpp.protocol.JID(jabberid) -connection = xmpp.Client(jid.getDomain(), debug=[]) +client = xmpp.Client(jid.getDomain(), debug = []) -bot = Bot(connection, receiver) +bot = Bot(client) if not bot.xmpp_connect(): sys.stderr.write("Could not connect to server.\n") sys.exit(1) -connection.sendInitPresence() +client.sendInitPresence() online = 1 while online: - connection.Process(1) + if not client.isConnected(): + client.reconnectAndReauth() + client.Process(1)