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 15895c9af077da9c3a0f520da85a84f8ebd50454
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Thu, 26 Nov 2020 17:36:50 -0500

Basic XMPP bot based on xmpppy example code

Diffstat:
Abot.py | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+), 0 deletions(-)

diff --git a/bot.py b/bot.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import xmpp +import sys +import os +import signal + +jabberid = "bot@xmpp.paritybit.ca" +password = "secret" +receiver = "jbauer@xmpp.paritybit.ca" + +def signal_handler(sig, frame): + connection.disconnect() + sys.exit(0) + +signal.signal(signal.SIGINT, signal_handler) + +class Bot: + + def __init__(self,jabber,remotejid): + self.jabber = jabber + self.remotejid = remotejid + + def register_handlers(self): + self.jabber.RegisterHandler('message',self.xmpp_message) + + 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') + self.jabber.send(m) + pass + + def xmpp_connect(self): + con=self.jabber.connect() + if not con: + sys.stderr.write('could not connect!\n') + return False + 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') + return False + sys.stderr.write('authenticated using %s\n'%auth) + self.register_handlers() + return con + +jid = xmpp.protocol.JID(jabberid) +connection = xmpp.Client(jid.getDomain(), debug=[]) + +bot = Bot(connection, receiver) + +if not bot.xmpp_connect(): + sys.stderr.write("Could not connect to server.\n") + sys.exit(1) + +connection.sendInitPresence() + +online = 1 +while online: + connection.Process(1)