2022-03-03 14:18:07 -05:00
import random
2022-05-18 03:59:32 -04:00
import features
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 :
return {
2022-05-20 06:35:33 -04:00
" response " : f " ⚠️ Error⚠️ "
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
def appointments ( ) :
return {
" response " : None ,
state : [ ]
}
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-08-09 15:39:51 -04:00
" meetings " : meetings
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)