From 9c717bd7a8faa8e98b7a96f4f311172975ba1f87 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 9 Aug 2022 15:39:51 -0400 Subject: [PATCH] easyappointments mvp --- commands.py | 6 ++- easyappointments.py | 93 +++++++++++++++++++++++++++++++++++++++++++++ features.py | 5 ++- main.py | 2 +- 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 easyappointments.py diff --git a/commands.py b/commands.py index 212387b..3aa721e 100644 --- a/commands.py +++ b/commands.py @@ -1,5 +1,4 @@ import random -from turtle import update import features api = features.API() @@ -198,6 +197,10 @@ def remove_admin(state, message, sender): } +def meetings(state,message,sender): + return { + 'response':api.appointments.list_upcoming_appointments(sender) + } def appointments(): @@ -219,6 +222,7 @@ commands = { "!remove footer": remove_footer, "!add admin": add_admin, "!remove admin": remove_admin, + "meetings":meetings } diff --git a/easyappointments.py b/easyappointments.py new file mode 100644 index 0000000..d7245b8 --- /dev/null +++ b/easyappointments.py @@ -0,0 +1,93 @@ +import requests,json + +class easyappointments: + def __init__(self,token,url): + self.token = token + self.url = url + self.headers = {"Authorization":"Bearer secrettoken"} + self.appointments = {} + self.services = {} + self.providers = {} + self.customers = {} + self.update_data() + + def get_appointments(self): + data = requests.get(self.url+'/api/v1/appointments',headers=self.headers) + return data.json() + def get_services(self): + data = requests.get(self.url+'/api/v1/services',headers=self.headers) + return data.json() + def get_providers(self): + data = requests.get(self.url+'/api/v1/providers',headers=self.headers) + return data.json() + def get_customers(self): + data = requests.get(self.url+'/api/v1/customers',headers=self.headers) + return data.json() + + def update_data(self): + appointments = self.get_appointments() + for a in appointments: + self.appointments[a['id']] = a + services = self.get_services() + for s in services: + self.services[s['id']]=s + providers = self.get_providers() + for p in providers: + self.providers[p['id']]=p + customers = self.get_customers() + for c in customers: + self.customers[c['id']]=c + + def is_customer(self,mxid): + customers = self.get_customers() + for c in customers: + if mxid in c['notes']: + return c['id'] + return False + + def is_provider(self,mxid): + providers = self.get_providers() + for p in providers: + if mxid in p['notes']: + return p['id'] + + def get_upcoming_appointments(self,mxid): + #get services & customers + id = self.is_provider(mxid) + if id: + apts = [] + data = self.get_appointments() + for a in data: + if a['providerId'] == id: + apts.append(a) + return apts + else: + return [] + + def list_upcoming_appointments(self,mxid): + apts = self.get_upcoming_appointments(mxid) + if apts == []: + return "No meetings recorded." + output = "" + for a in apts: + service = self.services[a['serviceId']]['name'] + customer = self.customers[a['customerId']]['firstName'] + output += f"## {service} with {customer}\nTime:{a['start']}\n" + return output + + def register_customer(self,customer_data): + #customer data + """ + { + "firstName":"name", + "lastName":"name", + "email":"email", + "phone":"phone" + "notes":"matrix:mxid" + } + """ + id = requests.post(self.url+'/api/v1/customers',headers=self.headers,data=json.dumps(customer_data)).json()['id'] + return id + + + diff --git a/features.py b/features.py index cdb6adf..e2ca2c9 100644 --- a/features.py +++ b/features.py @@ -1,5 +1,6 @@ import json import requests +import easyappointments """ Features This is the main API for executing functions with data @@ -10,6 +11,7 @@ class API: def __init__(self): with open('data.json') as f: self.data = json.loads(f.read()) + self.appointments = easyappointments.easyappointments(self.data['easyappointments']['token'], self.data['easyappointments']['url']) # Only refresh data when a change is made def update_data(self): with open('data.json','r') as f: @@ -115,8 +117,9 @@ class API: return True #Meetings - def get_meetings(self): + def get_meetings(self,mxid): self.update_data() + self.appointments.list_upcoming_appointments(mxid) pass def request_meeting(self): self.update_data() diff --git a/main.py b/main.py index 54bd85e..fb3256c 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ creds = botlib.Creds( bot = botlib.Bot(creds) @bot.listener.on_message_event -async def faq(room, message): +async def message(room, message): result = commands.handle_command(message.sender, message.body) if result['response'] != None and result['response'] != "": await bot.api.send_markdown_message(