Skip to main content

cleverbot.py

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 normally from PyPI with pip:

pip install cleverbot.py

Or install it with the asynchronous dependencies (Python 3.4.2+ only):

pip install cleverbot.py[async]

Requirements:

Dependencies:

  • requests 1.0.0+

  • Asynchronous:

    • aiohttp 1.0.0+

Usage

First import the package:

import cleverbot

If you have the asynchronous dependencies and want to use Cleverbot asynchronously import it as below instead:

from cleverbot import async_ as cleverbot

Then 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 a loop keyword argument


You can now start talking to Cleverbot.

Talk straight to Cleverbot:

reply = cb.say("Hello")

You can pass in keyword arguments to Cleverbot.say such as cs to change the conversation, vtext to change the current conversation’s history, or even tweak as an alias for cb_settings_tweak to change Cleverbot’s mood. Read the “Parameters” section of the official Cleverbot API docs for 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 including key. 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 turn Cleverbot.conversations into 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.say and Conversation.say are coroutines if you’re running asynchronously.


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 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 the HTTP status from the error like so:

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

This is similar for Timeout where you can get the defined timeout value with Timeout.timeout.

Additionally, all Cleverbot errors subclass CleverbotError so 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 the Cleverbot.data or Conversation.data dictionary:

cb.conversation_id == cb.data['conversation_id']
convo.conversation_id == convo.data['conversation_id']

Note that every attribute except for Cleverbot.cs and Conversation.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 in the 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 use Conversation.reset instead:

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 use Cleverbot.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 use load:

cb = cleverbot.load('cleverbot.pickle')

To only load the data and conversations use Cleverbot.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.close is a coroutine if you’re using Cleverbot asynchronously.

Metadata

Release files for cleverbot.py 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cleverbot.py 2.5.0
File Size Uploaded
cleverbot.py-2.5.0.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cleverbot.py 2.5.0
File Interpreter ABI Platform
cleverbot.py-2.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 29.4 kB

Release files / cleverbot.py-2.5.0.tar.gz

Download URL cleverbot.py-2.5.0.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
df5fb36891d69c0a46d406bab09999008ac6c85f16f152bba6e77f47da8eb61d
BLAKE2b-256 checksum
How to use checksums
09c242c2f45f9347441c086d3f30108954027cbc43f50410dc0b19e7a2ae8843
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

Release files / cleverbot.py-2.5.0-py2.py3-none-any.whl

Download URL cleverbot.py-2.5.0-py2.py3-none-any.whl
Size 15.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
46ca278e05bef5e071a1f11a8acc2ae7c5d8e0e6acbe4529a8476e84928aa6e0
BLAKE2b-256 checksum
How to use checksums
8cd2b3aa06c165b61ed98e86c617e9b2076dfdb8d0eb5daa3e9423a966911f36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.5

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page