Skip to content

Commit

Permalink
Merge pull request #16 from linuxdaemon/master+conversation-objects
Browse files Browse the repository at this point in the history
Add Conversation objects and clean up error handling
  • Loading branch information
edwardslabs authored Aug 23, 2019
2 parents 0c1e827 + 3a67b5f commit fa140ac
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 46 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ You can also clone this repository and import the library directly.
>>> cw.say("Hello CleverBot.")
"Hello Human."
>>> cw.reset() # resets the conversation ID and conversation state.
>>> conv = cw.new_conversation() # Start a new conversation with CleverBot
>>> conv.say("Hellon there.")
"Hello Human."
>>> conv.reset() # Reset the conversation state
```

# License
Expand Down
76 changes: 30 additions & 46 deletions cleverwrap/cleverwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@

import requests

from cleverwrap.conversation import Conversation


class CleverWrap:
""" A simple wrapper class for the www.cleverbot.com api. """

url = "https://www.cleverbot.com/getreply"

def __init__(self, api_key, name="CleverBot"):
def __init__(self, api_key, name="CleverBot", url="https://www.cleverbot.com/getreply"):
""" Initialize the class with an api key and optional name
:type name: string
:type api_key: string
:type history: dict or maybe a list
:type convo_id: string
:type cs: string
:type count: int
:type time_elapsed: int
:type time_taken: int
:type output: string
:type api_key: str
:type name: str
"""
self.name = name
self.key = api_key
self.history = {}
self.convo_id = ""
self.cs = ""
self.count = 0
self.time_elapsed = 0
self.time_taken = 0
self.output = ""
self.name = name
self.url = url
self._default_conversation = None

def new_conversation(self):
return Conversation(self)

@property
def default_conversation(self):
if self._default_conversation is None:
self._default_conversation = self.new_conversation()

return self._default_conversation

def say(self, text):
"""
Expand All @@ -49,48 +48,33 @@ def say(self, text):
Returns: string
"""

params = {
"input": text,
"key": self.key,
"cs": self.cs,
"conversation_id": self.convo_id,
"wrapper": "CleverWrap.py"
}

reply = self._send(params)
self._process_reply(reply)
return self.output

return self.default_conversation.say(text)

def _send(self, params):
"""
Make the request to www.cleverbot.com
:type params: dict
Returns: dict
"""
params.update(
key=self.key,
wrapper="CleverWrap.py",
)

# Get a response
try:
r = requests.get(self.url, params=params)
# catch errors, print then exit.
r.raise_for_status()
except requests.exceptions.RequestException as e:
# catch errors, print then exit.
print(e)
return r.json(strict=False) # Ignore possible control codes in returned data

raise # Propagate the exception up the call stack so the calling code can catch it

def _process_reply(self, reply):
""" take the cleverbot.com response and populate properties. """
self.cs = reply.get("cs", None)
self.count = int(reply.get("interaction_count", None))
self.output = reply.get("output", None)
self.convo_id = reply.get("conversation_id", None)
self.history = {key:value for key, value in reply.items() if key.startswith("interaction")}
self.time_taken = int(reply.get("time_taken", None))
self.time_elapsed = int(reply.get("time_elapsed", None))
return r.json(strict=False) # Ignore possible control codes in returned data

def reset(self):
"""
Drop values for self.cs and self.conversation_id
this will start a new conversation with the bot.
"""
self.cs = ""
self.convo_id = ""
return self.default_conversation.reset()
34 changes: 34 additions & 0 deletions cleverwrap/conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from cleverwrap.response import Response


class Conversation:
def __init__(self, api):
self.api = api
self.cs = ""
self.convo_id = ""

def say(self, text):
"""
Say something to www.cleverbot.com
:type text: string
Returns: string
"""

params = {
"input": text,
"cs": self.cs,
"conversation_id": self.convo_id,
}

reply = Response(self.api._send(params))
self.cs = reply.cs
self.convo_id = reply.convo_id
return reply.output

def reset(self):
"""
Drop values for self.cs and self.conversation_id
this will start a new conversation with the bot.
"""
self.cs = ""
self.convo_id = ""
9 changes: 9 additions & 0 deletions cleverwrap/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Response:
def __init__(self, reply):
self.cs = reply.get("cs", None)
self.count = int(reply.get("interaction_count", None))
self.output = reply.get("output", None)
self.convo_id = reply.get("conversation_id", None)
self.history = {key: value for key, value in reply.items() if key.startswith("interaction")}
self.time_taken = int(reply.get("time_taken", None))
self.time_elapsed = int(reply.get("time_elapsed", None))

0 comments on commit fa140ac

Please sign in to comment.