Skip to content

Commit

Permalink
OOPfied bot logic
Browse files Browse the repository at this point in the history
  • Loading branch information
akohli96 committed Apr 19, 2017
1 parent 87fd62a commit b66437e
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions mhapsite/mhap/bot_helper.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import re
import requests,json


help_patterns = ['help','please help me']

quote_patterns= ['quote','quotes','inspiration']

help_response=['https://www.adaa.org/tips-manage-anxiety-and-stress','http://tinybuddha.com/']



import requests



#http://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list
#http://stackoverflow.com/questions/1859959/python-static-methods-how-to-call-a-method-from-another-method
#http://stackoverflow.com/questions/3434581/accessing-a-class-member-variables-in-python
#http://stackoverflow.com/questions/740287/how-to-check-if-one-of-the-following-items-is-in-a-list
class Bot(object):
'Bot Logic in a class'
help_patterns = ['help','please help me']
Expand All @@ -22,27 +11,43 @@ class Bot(object):

help_response=['https://www.adaa.org/tips-manage-anxiety-and-stress','http://tinybuddha.com/']


@staticmethod
def process_message(user_message):
def fetch_quote(API="http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=json"):
try:
quote_json = requests.get(API).text
quote_json = quote_json.replace('\\x', '\\u00')
quote_json = json.loads(quote_json,strict=False)
return (str(quote_json['quoteText']), str(quote_json['quoteAuthor']))
except Exception as e:
print e

return "QUOTE","AUTHOR"



@classmethod
def process_message(cls,user_message):

if type(user_message) is not str:
return "I only accept strings"

splitted_message = user_message.split(" ")
print splitted_message
if any(message in cls.quote_patterns for message in splitted_message):
return cls.fetch_quote()
#return "You want a quote :)"

if any(message in help_patterns for message in splitted_message):
return "You want a quote :)"

elif any(message in quote_patterns for message in splitted_message):
elif any(message in cls.help_patterns for message in splitted_message):
return "You want help :)"
else:
return "I am not a very intelligent bot. Please ask the MHAP team to upgrade me"


for help in help_patterns:
print Bot.process_message(help)



print Bot.process_message("quote")

for quote in quote_patterns:
print Bot.process_message(quote)

print Bot.process_message("Cant interpret this")
#print Bot.process_message("Cant interpret this")

1 comment on commit b66437e

@akohli96
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PYTHON 🔥

Please sign in to comment.