2022-05-18 03:59:32 -04:00
|
|
|
import json
|
|
|
|
import requests
|
2022-08-09 15:39:51 -04:00
|
|
|
import easyappointments
|
2022-05-18 03:59:32 -04:00
|
|
|
"""
|
|
|
|
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())
|
2022-08-09 15:39:51 -04:00
|
|
|
self.appointments = easyappointments.easyappointments(self.data['easyappointments']['token'], self.data['easyappointments']['url'])
|
2022-05-18 03:59:32 -04:00
|
|
|
# Only refresh data when a change is made
|
2022-05-20 06:35:33 -04:00
|
|
|
def update_data(self):
|
|
|
|
with open('data.json','r') as f:
|
|
|
|
self.data = json.loads(f.read())
|
2022-05-18 03:59:32 -04:00
|
|
|
def save(self):
|
|
|
|
with open('data.json','w') as f:
|
|
|
|
f.write(json.dumps(self.data,indent=2))
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-05-20 06:35:33 -04:00
|
|
|
#Administration
|
2022-05-18 03:59:32 -04:00
|
|
|
def is_admin(self,handle):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
pass
|
|
|
|
def add_admin(self,handle):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
if self.is_admin(handle):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.data["admins"].append(handle)
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
def remove_admin(self,handle):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#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
|
|
|
|
|
2022-05-20 06:35:33 -04:00
|
|
|
|
|
|
|
#state management
|
|
|
|
def get_user_state(self,handle):
|
|
|
|
self.update_data()
|
|
|
|
if handle not in self.data["users"]:
|
|
|
|
self.data["users"][handle]={"state":[]}
|
|
|
|
self.save()
|
|
|
|
return self.data["users"][handle]["state"]
|
|
|
|
|
|
|
|
def update_user_state(self,handle,state):
|
|
|
|
self.update_data()
|
|
|
|
current_state = self.get_user_state(handle)
|
|
|
|
self.data["users"][handle]["state"] = state
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-05-18 03:59:32 -04:00
|
|
|
#FAQ
|
2022-05-20 06:35:33 -04:00
|
|
|
def get_faq_string(self):
|
|
|
|
self.update_data()
|
|
|
|
faq = self.data["faq"]
|
|
|
|
return faq["header"] + "".join(
|
|
|
|
["\n# "+q["question"]+"\n"+q["answer"]+"\n" for q in faq["questions"]]
|
|
|
|
) + "\n"+ faq["footer"]
|
|
|
|
|
|
|
|
def get_faq_questions(self):
|
|
|
|
self.update_data()
|
|
|
|
questions = self.data['faq']['questions']
|
|
|
|
response = ""
|
|
|
|
index = 0
|
|
|
|
for question in questions:
|
|
|
|
response += f"[{index}]:{question['question']}\n"
|
|
|
|
index += 1
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-18 03:59:32 -04:00
|
|
|
def get_header(self):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
return self.data["faq"]["header"]
|
|
|
|
def set_header(self,hdr):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
self.data["faq"]["header"] = hdr
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
def get_questions(self):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return questions
|
|
|
|
return self.data["faq"]["questions"]
|
|
|
|
def add_question(self,qtn):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
self.data["faq"]["questions"].append(qtn)
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
def remove_question(self,qtn_i):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
self.data["faq"]["questions"].pop(qtn_i)
|
|
|
|
self.save()
|
|
|
|
return True
|
|
|
|
def update_question(self,qtn_i,qtn):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
#return bool
|
|
|
|
self.data["faq"]["questions"][qtn_i]=qtn
|
|
|
|
return True
|
|
|
|
|
|
|
|
#Meetings
|
2022-08-09 15:39:51 -04:00
|
|
|
def get_meetings(self,mxid):
|
2022-05-20 06:35:33 -04:00
|
|
|
self.update_data()
|
2022-08-09 15:39:51 -04:00
|
|
|
self.appointments.list_upcoming_appointments(mxid)
|
2022-05-18 03:59:32 -04:00
|
|
|
pass
|
2022-05-20 06:35:33 -04:00
|
|
|
def request_meeting(self):
|
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
pass
|
2022-05-20 06:35:33 -04:00
|
|
|
def accept_meeting(self):
|
|
|
|
self.update_data()
|
2022-05-18 03:59:32 -04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
storeAPI = API()
|
|
|
|
print("Storage loaded.\n",storeAPI.data['username'])
|