#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# le RMS XMPP bot
# usage: relearn.py [-h] -j JID -p PASSWORD -m MUC
# for example: $ python relearn.py -j username@domain.org -p entrepot -m room@muc.domain.org
# before running this script:
# $ pip install argparse logging sleekxmpp pyasn1 pyasn1_modules
import argparse, json, logging, sleekxmpp
# command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--jid", help="jabber identifier", type=str, required=True)
parser.add_argument("-p", "--password", help="password", type=str, required=True)
parser.add_argument("-m", "--muc", help="destination muc", type=str, required=True)
args = parser.parse_args()
with open('commands.json') as data_file:
data = json.load(data_file)
# la Relearn tool introduction bot
class Relearn(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, room, nick):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.room = room
self.nick = nick
self.add_event_handler("session_start", self.start)
self.add_event_handler("groupchat_message", self.collect_from_etherpad)
self.add_event_handler("muc::%s::got_online" % self.room, self.welcome)
def start(self, event):
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room, self.nick)
# XEP-0084 User Avatar
# Requires SleekXMPP 81b7b2c1908e0f6a5435ce67745b5f4dafb59816
with open('pg.jpg', 'rb') as avatar_file:
avatar = avatar_file.read()
avatar_id = self['xep_0084'].generate_id(avatar)
info = {
'id': avatar_id,
'type': 'image/jpeg',
'bytes': len(avatar)
}
self['xep_0084'].publish_avatar(avatar)
self['xep_0084'].publish_avatar_metadata(items=[info])
# XEP-0153: vCard-Based Avatars
# Not working ATM
#self['xep_0153'].set_avatar(avatar=avatar, mtype='image/png')
def welcome(self, presence):
if presence['muc']['nick'] != self.nick:
self.send_message(mto=presence['from'].bare,
mbody="Hello %s, welcome to this collection muc called '%s'. To collect content from one of the etherpads, send a message to this muc with padname number, for example: 'etherpad start 11'. Enjoy!" % (presence['muc']['nick'], self.room.replace('@muc.complex.local','')),
mtype='groupchat')
def collect_from_etherpad(self, msg):
if 'etherpad' in msg['body'].lower():
if len(msg['body'].lower().split()) >= 3:
padname = msg['body'].lower().split(' ')[1]
line = msg['body'].lower().split(' ')[2]
if padname or line:
self.send_message(mto=msg['from'].bare, mbody='Line number %s from %s is collected.' % (line, padname) , mtype='groupchat')
else:
reply = "To collect content from one of the etherpads, send a message to this muc with padname number, for example: 'etherpad start 11'."
self.send_message(mto=msg['from'].bare, mbody=reply, mtype='groupchat')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(levelname)-8s %(message)s')
client = Relearn(args.jid, args.password, args.muc, "bot")
client.register_plugin('xep_0045')
client.register_plugin('xep_0030')
client.register_plugin('xep_0084')
#client.register_plugin('xep_0153')
if client.connect():
client.process(block=True)
else:
print("Can't connect.")
collection-bot.py