Skip to content

Commit fa140ac

Browse files
authored
Merge pull request #16 from linuxdaemon/master+conversation-objects
Add Conversation objects and clean up error handling
2 parents 0c1e827 + 3a67b5f commit fa140ac

File tree

4 files changed

+77
-46
lines changed

4 files changed

+77
-46
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ You can also clone this repository and import the library directly.
1818
>>> cw.say("Hello CleverBot.")
1919
"Hello Human."
2020
>>> cw.reset() # resets the conversation ID and conversation state.
21+
>>> conv = cw.new_conversation() # Start a new conversation with CleverBot
22+
>>> conv.say("Hellon there.")
23+
"Hello Human."
24+
>>> conv.reset() # Reset the conversation state
2125
```
2226

2327
# License

cleverwrap/cleverwrap.py

+30-46
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515

1616
import requests
1717

18+
from cleverwrap.conversation import Conversation
19+
20+
1821
class CleverWrap:
1922
""" A simple wrapper class for the www.cleverbot.com api. """
2023

21-
url = "https://www.cleverbot.com/getreply"
22-
23-
def __init__(self, api_key, name="CleverBot"):
24+
def __init__(self, api_key, name="CleverBot", url="https://www.cleverbot.com/getreply"):
2425
""" Initialize the class with an api key and optional name
25-
:type name: string
26-
:type api_key: string
27-
:type history: dict or maybe a list
28-
:type convo_id: string
29-
:type cs: string
30-
:type count: int
31-
:type time_elapsed: int
32-
:type time_taken: int
33-
:type output: string
26+
:type api_key: str
27+
:type name: str
3428
"""
35-
self.name = name
3629
self.key = api_key
37-
self.history = {}
38-
self.convo_id = ""
39-
self.cs = ""
40-
self.count = 0
41-
self.time_elapsed = 0
42-
self.time_taken = 0
43-
self.output = ""
30+
self.name = name
31+
self.url = url
32+
self._default_conversation = None
33+
34+
def new_conversation(self):
35+
return Conversation(self)
36+
37+
@property
38+
def default_conversation(self):
39+
if self._default_conversation is None:
40+
self._default_conversation = self.new_conversation()
41+
42+
return self._default_conversation
4443

4544
def say(self, text):
4645
"""
@@ -49,48 +48,33 @@ def say(self, text):
4948
Returns: string
5049
"""
5150

52-
params = {
53-
"input": text,
54-
"key": self.key,
55-
"cs": self.cs,
56-
"conversation_id": self.convo_id,
57-
"wrapper": "CleverWrap.py"
58-
}
59-
60-
reply = self._send(params)
61-
self._process_reply(reply)
62-
return self.output
63-
51+
return self.default_conversation.say(text)
6452

6553
def _send(self, params):
6654
"""
6755
Make the request to www.cleverbot.com
6856
:type params: dict
6957
Returns: dict
7058
"""
59+
params.update(
60+
key=self.key,
61+
wrapper="CleverWrap.py",
62+
)
63+
7164
# Get a response
7265
try:
7366
r = requests.get(self.url, params=params)
74-
# catch errors, print then exit.
67+
r.raise_for_status()
7568
except requests.exceptions.RequestException as e:
69+
# catch errors, print then exit.
7670
print(e)
77-
return r.json(strict=False) # Ignore possible control codes in returned data
78-
71+
raise # Propagate the exception up the call stack so the calling code can catch it
7972

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

9075
def reset(self):
9176
"""
9277
Drop values for self.cs and self.conversation_id
9378
this will start a new conversation with the bot.
9479
"""
95-
self.cs = ""
96-
self.convo_id = ""
80+
return self.default_conversation.reset()

cleverwrap/conversation.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from cleverwrap.response import Response
2+
3+
4+
class Conversation:
5+
def __init__(self, api):
6+
self.api = api
7+
self.cs = ""
8+
self.convo_id = ""
9+
10+
def say(self, text):
11+
"""
12+
Say something to www.cleverbot.com
13+
:type text: string
14+
Returns: string
15+
"""
16+
17+
params = {
18+
"input": text,
19+
"cs": self.cs,
20+
"conversation_id": self.convo_id,
21+
}
22+
23+
reply = Response(self.api._send(params))
24+
self.cs = reply.cs
25+
self.convo_id = reply.convo_id
26+
return reply.output
27+
28+
def reset(self):
29+
"""
30+
Drop values for self.cs and self.conversation_id
31+
this will start a new conversation with the bot.
32+
"""
33+
self.cs = ""
34+
self.convo_id = ""

cleverwrap/response.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Response:
2+
def __init__(self, reply):
3+
self.cs = reply.get("cs", None)
4+
self.count = int(reply.get("interaction_count", None))
5+
self.output = reply.get("output", None)
6+
self.convo_id = reply.get("conversation_id", None)
7+
self.history = {key: value for key, value in reply.items() if key.startswith("interaction")}
8+
self.time_taken = int(reply.get("time_taken", None))
9+
self.time_elapsed = int(reply.get("time_elapsed", None))

0 commit comments

Comments
 (0)