diff --git a/README.md b/README.md index 4888fd1..58f1c9a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cleverwrap/cleverwrap.py b/cleverwrap/cleverwrap.py index 25612c1..f13437b 100644 --- a/cleverwrap/cleverwrap.py +++ b/cleverwrap/cleverwrap.py @@ -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): """ @@ -49,18 +48,7 @@ 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): """ @@ -68,29 +56,25 @@ def _send(self, params): :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() diff --git a/cleverwrap/conversation.py b/cleverwrap/conversation.py new file mode 100644 index 0000000..ec61aaf --- /dev/null +++ b/cleverwrap/conversation.py @@ -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 = "" diff --git a/cleverwrap/response.py b/cleverwrap/response.py new file mode 100644 index 0000000..0ca4336 --- /dev/null +++ b/cleverwrap/response.py @@ -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))