15
15
16
16
import requests
17
17
18
+ from cleverwrap .conversation import Conversation
19
+
20
+
18
21
class CleverWrap :
19
22
""" A simple wrapper class for the www.cleverbot.com api. """
20
23
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" ):
24
25
""" 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
34
28
"""
35
- self .name = name
36
29
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
44
43
45
44
def say (self , text ):
46
45
"""
@@ -49,48 +48,33 @@ def say(self, text):
49
48
Returns: string
50
49
"""
51
50
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 )
64
52
65
53
def _send (self , params ):
66
54
"""
67
55
Make the request to www.cleverbot.com
68
56
:type params: dict
69
57
Returns: dict
70
58
"""
59
+ params .update (
60
+ key = self .key ,
61
+ wrapper = "CleverWrap.py" ,
62
+ )
63
+
71
64
# Get a response
72
65
try :
73
66
r = requests .get (self .url , params = params )
74
- # catch errors, print then exit.
67
+ r . raise_for_status ()
75
68
except requests .exceptions .RequestException as e :
69
+ # catch errors, print then exit.
76
70
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
79
72
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
89
74
90
75
def reset (self ):
91
76
"""
92
77
Drop values for self.cs and self.conversation_id
93
78
this will start a new conversation with the bot.
94
79
"""
95
- self .cs = ""
96
- self .convo_id = ""
80
+ return self .default_conversation .reset ()
0 commit comments