0

Currently I'm having a problem setting up a simple REST service with flask and a chatterbot. You can see the full code here.

The goal is, that the service returns a json with a response from the chatbot to a given request.

The problem is, that when I want to save the response from the chatbot in a dict:

dialog = {
            "id": 1,
            "usersay": request,
            # chatterbot function to get a response from the bot
            "botsay": chatbot.get_response(request)
        }

It will be saved as a chatterbot special Statement Object like which will then look like this:

"botsay": <Statement text:bot response>

When I try to jsonify a dict with this object I get the following error:

TypeError: Can't convert 'Statement' object to str implicitly

I searched online to find a solution but havent found anything helpful. In addition, I'm not experienced with python. What is absolutely unexplainable for me is, when I use

>>> request = "Hi"
>>> print(chatbot.get_response(request))

I will get the correct output

> Hello

I just want to save the plain response in the dict so can I return it as a json to the client.

Could anyone explain the problem?

Thanks in advance!

Robert
  • 159
  • 3
  • 12

1 Answers1

0

Problem solved by simple accessing the "text" attribute of the Statement Object with the . notation (see heere).

>>> response = chatterbot.get_response("Hi")
>>> dialog = { ..., "botsay" = response.text, ... }
>>> print dialog
{ ..., "botsay": "Hello", ...}
Robert
  • 159
  • 3
  • 12