nerve/commands.py
2022-08-09 15:39:51 -04:00

237 lines
5.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import random
import features
api = features.API()
debug = False
def handle_command(sender, message):
state = api.get_user_state(sender)
for command in commands:
if message.find(command) == 0:
result = commands[command](state, message, sender)
return result
if len(state) > 0:
if debug == 1:
return commands[state[0]](state, message, sender)
try:
result = commands[state[0]](state, message, sender)
return result
except Exception as e:
return {
"response": f"Error⚠"
}
return {
"response": "",
}
def get_faq_string():
return api.get_faq_string()
def nevermind(state, message, sender):
api.update_user_state(sender, [])
return {
"response": "starting over."
}
# faq
def get_faq(state, message, sender):
if len(state) == 0:
return {
"response": api.get_faq_string()
}
return {
"response": api.get_faq_string()
}
def add_question(state, message, sender):
if len(state) == 0:
api.update_user_state(sender, ["!add question"])
return {
'response': "Please enter the question title."
}
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, [])
return {
'response': 'Question added.'
}
elif message == 'restart':
api.update_user_state(sender, ['!add question'])
return {
'response': 'Please enter the question title.'
}
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"
}
return {
'response': None
}
def update_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,"!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,
}
def meetings(state,message,sender):
return {
'response':api.appointments.list_upcoming_appointments(sender)
}
def appointments():
return {
"response": None,
state: []
}
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,
"meetings":meetings
}
if __name__ == '__main__':
user = "admin"
msg = ""
debug = True
while msg != "exit":
msg = input("user:")
print("bot:"+handle_command(user, msg)['response'])
# print("state:",user)