Skip to main content

A Cleverbot API wrapper for Python with asynchronous functionality.

Project description

cleverbot.py is a Cleverbot API wrapper for Python made to be both fully-featured and easy to use.

Example

import cleverbot


cb = cleverbot.Cleverbot('YOUR_API_KEY', timeout=60)

text = input("Say to Cleverbot: ")
try:
    reply = cb.say(text)
except cleverbot.CleverbotError as error:
    print(error)
else:
    print(reply)
finally:
    cb.close()

Installing

Install it from PyPI with pip:

pip install cleverbot.py

Or install it from GitHub using git:

git clone https://github.com/orlnub123/cleverbot.py
cd cleverbot.py
python setup.py install

If you don’t have pip or git you can also download the source and run python setup.py install on it.

To install Cleverbot with asynchronous support you’ll have to use pip and be on Python 3.4.2+.

pip install cleverbot.py[async]

This is not required if you already have aiohttp 1.0.0 or later.

Requirements:

Dependencies:

  • requests 1.0.0+

  • aiohttp 1.0.0+ (Optional, for asynchronous support)

Usage

First import the package:

import cleverbot

Then initialize Cleverbot with your API key and optionally a cleverbot state and or timeout:

cb = cleverbot.Cleverbot('YOUR_API_KEY', cs='76nxdxIJ02AAA', timeout=60)

The cleverbot state is the encoded state of the conversation so far and includes the whole conversation history up to that point.


You can now start talking to Cleverbot.

Get the reply from the input:

reply = cb.say("Hello")

Or alternatively get it later:

cb.say("Hello")
reply = cb.output

If you want to talk to Cleverbot asynchronously use asay instead:

await cb.asay("Hello")

asay only works if you’re on Python 3.4.2+ and have aiohttp installed. Experience with asyncio is recommended as you’ll have to run it in an event loop.

A big benefit of using asay is that it allows multiple requests to be sent at once instead of waiting for the previous request to return a response which can take significantly longer.


If something goes wrong with the request, such as an invalid API key, an APIError will be raised containing the error message or, if you’ve defined a timeout and you don’t get a reply within the defined amount of seconds you’ll get a Timeout.

As an example:

cleverbot.errors.APIError: Missing or invalid API key or POST request, please visit www.cleverbot.com/api

You can get the error message and additionally the HTTP status like so:

try:
    cb.say("Hello")
except cleverbot.APIError as error:
    print(error.error, error.status)

This is also applicable to Timeout where you can get the defined timeout value with error.timeout.

Also, all Cleverbot errors subclass CleverbotError so you can use it to catch everything Cleverbot related.


Print out all of the data Cleverbot gained from the previous conversation:

print(cb.data)

Take note of the cs key as we’ll use it to save the conversation in the next section.

To access the data you can either get them from an attribute or directly get them from cb.data:

cb.output == cb.data['output']

However modifying the data with an attribute is only applicable to the cleverbot state.

To get a list of all of the keys’ descriptions either take a look at the _query method’s docstring in cleverbot.py or go to the ‘JSON Reply’ section in the official Cleverbot API docs.


Save the conversation in preparation for a reset:

cs = cb.cs

Reset Cleverbot, deleting all of the data it’s gained from the previous conversations:

cb.reset()

Note that if you try to get the cleverbot state now you’ll get an error:

AttributeError: 'Cleverbot' object has no attribute 'cs'

Now start right where you left off by setting the cleverbot state you saved earlier:

cb.cs = cs

Or by setting it when creating a new Cleverbot instance:

cb = cleverbot.Cleverbot('YOUR_API_KEY', cs=cs)

When you’re all done, close Cleverbot’s connection to the API:

cb.close()

This should only be done when you’re not going to use the current instance of Cleverbot anymore.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cleverbot.py-1.3.0.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

cleverbot.py-1.3.0-py2.py3-none-any.whl (11.8 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page