Browse Source

changes to gather and spark

master
Cristina Cochior 5 years ago
parent
commit
0990f70c9b
  1. 72
      gather.py
  2. 81
      spark.py

72
gather.py

@ -1,50 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# To run this bot:
# $ python3 logbot.py
# The output folder of this bot currently is: /var/www/logs/digital-autonomy
#######################################################################################################
#######################################################################################################
### You can use this code by calling three different options in the chat: ###
### "@glossary-all word": adds the word "word" in the list ###
### "@glossary-drift": returns a term from the list that was selected by chance ###
### "@glossary-all": shows all terms in the list ###
#######################################################################################################
#######################################################################################################
"""
This bot will add words and expressions to a file.
You can use this code in the chat by calling three different options in the chat:
"@glossary-add word": adds the word "word" in the list
"@glossary-drift": returns a term from the list that was selected by chance
"@glossary-all": returns a list of all the saved expressions.
To run this bot, type the following command in your terminal:
python3 gather.py -d -j administratorbot@conversejs.org -r botsofconduct@muc.vvvvvvaria.org -n gatherbot -p testing -o .
"""
import logging
from getpass import getpass
from argparse import ArgumentParser
import slixmpp
import ssl, os, requests, urllib
class MUCBot(slixmpp.ClientXMPP):
"""
A simple Slixmpp bot that will save images
and messages that are marked with @bot to a folder.
"""
def __init__(self, jid, password, room, nick, output):
slixmpp.ClientXMPP.__init__(self, jid, password)
self.room = room
self.nick = nick
self.output = output
# The session_start event will be triggered when
# the bot establishes its connection with the server
# and the XML logs are ready for use. We want tob
# listen for this event so that we we can initialize
# our roster.
self.add_event_handler("session_start", self.start)
# The groupchat_message event is triggered whenever a message
# stanza is received from any chat room. If you also also
# register a handler for the 'message' event, MUC messages
# will be processed by both handlers.
self.add_event_handler("groupchat_message", self.muc_message)
@ -59,10 +40,6 @@ class MUCBot(slixmpp.ClientXMPP):
wait=True)
def muc_message(self, msg):
# Some inspection commands
#print('Message: {}'.format(msg))
# Always check that a message is not the bot itself, otherwise you will create an infinite loop responding to your own messages.
if msg['mucnick'] != self.nick:
# Check if output folder exists
@ -73,12 +50,10 @@ class MUCBot(slixmpp.ClientXMPP):
if '@glossary-add' in msg['body']:
# reply from the bot
self.send_message(mto=self.room,
mbody="new word(s) added - language in motion".format(msg['mucnick']),
mtype='groupchat')
self.send_message(mto=self.room, mbody="new word(s) added; language is in motion".format(msg['mucnick']), mtype='groupchat')
# Add message to log
message = '{}'.format(msg['body'].replace('@growinggloss',''))
message = '{}'.format(msg['body'].replace('@glossary-add',''))
log = 'glossary.txt'
log_path = os.path.join(self.output, log)
f = open(log_path, 'a+')
@ -96,10 +71,7 @@ class MUCBot(slixmpp.ClientXMPP):
filelist.append(line)
print(filelist)
self.send_message(mto=self.room,
mbody= filelist,
mtype='groupchat')
self.send_message(mto=self.room, mbody= filelist, mtype='groupchat')
# retrieves a random term from the growing glossary file:
if '@glossary-drift' in msg['body']:
@ -112,10 +84,7 @@ class MUCBot(slixmpp.ClientXMPP):
filelist.append(line)
print(filelist)
self.send_message(mto=self.room,
mbody= random.choice(filelist),
mtype='groupchat')
self.send_message(mto=self.room, mbody= random.choice(filelist), mtype='groupchat')
if __name__ == '__main__':
@ -174,3 +143,16 @@ if __name__ == '__main__':
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
xmpp.process()
"""
Slixmpp: The Slick XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of Slixmpp.
See the file LICENSE for copying permission.
https://lab.louiz.org/poezio/slixmpp/blob/master/LICENSE
The code has been modified for the Collective Conditions work session in Brussels, 2019, by Cristina Cochior and Joana Chicau.
http://constantvzw.org/site/-Collective-Conditions,220-.html
"""

