I’m trying out the example in the documentation. The example is a python script fetching the CORE API getResponse endpoint:
# Importing a couple of libraries
import base64
import json
import requests
# This function will be useful for passing the request
def convert(l):
"""This little function will help us convert
the list of dictionaries to the required format"""
o = ""
for each in l:
o += json.dumps(each) + ";"
return o
# Use your character id here
CHAR_ID = "************************************"
# You should set your Convai API key here
CONVAI_KEY = "************************************"
# This is the URL which will generate actions and responses
action_url = "https://api.convai.com/character/getResponse"
# URL for updating the backstory
url = "https://api.convai.com/character/update"
userText = input("Enter your command: ")
characters = [{"name": "User", "bio": "It's the guy using the Action API."},
{"name": "Action-API-Doc-Bot", "bio": "Helping create docs here at Convai."},]
actions = "Play, Serve, Give, Call Cab"
objects = [{"name": "Mojito (Can be: Served|Cannot be: Played, Given)","description": "For once, a VIRGIN Mojito."},
{"name": "Tequilla Sunrise (Can be: Served|Cannot be: Played, Given)","description": "Not my personal favourite."},
{"name": "White Russian (Can be: Served|Cannot be: Played, Given)","description": "The Dude's favourite drink."},
{"name": "A Jukebox (Can be: Played|Cannot be: Served, Given)","description": "A jukebox with lots of songs."},
{"name": "Freshly sliced oranges (Can be: Served|Cannot be: Played, Given)","description": "Some zesty oranges to freshen you up."},
{"name": "Fish and Chips (Can be: Served|Cannot be: Played, Given)","description": "Good old fish n' chips."},
{"name": "Some Samosas (Can be: Served|Cannot be: Played, Given)","description": "A tasty Indian snack."},
{"name": "Book (Can be: Given|Cannot be: Played, Served)","description": "A book to pass your time as you enjoy your meal."},
]
payload={'userText': userText,
'charID': CHAR_ID,
'sessionID': '-1',
'voiceResponse': 'false',
'actions': actions,
'classification':'multistep',
'objects': convert(objects),
'characters': convert(characters),
}
files = []
headers = {
'CONVAI-API-KEY': CONVAI_KEY
}
response = requests.request("POST", action_url, headers=headers, data=payload, files=files)
print(response.text)
response = json.loads(response.text)
print(json.dumps(response, indent=4))
The API response is not a JSON, but a text that says “Get Response Failed An unexpected error occurred: string indices must be integers”