Package: cleverbot.py
cleverbot.py is a Cleverbot API wrapper for Python made to be both
fully-featured and easy to use.Exampleimportcleverbotcb=cleverbot.Cleverbot('YOUR_API_KEY',timeout=60)text=input("Say to Cleverbot: ")try:reply=cb.say(text)exceptcleverbot.CleverbotErroraserror:print(error)else:print(reply)finally:cb.close()InstallingInstall it normally fromPyPIwith
pip:pip install cleverbot.pyOr install it with the asynchronous dependencies (Python 3.4.2+ only):pip install cleverbot.py[async]Requirements:Python 3.2+ or 2.7A Cleverbot API keyDependencies:requests 1.0.0+Asynchronous:aiohttp 1.0.0+UsageFirst import the package:importcleverbotIf you have the asynchronous dependencies and want to use Cleverbot
asynchronously import it as below instead:fromcleverbotimportasync_ascleverbotThen initialize Cleverbot with your API key and optionally a cleverbot state,
timeout and or tweak if you want to adjust Cleverbot’s mood:cb=cleverbot.Cleverbot('YOUR_API_KEY',cs='76nxdxIJ02AAA',timeout=60,tweak1=0,tweak2=100,tweak3=100)The cleverbot state is the encoded state of the conversation that you get from
talking to Cleverbot and includes the whole conversation history.If you’re using Cleverbot asynchronously you can also give an event loop to
Cleverbot with aloopkeyword argumentYou can now start talking to Cleverbot.Talk straight to Cleverbot:reply=cb.say("Hello")You can pass in keyword arguments toCleverbot.saysuch ascsto change
the conversation,vtextto change the current conversation’s history, or
eventweakas an alias forcb_settings_tweakto change Cleverbot’s
mood. Read the “Parameters” section ofthe official Cleverbot API docsfor more information.Alternatively, start a new conversation and talk from it:convo=cb.conversation()reply=convo.say("Hello")Conversations are like mini Cleverbots so you can pass in anything that
Cleverbot takes as keyword arguments includingkey. The values you don’t
pass in excluding the cleverbot state will be taken from the originating
Cleverbot.If you want to manage your conversations more easily you can pass in a name as
the first argument to every conversation you create which will turnCleverbot.conversationsinto a dictionary with the name as the key and the
conversation as the value. Trying to mix both named and nameless conversations
will result in an error.Cleverbot.sayandConversation.sayare coroutines if you’re running
asynchronously.If something goes wrong with the request such as an invalid API key anAPIErrorwill be raised containing the error message or if you’ve defined
a timeout and don’t get a reply within the defined amount of seconds you’ll
get aTimeout.As an example:cleverbot.errors.APIError: Missing or invalid API key or POST request, please visit www.cleverbot.com/apiYou can get the error message and the HTTP status from the error
like so:try:cb.say("Hello")exceptcleverbot.APIErroraserror:print(error.error,error.status)This is similar forTimeoutwhere you can get the defined timeout
value withTimeout.timeout.Additionally, all Cleverbot errors subclassCleverbotErrorso you can use
it to catch every Cleverbot related error.To access the data gained from talking straight to Cleverbot or from talking in
a conversation you can either get it from an attribute or directly get it from
theCleverbot.dataorConversation.datadictionary:cb.conversation_id==cb.data['conversation_id']convo.conversation_id==convo.data['conversation_id']Note that every attribute except forCleverbot.csandConversation.cs(i.e. the cleverbot state) is read-only and will get shadowed if you set it to
something.For a list of all of the data and their descriptions go to the “JSON Reply”
section inthe official Cleverbot API docs.To reset Cleverbot’s and all of its conversations’ data you can simply do the
following:cb.reset()To only reset a single conversation’s data useConversation.resetinstead:convo.reset()Resetting won’t delete any conversations so you’ll be able to reuse them.If you want to save the current state of Cleverbot and all of its conversations
you can useCleverbot.save:cb.save('cleverbot.pickle')This saves the key, timeout and tweaks you’ve given to Cleverbot and its
conversations and also the data of each including the cleverbot state.In order to load and recreate the previously saved state as a new Cleverbot
instance useload:cb=cleverbot.load('cleverbot.pickle')To only load the data and conversations useCleverbot.load:cb.load('cleverbot.pickle')Loading conversations will delete the old ones.When you’re done with the current instance of Cleverbot, close Cleverbot’s
connection to the API:cb.close()Cleverbot.closeis a coroutine if you’re using Cleverbot asynchronously.