2022-03-03 14:18:07 -05:00
import random
2022-05-18 03:59:32 -04:00
import features
2022-09-28 11:02:35 -04:00
import json
2022-03-03 14:18:07 -05:00
2022-05-20 06:35:33 -04:00
api = features . API ( )
debug = False
def handle_command ( sender , message ) :
state = api . get_user_state ( sender )
2022-03-03 14:18:07 -05:00
for command in commands :
2022-05-20 06:35:33 -04:00
if message . find ( command ) == 0 :
result = commands [ command ] ( state , message , sender )
return result
2022-03-07 04:24:40 -05:00
if len ( state ) > 0 :
2022-05-20 06:35:33 -04:00
if debug == 1 :
return commands [ state [ 0 ] ] ( state , message , sender )
2022-03-07 04:24:40 -05:00
try :
2022-05-20 06:35:33 -04:00
result = commands [ state [ 0 ] ] ( state , message , sender )
2022-03-07 04:24:40 -05:00
return result
except Exception as e :
2022-09-28 11:02:35 -04:00
if __name__ != ' __main__ ' :
state = [ ]
api . update_user_state ( sender , state )
2022-03-07 04:24:40 -05:00
return {
2022-09-28 11:02:35 -04:00
" response " : f " ⚠️ Error⚠️ \n { e } "
2022-03-07 04:24:40 -05:00
}
2022-05-20 06:35:33 -04:00
2022-03-07 04:24:40 -05:00
return {
2022-05-20 06:35:33 -04:00
" response " : " " ,
2022-03-07 04:24:40 -05:00
}
2022-03-03 14:18:07 -05:00
2022-05-20 06:35:33 -04:00
def get_faq_string ( ) :
return api . get_faq_string ( )
def nevermind ( state , message , sender ) :
api . update_user_state ( sender , [ ] )
2022-03-03 14:18:07 -05:00
return {
2022-05-20 06:35:33 -04:00
" response " : " starting over. "
2022-03-03 14:18:07 -05:00
}
2022-05-20 06:35:33 -04:00
# faq
def get_faq ( state , message , sender ) :
2022-03-03 14:18:07 -05:00
if len ( state ) == 0 :
return {
2022-05-20 06:35:33 -04:00
" response " : api . get_faq_string ( )
2022-03-03 14:18:07 -05:00
}
return {
2022-05-20 06:35:33 -04:00
" response " : api . get_faq_string ( )
2022-03-03 14:18:07 -05:00
}
2022-05-20 06:35:33 -04:00
def add_question ( state , message , sender ) :
2022-03-03 14:18:07 -05:00
if len ( state ) == 0 :
2022-05-20 06:35:33 -04:00
api . update_user_state ( sender , [ " !add question " ] )
2022-03-03 14:18:07 -05:00
return {
2022-05-20 06:35:33 -04:00
' response ' : " Please enter the question title. "
2022-03-03 14:18:07 -05:00
}
2022-03-07 04:24:40 -05:00
elif len ( state ) == 1 :
2022-05-20 06:35:33 -04:00
state . append ( message )
api . update_user_state ( sender , state )
return {
' response ' : ' Please enter the answer to the question. '
}
elif len ( state ) == 2 :
state . append ( message )
api . update_user_state ( sender , state )
state = api . get_user_state ( sender )
return {
' response ' : f " Your question is: \n --- \n # { state [ 1 ] } \n { state [ 2 ] } \n \n --- \n If this is what you want respond with `confirm` \n If you ' d like to start over respond with `restart` \n Respond with `nevermind` to cancel entirely "
}
elif len ( state ) == 3 :
if message == ' confirm ' :
api . add_question ( {
' question ' : state [ 1 ] ,
' answer ' : state [ 2 ]
} )
api . update_user_state ( sender , [ ] )
2022-03-03 14:18:07 -05:00
return {
2022-05-20 06:35:33 -04:00
' response ' : ' Question added. '
2022-03-03 14:18:07 -05:00
}
2022-05-20 06:35:33 -04:00
elif message == ' restart ' :
api . update_user_state ( sender , [ ' !add question ' ] )
2022-03-03 14:18:07 -05:00
return {
2022-05-20 06:35:33 -04:00
' response ' : ' Please enter the question title. '
2022-03-03 14:18:07 -05:00
}
else :
return {
2022-05-20 06:35:33 -04:00
' response ' : f " Your question is: \n # { state [ 1 ] } \n { state [ 2 ] } \n \n If this is what you want respond with `confirm` \n If you ' d like to start over respond with `restart` \n Respond with `nevermind` to cancel entirely "
2022-03-03 14:18:07 -05:00
}
2022-05-20 06:35:33 -04:00
2022-03-07 04:24:40 -05:00
return {
2022-05-20 06:35:33 -04:00
' response ' : None
2022-03-07 04:24:40 -05:00
}
2022-03-03 14:18:07 -05:00
2022-05-20 06:35:33 -04:00
def update_question ( state , message , sender ) :
if len ( api . data [ ' faq ' ] [ ' questions ' ] ) == 0 :
api . update_user_state ( sender , [ ] )
2022-05-18 03:59:32 -04:00
return {
2022-05-20 06:35:33 -04:00
' response ' : " No questions registered. "
2022-05-18 03:59:32 -04:00
}
2022-05-20 06:35:33 -04:00
elif len ( state ) == 0 :
api . update_user_state ( sender , " !update question " )
return {
" response " : f " Choose a question to update: \n { api . get_faq_questions } "
}
return {
' response ' : None ,
}
def remove_question ( state , message , sender ) :
if len ( api . data [ ' faq ' ] [ ' questions ' ] ) == 0 :
api . update_user_state ( sender , [ ] )
return {
' response ' : " No questions registered. "
}
elif len ( state ) == 0 :
api . update_user_state ( sender , [ " !remove question " ] )
return {
" response " : f " Choose a question to remove: \n { api . get_faq_questions ( ) } "
}
elif len ( state ) == 1 :
try :
int ( message )
except :
return { " response " : " Please enter a number. " }
state . append ( message )
if int ( state [ 1 ] ) < len ( api . data [ ' faq ' ] [ ' questions ' ] ) and int ( state [ 1 ] ) > = 0 :
if debug == True :
api . remove_question ( int ( state [ 1 ] ) )
api . update_user_state ( sender , [ ] )
return {
' response ' : ' Question removed '
}
else :
try :
api . update_user_state ( sender , [ ] )
api . remove_question ( int ( state [ 1 ] ) )
return {
' response ' : ' Question removed '
}
except :
pass
api . update_user_state ( sender , [ " !remove question " ] )
return {
' response ' : ' Please enter the correct question # '
}
return {
' response ' : None ,
}
def update_header ( state , message , sender ) :
return {
' response ' : None ,
}
def remove_header ( state , message , sender ) :
return {
' response ' : None ,
}
def update_footer ( state , message , sender ) :
return {
' response ' : None ,
}
def remove_footer ( state , message , sender ) :
return {
' response ' : None ,
}
def add_admin ( state , message , sender ) :
return {
' response ' : None ,
}
def remove_admin ( state , message , sender ) :
return {
' response ' : None ,
}
2022-08-09 15:39:51 -04:00
def meetings ( state , message , sender ) :
return {
' response ' : api . appointments . list_upcoming_appointments ( sender )
}
2022-05-20 06:35:33 -04:00
2022-09-28 11:02:35 -04:00
def register_customer ( state , message , sender ) :
if api . appointments . is_customer ( sender ) :
return { ' response ' : " Already registered. " }
if state == [ ' !register2 ' ] :
return {
' response ' : ' What is your first name? '
}
2022-05-20 06:35:33 -04:00
2022-09-28 11:02:35 -04:00
if state == [ ] :
state = [ ' !register ' ]
api . update_user_state ( sender , state )
return {
' response ' : ' What is your first name? '
}
if len ( state ) == 1 :
state . append ( message )
api . update_user_state ( sender , state )
return {
' response ' : ' What is your last name? '
}
if len ( state ) == 2 :
state . append ( message )
api . update_user_state ( sender , state )
return {
' response ' : ' What is your e-mail? '
}
if len ( state ) == 3 :
state . append ( message )
api . update_user_state ( sender , state )
return {
' response ' : " What is your phone number? "
}
if len ( state ) == 4 :
state . append ( message )
api . update_user_state ( sender , state )
resp = f " You ' ve entered: ` { state [ 1 ] } { state [ 2 ] } ( { state [ 3 ] } / { state [ 4 ] } )` \n **Is this correct?** \n If so, respond with:Yes \n If not, to restart respond with :No \n You can always cancel with:nevermind "
return {
' response ' : resp
}
if len ( state ) == 5 :
if " yes " in message . lower ( ) :
data = {
" firstName " : state [ 1 ] ,
" lastName " : state [ 2 ] ,
" email " : state [ 3 ] ,
" phone " : state [ 4 ] ,
" notes " : f " matrix: { sender } "
}
if sender == " admin " :
dat = json . dumps ( data )
print ( f " Data: \n { dat } " )
if api . appointments . register_customer ( data ) :
if ( state [ 0 ] == ' !register2 ' ) :
state = [ ' !book ' ]
api . update_user_state ( sender , state )
return { ' response ' : " Successfully registered. \n \n " + booking ( [ ' !book ' ] , ' ' , sender ) [ ' response ' ] }
state = [ ]
api . update_user_state ( sender , state )
return { ' response ' : " Successfully registered. " }
else :
return { ' response ' : " Error, please try again. " }
api . update_user_state ( sender , [ ] )
return {
' response ' : ' '
}
def select_service ( state , message , sender ) :
if len ( state ) > = 2 :
return select_provider ( state , message , sender )
# ['!book','select-service']
if message == " !book " :
return { ' response ' : api . select_service ( ) }
else :
try :
state . append ( api . appointments . get_services ( ) [ int ( message ) ] [ ' id ' ] )
api . update_user_state ( sender , state )
return booking ( state , " " , sender )
except :
return { ' response ' : ' Error selecting service \n To cancel type:nevermind ' }
def select_provider ( state , message , sender ) :
if len ( state ) > = 3 :
return select_time ( state , message , sender )
if message == " " :
return { ' response ' : api . select_provider ( ) }
try :
state . append ( api . appointments . get_providers ( ) [ int ( message ) ] [ ' id ' ] )
api . update_user_state ( sender , state )
return booking ( state , " " , sender )
except :
return { ' response ' : ' Error selecting provider \n To cancel type:nevermind ' }
def select_time ( state , message , sender ) :
if len ( state ) > = 5 :
return booking ( state , message , sender )
api . update_data ( )
# ['!book','select-time']
if len ( state ) == 3 :
msg = api . select_times ( state [ 1 ] , state [ 2 ] , message )
if msg == False :
return { ' response ' : " Please enter a date you ' d like to book ( \" YYYY-MM-DD \" format) " }
state . append ( message )
api . update_user_state ( sender , state )
return { ' response ' : msg }
if len ( state ) == 4 :
times = api . appointments . get_availabilities ( state [ 1 ] , state [ 2 ] , state [ 3 ] )
if message in times :
state . append ( message )
time = None
try :
time = times [ int ( message ) - 1 ]
state . append ( time )
api . update_user_state ( sender , state )
return booking ( state , message , sender )
except :
return { ' response ' : api . select_times ( state [ 1 ] , state [ 2 ] , state [ 3 ] ) }
return { ' response ' : error }
def booking ( state , message , sender ) :
if len ( state ) > 5 :
state = [ ]
if state == [ ] :
state . append ( " !book " )
api . update_user_state ( sender , state )
"""
To book an appointment , you need ( in order ) :
* the customer ID ( now mapped to mxid ) ( 0 ? )
* The service id ( 1 )
* the provider id ( 2 )
* the start date & time ( 3 )
"""
#Quick checks
customer = api . appointments . is_customer ( sender )
if customer == False :
return register_customer ( [ ] , " " , sender )
#Service check
service = None
if len ( api . appointments . services ) == 0 :
return { ' response ' : " Error: there are no services " }
if len ( api . appointments . services ) == 1 :
service = api . appointments . get_services ( ) [ 0 ]
state . append ( service )
api . update_user_state ( sender , state )
return booking ( state , message , sender )
if len ( state ) > = 2 :
service = state [ 1 ]
if service == None :
return select_service ( state , message , sender )
#Provider check
provider = None
if len ( api . appointments . providers ) == 0 :
return { ' response ' : ' error: there are no providers ' }
if len ( state ) > = 3 :
provider = state [ 2 ]
elif len ( api . appointments . providers ) == 1 :
provider = api . appointments . get_providers ( ) [ 0 ] [ ' id ' ]
if service != None :
state . append ( provider )
api . update_user_state ( sender , state )
if provider == None :
return select_provider ( state , message , sender )
time = None
if len ( state ) == 5 :
time = state [ 4 ]
if time == None :
return select_time ( state , message , sender )
data = {
" start " : state [ 3 ] + " " + state [ 4 ] + " :00 " ,
" customerId " : int ( customer ) ,
" providerId " : int ( provider ) ,
" serviceId " : int ( service )
2022-05-20 06:35:33 -04:00
}
2022-09-28 11:02:35 -04:00
result = api . appointments . register_appointment ( data )
if result != False :
state = [ ]
api . update_user_state ( sender , state )
return { ' response ' : ' Successfully registered appointment! ' }
else :
state = [ ]
api . update_user_state ( sender , state )
print ( result )
return { ' response ' : " Error: please try again " }
return { ' response ' : " Error: please try again " }
2022-05-20 06:35:33 -04:00
2022-09-28 11:02:35 -04:00
def services ( state , message , sender ) :
return {
' response ' : api . list_services ( )
}
2022-05-20 06:35:33 -04:00
2022-03-03 14:18:07 -05:00
commands = {
2022-05-20 06:35:33 -04:00
" nevermind " : nevermind ,
" !faq " : get_faq ,
" !add question " : add_question ,
" !remove question " : remove_question ,
" !update question " : update_question ,
" !update header " : update_header ,
" !remove header " : remove_header ,
" !update footer " : update_footer ,
" !remove footer " : remove_footer ,
" !add admin " : add_admin ,
" !remove admin " : remove_admin ,
2022-09-28 11:02:35 -04:00
" !meetings " : meetings ,
" !register " : register_customer ,
" !register2 " : register_customer ,
" !book " : booking ,
" !services " : services
2022-03-03 14:18:07 -05:00
2022-05-20 06:35:33 -04:00
}
2022-03-03 14:18:07 -05:00
if __name__ == ' __main__ ' :
2022-05-20 06:35:33 -04:00
user = " admin "
2022-03-03 14:18:07 -05:00
msg = " "
2022-05-20 06:35:33 -04:00
debug = True
2022-03-03 14:18:07 -05:00
while msg != " exit " :
msg = input ( " user: " )
2022-05-20 06:35:33 -04:00
print ( " bot: " + handle_command ( user , msg ) [ ' response ' ] )
# print("state:",user)
2022-09-28 11:02:35 -04:00