I have a facebook messenger chatbot I programmed in python. There are a few dozen commands it handles, mostly via a ton of if/then statements as follows
if state == "state1":
if "command1" in message:
(do stuff)
elif "command2" in message:
(do other stuff)
state = "waiting_command2_resp"
elif ...
elif state == "waiting_command2_resp":
if message == "response1":
(stuff)
elif message == "response2":
(you get the idea)
elif...
You get the idea. As the capabilities of the bot have grown and become more complex, the code has become ridiculously confusing to navigate.
I'm looking for a way to refactor the code so it's cleaner. I can potentially make each action a method, but the mass of if/else statements will remain to determine which method to call
What's a good, maintainable way of assigning a string to each method, and running the appropriate method where its assigned string matches the message string?
I know Flask runs methods in response to URL path strings above the method definition. For example:
@app.route('/')
def index():
do stuff
@app.route('/endpoint')
def method():
do other stuff
Is there any way I can potentially do this via similar behavior?
Edit: This is not a duplicate of the question it's marked as a duplicate of. That question is about if you have a string with the exact name of the function. That is not the case here