This commit is contained in:
Gabriel 2022-05-18 03:59:32 -04:00
parent 2826163493
commit 6c71c30454
5 changed files with 110 additions and 10 deletions

View file

@ -26,5 +26,11 @@ You may want to set data.json as a volume to be able to make changes on the fly
![matrix room screenshot](screenshots/example2.png) ![matrix room screenshot](screenshots/example2.png)
## Features in the works
* Alerts
* Dynamic configuration
* Meeting requests
* Configurable API calls

View file

@ -1,18 +1,19 @@
import random import random
import features
jokes = [ jokes = [
["Testing","Testing you!"], ["Testing","Testing you!"],
["singing pokemon","Jiggalypuff! 🎙️"] ["singing pokemon","Jiggalypuff! 🎙️"]
] ]
#jokes = [] #jokes = []
def handle_command(state,message): def handle_command(state,sender,message):
for command in commands: for command in commands:
if message.find(command)==0: if message.find(command)==0:
result = commands[command](state,message) result = commands[command](state,message,sender)
return result return result
if len(state) > 0: if len(state) > 0:
try: try:
result = commands[state[0]](state,message) result = commands[state[0]](state,message,sender)
return result return result
except Exception as e: except Exception as e:
return { return {
@ -39,13 +40,13 @@ def handle_command(state,message):
bot: Nice one! -> bot saves knock-knock joke bot: Nice one! -> bot saves knock-knock joke
""" """
def nevermind(state,message): def nevermind(state,message,sender):
return { return {
"response":"starting over.", "response":"starting over.",
"state":[] "state":[]
} }
def knock_knock(state,message): def knock_knock(state,message,sender):
#return {response:"",state:""} #return {response:"",state:""}
try: try:
if state[0] != "knock knock": if state[0] != "knock knock":
@ -93,7 +94,7 @@ def knock_knock(state,message):
bot: bot:
""" """
def joke(state,message): def joke(state,message,sender):
if len(jokes) == 0: if len(jokes) == 0:
return { return {
"response":"I don't know any jokes...", "response":"I don't know any jokes...",
@ -135,7 +136,15 @@ def joke(state,message):
"state":[] "state":[]
} }
"""
"""
def appointments():
return {
"response":None,
state:[]
}
commands = { commands = {
"nevermind":nevermind, "nevermind":nevermind,
"knock knock":knock_knock, "knock knock":knock_knock,
@ -144,7 +153,6 @@ commands = {
if __name__ == '__main__': if __name__ == '__main__':
msg = "" msg = ""
while msg != "exit": while msg != "exit":

View file

@ -23,5 +23,7 @@
"footer": "Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)" "footer": "Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)"
}, },
"users": {} "users": {},
"admins": [],
"meetings": []
} }

82
features.py Normal file
View file

@ -0,0 +1,82 @@
import json
import requests
"""
Features
This is the main API for executing functions with data
"""
class API:
def __init__(self):
with open('data.json') as f:
self.data = json.loads(f.read())
# Only refresh data when a change is made
def save(self):
with open('data.json','w') as f:
f.write(json.dumps(self.data,indent=2))
return True
return False
#Administration
def is_admin(self,handle):
#return bool
pass
def add_admin(self,handle):
#return bool
if self.is_admin(handle):
return True
else:
self.data["admins"].append(handle)
self.save()
return True
def remove_admin(self,handle):
#return bool
if self.is_admin(handle):
i = self.data["admins"].index(handle)
self.data["admins"].pop(i)
self.save()
return True
else:
return True
#FAQ
def get_header(self):
return self.data["faq"]["header"]
def set_header(self,hdr):
#return bool
self.data["faq"]["header"] = hdr
self.save()
return True
def get_questions(self):
#return questions
return self.data["faq"]["questions"]
def add_question(self,qtn):
#return bool
self.data["faq"]["questions"].append(qtn)
self.save()
return True
def remove_question(self,qtn_i):
#return bool
self.data["faq"]["questions"].pop(qtn_i)
self.save()
return True
def update_question(self,qtn_i,qtn):
#return bool
self.data["faq"]["questions"][qtn_i]=qtn
return True
#Meetings
def get_meetings():
pass
def request_meeting():
pass
def accept_meeting():
pass
if __name__ == '__main__':
storeAPI = API()
print("Storage loaded.\n",storeAPI.data['username'])

View file

@ -3,6 +3,8 @@ import nio
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
import commands import commands
#TODO move state functionality entirely to features.py
data = json.loads(open('data.json').read()) data = json.loads(open('data.json').read())
dfaq = data["faq"] dfaq = data["faq"]
userstate = data['users'] userstate = data['users']
@ -34,7 +36,7 @@ async def faq(room, message):
state = userstate[message.sender]['state'] state = userstate[message.sender]['state']
result = commands.handle_command(state,message.body) result = commands.handle_command(state,message.sender,message.body)
if result['response']!= None: if result['response']!= None:
state = result['state'] state = result['state']
userstate[message.sender]['state'] = result['state'] userstate[message.sender]['state'] = result['state']