experimenting with state
This commit is contained in:
parent
481d510c07
commit
baaee8a5f6
4 changed files with 160 additions and 1 deletions
155
commands.py
155
commands.py
|
@ -0,0 +1,155 @@
|
|||
import random
|
||||
|
||||
user = []
|
||||
jokes = [
|
||||
["Testing","Testing you!"],
|
||||
["singing pokemon","Jiggalypuff! 🎙️"]
|
||||
]
|
||||
#jokes = []
|
||||
|
||||
def handle_command(state,message):
|
||||
for command in commands:
|
||||
if command in message:
|
||||
result = commands[command](state,message)
|
||||
print("Bot: "+result["response"])
|
||||
return result["state"]
|
||||
else:
|
||||
if len(user) > 0:
|
||||
try:
|
||||
result = commands[user[0]](state,message)
|
||||
print("Bot: "+result["response"])
|
||||
return result["state"]
|
||||
except:
|
||||
print(e)
|
||||
print("Error saving state:",message)
|
||||
pass
|
||||
return state
|
||||
|
||||
"""
|
||||
Knock-knock implimentation
|
||||
|
||||
when you receive message, read state.
|
||||
user: knock knock
|
||||
bot: who's there? -> bot responds and registers state
|
||||
(user.state=[knock-knock])
|
||||
user: {statement}
|
||||
bot: {statement} who? -> bot responds and stores statement
|
||||
(user.stage=[knock-knock,{statement}])
|
||||
user: {punchline}
|
||||
bot: Nice one! -> bot saves knock-knock joke
|
||||
"""
|
||||
|
||||
def nevermind(state,message):
|
||||
return {
|
||||
"response":"starting over.",
|
||||
"state":[]
|
||||
}
|
||||
|
||||
def knock_knock(state,message):
|
||||
#return {response:"",state:""}
|
||||
try:
|
||||
if state[0] != "knock knock":
|
||||
#clear the state
|
||||
state = []
|
||||
except:
|
||||
pass
|
||||
if len(state) == 0:
|
||||
state.append("knock knock")
|
||||
return {
|
||||
"response":"Who's there?",
|
||||
"state": state
|
||||
}
|
||||
if len(state) == 1:
|
||||
state.append(message)
|
||||
return {
|
||||
"response":message + " who?",
|
||||
"state":state
|
||||
}
|
||||
if len(state) == 2:
|
||||
state.append(message)
|
||||
jokes.append([
|
||||
state[1],state[2]
|
||||
])
|
||||
print("joke registered.")
|
||||
return {
|
||||
"response":"Hah! very funny!",
|
||||
"state":[]
|
||||
}
|
||||
|
||||
return {
|
||||
"response":"Cleared.",
|
||||
"state":[]
|
||||
}
|
||||
|
||||
"""'joke' command
|
||||
-------------
|
||||
user:!joke
|
||||
bot:Knock knock -> bot initializes state
|
||||
(user.state=[joke])
|
||||
user: Who's there?
|
||||
bot: {statement} -> bot responds with chosen joke
|
||||
(user.state=[joke,chosenjoke])
|
||||
user: {statement} who?
|
||||
bot:
|
||||
|
||||
"""
|
||||
def joke(state,message):
|
||||
if len(jokes) == 0:
|
||||
return {
|
||||
"response":"I don't know any jokes...",
|
||||
"state":[]
|
||||
}
|
||||
try:
|
||||
if state[0] != "joke":
|
||||
state = []
|
||||
except:
|
||||
pass
|
||||
|
||||
if len(state) == 0:
|
||||
return {
|
||||
"response":"Knock knock",
|
||||
"state":["joke"]
|
||||
}
|
||||
if len(state) == 1:
|
||||
if message.find("there?") > 0 :
|
||||
#get random joke index
|
||||
state.append(random.randint(0,len(jokes)-1))
|
||||
return {
|
||||
"response":jokes[state[1]][0],
|
||||
"state":state
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"response":"ask, \"who's there?\"",
|
||||
"state":state
|
||||
}
|
||||
if len(state) == 2:
|
||||
if message.find("who?") > 0:
|
||||
return {
|
||||
"response":jokes[state[1]][1],
|
||||
"state":[]
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"response":"ask, \""+ jokes[state[1]][0] +" who?\"",
|
||||
"state":state
|
||||
}
|
||||
|
||||
return state
|
||||
|
||||
|
||||
commands = {
|
||||
"nevermind":nevermind,
|
||||
"knock knock":knock_knock,
|
||||
"joke":joke
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
msg = ""
|
||||
while msg != "exit":
|
||||
msg = input("user:")
|
||||
user = handle_command(user,msg)
|
||||
#print("state:",user)
|
0
faq.py
Normal file
0
faq.py
Normal file
1
knockknock.py
Normal file
1
knockknock.py
Normal file
|
@ -0,0 +1 @@
|
|||
print("knock-knock functionality loaded")
|
5
main.py
5
main.py
|
@ -1,6 +1,7 @@
|
|||
import json
|
||||
import nio
|
||||
import simplematrixbotlib as botlib
|
||||
import commands
|
||||
|
||||
data = json.loads(open('data.json').read())
|
||||
dfaq = data["faq"]
|
||||
|
@ -20,7 +21,6 @@ def load_faq():
|
|||
dfaq = json.loads(open('data.json').read())["faq"]
|
||||
return dfaq["header"] + "".join(["\n"+q["question"]+"\n"+q["answer"] for q in dfaq["questions"]]) + "\n\n"+dfaq["footer"]
|
||||
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def faq(room, message):
|
||||
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||||
|
@ -59,5 +59,8 @@ async def faqresponse(room, message):
|
|||
response + dfaq["footer"]
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bot.run()
|
||||
|
|
Loading…
Reference in a new issue