|
|
@ -2,92 +2,101 @@ from xbotlib import Bot |
|
|
|
import json |
|
|
|
import jinja2 |
|
|
|
import re |
|
|
|
import os |
|
|
|
import shutil |
|
|
|
|
|
|
|
output = './' |
|
|
|
|
|
|
|
def add_to_db(self, message): |
|
|
|
keys = [int(x) for x in self.db[message.room].keys()] |
|
|
|
if not keys: |
|
|
|
new_key = '0' |
|
|
|
else: |
|
|
|
new_key = str(keys[-1] + 1) |
|
|
|
self.db[message.room][new_key] = message.content |
|
|
|
|
|
|
|
def del_from_db(self, message, key): |
|
|
|
del self.db[message.room][key] |
|
|
|
|
|
|
|
def write_log(self, message): |
|
|
|
template_file = 'index.html.j2' # Hmm... how to read the self.template ? |
|
|
|
print('using the template: ', template_file) |
|
|
|
template = jinja2.Template(open(template_file).read()) |
|
|
|
if not os.path.isdir(message.room): |
|
|
|
os.mkdir(message.room) |
|
|
|
shutil.copy('stylesheet.css', message.room) |
|
|
|
with open(f'{ output }/{ message.room }/index.html','w') as out: |
|
|
|
html = template.render(room=message.room, db=self.db[message.room]) |
|
|
|
out.write(html) |
|
|
|
print('writing to: ', f'{ message.room }/index.html') |
|
|
|
|
|
|
|
db = 'storage.json' |
|
|
|
|
|
|
|
def readdb(): |
|
|
|
storage = open(db, 'r').read() |
|
|
|
messages = json.loads(storage) |
|
|
|
return messages |
|
|
|
|
|
|
|
def writedb(message): |
|
|
|
try: |
|
|
|
with open(db, 'r') as storage: |
|
|
|
messages = json.loads(storage.read()) |
|
|
|
if messages.keys(): |
|
|
|
keys = [int(x) for x in messages.keys()] |
|
|
|
keys.sort() |
|
|
|
lastid = keys[-1] |
|
|
|
nextid = lastid + 1 |
|
|
|
else: |
|
|
|
nextid = 0 |
|
|
|
messages[f'{ nextid }'] = message |
|
|
|
storage = open(db, 'w') |
|
|
|
storage.write(json.dumps(messages, indent=4)) |
|
|
|
except IOError: |
|
|
|
with open(db, 'w') as storage: |
|
|
|
storage.write(json.dumps('{}')) |
|
|
|
writedb(message) |
|
|
|
return messages |
|
|
|
|
|
|
|
def deletefromdb(id): |
|
|
|
with open(db, 'r') as storage: |
|
|
|
messages = json.loads(storage.read()) |
|
|
|
del messages[id] |
|
|
|
storage = open(db, 'w') |
|
|
|
storage.write(json.dumps(messages, indent=4)) |
|
|
|
return messages |
|
|
|
|
|
|
|
def writelog(messages): |
|
|
|
template = jinja2.Template(open('index.html.j2').read()) |
|
|
|
with open('log.html','w') as out: |
|
|
|
html = template.render(messages=messages) |
|
|
|
out.write(html) |
|
|
|
|
|
|
|
class logbot(Bot): |
|
|
|
|
|
|
|
help = '''Oh dear, logbot is here! |
|
|
|
help = '''Oh dear, logbot is here! |
|
|
|
|
|
|
|
@delete <num> |
|
|
|
Delete posts from the log. |
|
|
|
For example: @logbot @delete 5 |
|
|
|
(You can speak to the bot using @logbot or logbot:) |
|
|
|
|
|
|
|
@bots |
|
|
|
To see who is around :) |
|
|
|
<image>: Your image is added to the log. |
|
|
|
|
|
|
|
@uptime |
|
|
|
To check how long @logbot has been around |
|
|
|
logbot @help: Print this message |
|
|
|
|
|
|
|
@help |
|
|
|
Print this message |
|
|
|
logbot @add <message>: Add a message to the log. |
|
|
|
|
|
|
|
logbot @delete <num>: Delete posts from the log. For example: @logbot @delete 5 |
|
|
|
|
|
|
|
logbot @title <string>: Set the title of your log. [future-feature] |
|
|
|
|
|
|
|
logbot @style <element> <css-rule>: Edit the css of your log. For example: logbot @style body background-color: pink; [future-feature] |
|
|
|
|
|
|
|
logbot @uptime: To check how long @logbot has been around |
|
|
|
|
|
|
|
@bots: To see who is around :) |
|
|
|
''' |
|
|
|
|
|
|
|
def group(self, message): |
|
|
|
print(message.content) |
|
|
|
messages = readdb() |
|
|
|
|
|
|
|
if message.url: |
|
|
|
messages = writedb(f'<img src="{ message.url }">') |
|
|
|
reply = 'Thanks for that image!' |
|
|
|
|
|
|
|
elif '@delete' in message.text: |
|
|
|
match = re.findall("@delete \d*", message.content)[0] |
|
|
|
id = match.replace('@delete ','') |
|
|
|
if id in messages: |
|
|
|
print('To be deleted:', messages[str(id)]) |
|
|
|
reply = f'This message is deleted: { messages[str(id)] }' |
|
|
|
messages = deletefromdb(id) |
|
|
|
else: |
|
|
|
reply = 'This message is already gone!' |
|
|
|
|
|
|
|
elif '@help' in message.text: |
|
|
|
print('HELP') |
|
|
|
|
|
|
|
else: |
|
|
|
messages = writedb(message.text) |
|
|
|
reply = 'Added, thanks!' |
|
|
|
|
|
|
|
writelog(messages) |
|
|
|
return self.reply(reply, room=message.room) |
|
|
|
def group(self, message): |
|
|
|
|
|
|
|
if not message.room in self.db.keys(): |
|
|
|
self.db[message.room] = {} |
|
|
|
|
|
|
|
# to debug in the terminal |
|
|
|
print('------------------') |
|
|
|
print('message: ', message.text) |
|
|
|
print('room: ', message.room) |
|
|
|
print('sender: ', message.sender) |
|
|
|
|
|
|
|
if message.url: |
|
|
|
# messages = writedb(f'<img src="{ message.url }">') |
|
|
|
reply = 'Thanks for that image!' |
|
|
|
|
|
|
|
elif '@add' in message.text: |
|
|
|
add_to_db(self, message) |
|
|
|
reply = 'Added, thanks!' |
|
|
|
|
|
|
|
elif '@delete' in message.text: |
|
|
|
match = re.findall("@delete \d*", message.content)[0] |
|
|
|
key = str(match.replace('@delete ','')) |
|
|
|
|
|
|
|
if key in self.db[message.room]: |
|
|
|
print('To be deleted:', self.db[message.room][key]) |
|
|
|
reply = f'This message is deleted: { self.db[message.room][key] }' |
|
|
|
del_from_db(self, message, key) |
|
|
|
else: |
|
|
|
reply = 'This message is already gone!' |
|
|
|
|
|
|
|
elif '@help' in message.text: |
|
|
|
print('HELP') |
|
|
|
|
|
|
|
elif '@title' in message.text: |
|
|
|
reply = 'This is a future-feature ...' |
|
|
|
|
|
|
|
elif '@style' in message.text: |
|
|
|
reply = 'This is a future-feature ...' |
|
|
|
|
|
|
|
else: |
|
|
|
reply = 'Hmm ... not sure what you want to do?' |
|
|
|
|
|
|
|
write_log(self, message) |
|
|
|
return self.reply(reply, room=message.room) |
|
|
|
|
|
|
|
logbot() |
|
|
|