nerve/commands.py

238 lines
5.8 KiB
Python
Raw Normal View History

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
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:
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:
if debug == 1:
return commands[state[0]](state, message, sender)
2022-03-07 04:24:40 -05:00
try:
result = commands[state[0]](state, message, sender)
2022-03-07 04:24:40 -05:00
return result
except Exception as e:
return {
"response": f"Error⚠"
2022-03-07 04:24:40 -05:00
}
2022-03-07 04:24:40 -05:00
return {
"response": "",
2022-03-07 04:24:40 -05:00
}
2022-03-03 14:18:07 -05: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 {
"response": "starting over."
2022-03-03 14:18:07 -05:00
}
# faq
def get_faq(state, message, sender):
2022-03-03 14:18:07 -05:00
if len(state) == 0:
return {
"response": api.get_faq_string()
2022-03-03 14:18:07 -05:00
}
return {
"response": api.get_faq_string()
2022-03-03 14:18:07 -05:00
}
def add_question(state, message, sender):
2022-03-03 14:18:07 -05:00
if len(state) == 0:
api.update_user_state(sender, ["!add question"])
2022-03-03 14:18:07 -05:00
return {
'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:
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---\nIf 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 {
'response': 'Question added.'
2022-03-03 14:18:07 -05:00
}
elif message == 'restart':
api.update_user_state(sender, ['!add question'])
2022-03-03 14:18:07 -05:00
return {
'response': 'Please enter the question title.'
2022-03-03 14:18:07 -05:00
}
else:
return {
'response': f"Your question is:\n# {state[1]}\n{state[2]}\n\nIf 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-03-07 04:24:40 -05:00
return {
'response': None
2022-03-07 04:24:40 -05:00
}
2022-03-03 14:18:07 -05: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 {
'response': "No questions registered."
2022-05-18 03:59:32 -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)
}
def appointments():
return {
"response": None,
state: []
}
2022-03-03 14:18:07 -05:00
commands = {
"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-03-03 14:18:07 -05:00
if __name__ == '__main__':
user = "admin"
2022-03-03 14:18:07 -05:00
msg = ""
debug = True
2022-03-03 14:18:07 -05:00
while msg != "exit":
msg = input("user:")
print("bot:"+handle_command(user, msg)['response'])
# print("state:",user)