Skip to content
Permalink
Browse files

Fixed a lot of little bugs, finished the fun fact function

- when user doesn't answer for three times, now fixed

- User can now ask for a fun fact

-Added comments
  • Loading branch information
mateussa committed Nov 23, 2017
1 parent 3b3b3bb commit 81b9394d0cf5361290efdc4173986d049d4af88e
Showing with 81 additions and 48 deletions.
  1. +12 −12 DataAPI.py
  2. +67 −34 Server.py
  3. +2 −2 data/Birthdays.json
@@ -173,7 +173,7 @@ def getBirthday(): # Andre
import random

# Get JSON data from GitHub file
birthdaysJson = readJSON("https://github.coventry.ac.uk/raw/hortonr6/chatbot-jeff/master/data/Birthdays.json?token=AAAIWA9gX7wN-fXNV6l2E7WBYQwV7a1Fks5aFgGzwA%3D%3D")
birthdaysJson = readJSON("http://andrefmsilva.coventry.domains/ALL%20Project%20One%20-%20Chatbot%20Data/Birthdays.json")
if ("Error" in birthdaysJson):
return(birthdaysJson)

@@ -190,7 +190,7 @@ def getHistory(): # Andre
import random

# Get JSON data from GitHub file
historyJson = readJSON("https://github.coventry.ac.uk/raw/hortonr6/chatbot-jeff/master/data/History.json?token=AAAIWGsqhT6le5g3fQBEHXbKDXuo5lOQks5aFgd3wA%3D%3D")
historyJson = readJSON("http://andrefmsilva.coventry.domains/ALL%20Project%20One%20-%20Chatbot%20Data/History.json")
if ("Error" in historyJson):
return(historyJson)

@@ -207,7 +207,7 @@ def getQuote(): # Andre
import random

# Get JSON data from GitHub file
quoteJson = readJSON("https://github.coventry.ac.uk/raw/hortonr6/chatbot-jeff/master/data/Quotes.json?token=AAAIWNQvyBUwZre3_VZqwp8aZUHHL4umks5aFgdiwA%3D%3D")
quoteJson = readJSON("http://andrefmsilva.coventry.domains/ALL%20Project%20One%20-%20Chatbot%20Data/Quotes.json")
if ("Error" in quoteJson):
return(quoteJson)

@@ -233,21 +233,21 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): # A
questionSet = getQuote()


if ("Error" in questionSet):
if ("Error" in questionSet): # Return every thing as a list to keep the same structure as getting multiple questions
if (questionSet[1] == 0):
return("Error", "There was an error opening the URL")
return(["Error", "There was an error opening the URL"])
elif (questionSet[1] == 1):
return("Error", "Error reading JSON data")
return(["Error", "Error reading JSON data"])
elif (questionSet[1] == 2):
return("Error", "The number of questions requested not valid")
return(["Error", "The number of questions requested not valid"])
elif (questionSet[1] == 3):
return("Error", "Category not valid")
return(["Error", "Category not valid"])
elif (questionSet[1] == 4):
return("Error", "Difficulty not valid")
return(["Error", "Difficulty not valid"])
elif (questionSet[1] == 5):
return("Error", "No results were found")
return(["Error", "No results were found"])
elif (questionSet[1] == 6):
return("Error", "Invalid parameter")
return(["Error", "Invalid parameter"])
elif (questionSet[1] == 7):
import os

@@ -256,7 +256,7 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): # A
os.remove("./data/token.jeff")
questionSet = getOpentDB(category, difficulty, nrQuestions)
if ("Error" in questionSet):
return("Error", "Token not found or all available question were used")
return(["Error", "Token not found or all available question were used"])

return(questionSet)

101 Server.py
@@ -1,7 +1,7 @@
import socket
import time
import random
from DataAPI import getQuestion, getCategories
from DataAPI import getQuestion, getCategories, getBirthday, getHistory, getQuote
from determineUserInput import determineUserInput

def sendMessage(message, EOM = True): # Andre
@@ -26,7 +26,7 @@ def receiveMessage(): # Andre
sendMessage("Sorry, I not able to receive that, plese try again another day.")
exit()

if (message != "END"):
if (message != "END"): # Check if the user want to end the conversation
print ("CLIENT: {}".format(message))

