1515
1616import requests
1717
18+ from cleverwrap .conversation import Conversation
19+
20+
1821class 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 ()
0 commit comments