81
spark.py

@ -1,44 +1,31 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# To run this bot:
# $ python3 logbot.py
"""
This bot replies to a specific word.
On line 42, you can edit the word-to-be-responded-to after "if '":
if 'target-word' in msg['body']:
On line 44, you can edit the response options after "wordslist = ['":
wordslist = ['a response to the target-word.']
# This bot replies to a specific word — see line 67.
To run this bot, type the following command in your terminal:
python3 spark.py -d -j administratorbot@conversejs.org -r botsofconduct@muc.vvvvvvaria.org -n sparkbot -p testing
"""
import logging
from getpass import getpass
from argparse import ArgumentParser
import slixmpp
import random
import ssl, os, requests, urllib
class MUCBot(slixmpp.ClientXMPP):
"""
A simple Slixmpp bot that will save images
and messages that are marked with @bot to a folder.
"""
def __init__(self, jid, password, room, nick, output):
def __init__(self, jid, password, room, nick):
slixmpp.ClientXMPP.__init__(self, jid, password)
self.room = room
self.nick = nick
self.output = output
# The session_start event will be triggered when
# the bot establishes its connection with the server
# and the XML logs are ready for use. We want tob
# listen for this event so that we we can initialize
# our roster.
self.add_event_handler("session_start", self.start)
# The groupchat_message event is triggered whenever a message
# stanza is received from any chat room. If you also also
# register a handler for the 'message' event, MUC messages
# will be processed by both handlers.
self.add_event_handler("groupchat_message", self.muc_message)
@ -47,35 +34,17 @@ class MUCBot(slixmpp.ClientXMPP):
self.send_presence()
# https://xmpp.org/extensions/xep-0045.html
self.plugin['xep_0045'].join_muc(self.room,
self.nick,
# If a room password is needed, use:
# password=the_room_password,
wait=True)
self.plugin['xep_0045'].join_muc(self.room, self.nick, wait=True)
def muc_message(self, msg):
# Some inspection commands
#print('Message: {}'.format(msg))
# Always check that a message is not the bot itself, otherwise you will create an infinite loop responding to your own messages.
if msg['mucnick'] != self.nick:
# Everytime someone types this word or expression, the bot replies. You can edit it here:
if 'collective' in msg['body']:
# The list of responses one line below can be expanded with the following format: ['text','text','text']
wordslist = ['conditions']
# The bot will respond with one of the options at random:
self.send_message(mto=self.room,mbody= random.choice(wordslist), mtype='groupchat')
# Check if output folder exists
if not os.path.exists(self.output):
os.mkdir(self.output)
# everytime someone types target-word, the bot replies:
if 'target-word' in msg['body']:
import random
wordslist = ['a response to the target-word.']
# reply from the bot
self.send_message(mto=self.room,
mbody= random.choice(wordslist),
mtype='groupchat')
if __name__ == '__main__':
# Setup the command line arguments.
@ -118,13 +87,11 @@ if __name__ == '__main__':
args.room = input("MUC room: ")
if args.nick is None:
args.nick = input("MUC nickname: ")
if args.output is None:
args.output = input("Output folder: ")
# Setup the MUCBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
xmpp = MUCBot(args.jid, args.password, args.room, args.nick, args.output)
xmpp = MUCBot(args.jid, args.password, args.room, args.nick)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
@ -134,4 +101,14 @@ if __name__ == '__main__':
xmpp.connect()
xmpp.process()
"""
Slixmpp: The Slick XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of Slixmpp.
See the file LICENSE for copying permission.
https://lab.louiz.org/poezio/slixmpp/blob/master/LICENSE
The code has been modified for the Collective Conditions work session in Brussels, 2019, by Cristina Cochior and Joana Chicau.
http://constantvzw.org/site/-Collective-Conditions,220-.html
"""

Loading…
Cancel
Save