answerUserInput = determineUserInput(message)
@@ -36,18 +36,18 @@ def receiveMessage(): # Andre
return(answerUserInput)

def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
''' With input of the type of answer code expected and a string question,
a list of sentences if not expected answer, and the default fallback
''' With input of the type of answer code expected and a list of messages to send,
another of sentences if not expected answer, and the default fallback
answer returns the answer to the question '''

for i, item in enumerate(sendMessages):
for i, item in enumerate(sendMessages): # Send all the messages to client
sendMessage(item, (True if i == len(sendMessages) - 1 else False))

answer = receiveMessage()
if (type(answer) != tuple):
if (type(answer) != tuple): # If the answer if for exemple "END" just return it
return(answer)

while (answer[1] != answerType or answer[0] == "" and len(noAnswers) > 0):
while ((answer[1] != answerType or answer[0] == "") and len(noAnswers) > 0):

if(answerType == -1):
for cat in sendMessages:
@@ -57,21 +57,21 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
break

if (answer[1] == 2): # If the user asked a question
if (type(answer[0]) == str):
sendMessage(answer[0], False)
else:
if (type(answer[0]) == tuple): # Send two messages, the text and the link of the google search
sendMessage(answer[0][0], False)
sendMessage(answer[0][1], False)
else:
sendMessage(answer[0], False)

sendMessage(noAnswers[0]) # FIX WHEN THE USER DOENS ANSWER FOR 3 TIMES
if (len(noAnswers) > 0):
noAnswers.pop(0)
sendMessage(noAnswers[0])
noAnswers.pop(0)
elif (answer[1] == 5): # If the user doesn't want to answer
sendMessage("You should, it would be more fun!")
noAnswers.pop(len(noAnswers) - 1)
else:
sendMessage("Sorry I didn't undertood that.", False)
sendMessage("Can you repeat please.")
noAnswers.pop(len(noAnswers) - 1)

answer = receiveMessage()

@@ -82,7 +82,8 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
return (answer[0])

def oneQuestion(qType): # Andre
''' Output a set of questions with a category choosen by the user '''
''' Input of type of question as string Output a set of
a question of that type '''
if (qType == "OpentDB"):
cat = getCategories(True, 3)
if ("Error" in cat):
@@ -101,7 +102,7 @@ def oneQuestion(qType): # Andre
sendMessage(questionSet[1], False)
sendMessage("Lets try again.", False)
return(questionSet)

# If question set of Multiple type
if (questionSet["Type"] == "Multiple"):
receivedMessage = askSomething(3, [questionSet["Question"],
@@ -118,36 +119,42 @@ def oneQuestion(qType): # Andre
"B: {}".format(questionSet["B"])],
["You can choose one of the options.",
"Hmmm, I see you are afraid of making a mistake."], "X")

# If question set of Birthday type
elif (questionSet["Type"] == "Birthday"):
receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage

# If question set of History type
elif (questionSet["Type"] == "History"):
receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage

# If question set of Quote type
elif (questionSet["Type"] == "Quote"):
receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage


print(receivedMessage.casefold())
print(questionSet["corrAnswer"].casefold())
if (receivedMessage.casefold() == questionSet["corrAnswer"].casefold()):
if (questionSet[receivedMessage.upper()] == questionSet["corrAnswer"]):
sendMessage("Congratulations! That was the right answer!!", False)
sendMessage("-" * 50, False)
else:
sendMessage("Nice try, but that's not the right answer.", False)
sendMessage("The right answer was {}."
.format(questionSet["corrAnswer"]), False)
sendMessage("The right answer was {}.".format(questionSet["corrAnswer"]), False)
sendMessage("I know you can get the next one!!", False)
sendMessage("-" * 50, False)


def quizChallange(nrQuestions): # Andre
''' With input of a number of questions, output a list of question sets
with the lenght of the choosen input '''
if (nrQuestions < 1 or nrQuestions > 50):
sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.")
return(True)
@@ -177,7 +184,7 @@ def quizChallange(nrQuestions): # Andre

for questionSet in qChaSet:
# If question set of multiple type
if (questionSet["Type"] == "multiple"):
if (questionSet["Type"] == "Multiple"):
receivedMessage = askSomething(3, [questionSet["Question"],
"A: {}".format(questionSet["A"]),
"B: {}".format(questionSet["B"]),
@@ -192,24 +199,44 @@ def quizChallange(nrQuestions): # Andre
"B: {}".format(questionSet["B"])],
["You can choose one of the options.",
"Hmmm, I see you are afraid of making a mistake."], "X")
if (receivedMessage[0].casefold() == questionSet["corrAnswer"].casefold()):

if (questionSet[receivedMessage.upper()] == questionSet["corrAnswer"]):
sendMessage("Congratulations! That was the right answer!!", False)
sendMessage("-" * 50, False)
Score += 1
else:
sendMessage("Nice try, but that's not the right answer.", False)
sendMessage("The right answer was {} ({})."
.format(questionSet["corrAnswer"],
questionSet[questionSet["corrAnswer"]]), False)
sendMessage("The right answer was {}.".format(questionSet["corrAnswer"]), False)
sendMessage("I know you can get the next one!!", False)
sendMessage("-" * 50, False)

return(Score)

def funFacts(): # Andre
''' With no input, output one fun fact '''

fType = random.choice(["Birthday", "History", "Quote"])

if (fType == "Birthday"):
fact = getBirthday()[0]
sendMessage("I know that {} was born in {}.".format(fact["Name"], fact["Date"]), False)

elif (fType == "History"):
fact = getHistory()[0]
sendMessage("I found that '{}' in {}.".format(fact["Event"], fact["Date"]), False)

elif (fType == "Quote"):
fact = getQuote()[0]
sendMessage("I know that {} said '{}'.".format(fact["Name"], fact["Quote"]), False)


#### CONNECTION ####



####################
# #
# CONNECTION #
# #
####################
# Create Socket and bind server to socket
thisSocket = socket.socket()
thisSocket.bind(("127.0.0.1", 5001))
@@ -234,10 +261,10 @@ def quizChallange(nrQuestions): # Andre

sendMessage("YOURNAMEWILLBE " + clientName, False)

message = askSomething(0, ["So, {}, I will teach you something today!"
.format(clientName.title()), "Would you like a challenge or have any questions?"],
["You can ask for a Question challenge",
"If you want you could go for a Quiz Challenge!"], None)
message = askSomething(0, ["So, {}, I will teach you something today!".format(clientName.title()),
"Would you like a 'challenge' or maybe just a 'fun fact'?"],
["You can ask for a 'Question challenge' or even a 'fun fact'.",
"If you want you could go for a 'Quiz Challenge'!"], None)

while True:
if (type(message) == str and "question challange".casefold() in message.casefold()):
@@ -258,6 +285,9 @@ def quizChallange(nrQuestions): # Andre

elif (type(message) == str and "quote challange".casefold() in message.casefold()):
oneQuestion("Quote")

elif (type(message) == str and "fun fact".casefold() in message.casefold()):
funFacts()

elif (type(message) == str and "challange".casefold() in message.casefold()):
message = askSomething(0, ["You can ask for a question, quiz, history, birthday or quote challange."],
@@ -268,9 +298,12 @@ def quizChallange(nrQuestions): # Andre
break


message = askSomething(0, ["If you want you can ask anything else!"],
["You can ask me anything else, I will try to help you."], None)
message = askSomething(0, ["If you want you can ask anything!"],
["You can ask me anything, I will try to help you."], None)



sendMessage("See you next time! Bye!")
conn.close()
thisSocket.close
exit()
@@ -18797,15 +18797,15 @@
},
{
"Date": "1925-11-20",
"Name": "Robert Kennedy, in Brookline, Mass D-Sen-NY AG; assassinated"
"Name": "Robert Kennedy, in Brookline, Mass D-Sen-NY AG"
},
{
"Date": "1926-11-20",
"Name": "Kaye Ballard, in Ohio Actress-comedian"
},
{
"Date": "1927-11-20",
"Name": "Estelle Parsons, Actress Rachel, Rachel, Bonnie and Clyde"
"Name": "Estelle Parsons, Actress Rachel, Bonnie and Clyde"
},
{
"Date": "1932-11-20",

1 comment on commit 81b9394

@mateussa

This comment has been minimized.

Copy link
Collaborator Author

@mateussa mateussa commented on 81b9394 Nov 23, 2017

Also changed files from GitHub to my portfolio website, GitHub uses keys like tokens to access raw files that change over time and then it breaks the code.

Please sign in to comment.
You can’t perform that action at this time.