Mushroom
Mushroom

Reputation: 13

How to get text response directly on Dialogflow?

I'm trying to get a direct text response from dialogflow. I do get an answer from the example code on github, but thats not user-friendly at all. How can I get the "Speech-Only" response?

import os.path
import sys

try:
    import apiai
except ImportError:
    sys.path.append(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)
    )
    import apiai

CLIENT_ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'


def main():
    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)

    request = ai.text_request()

    request.lang = 'de'  # optional, default value equal 'en'

    request.session_id = "<SESSION ID, UNIQUE FOR EACH USER>"

    request.query = "Hello"

    response = request.getresponse()

    print (response.read())


if __name__ == '__main__':
    main()

I just expect a a simple result. A plain hello text.

What I get instead = b'{\n "id": "306fd06a-d9e6-4c2e-8c05-98ff7fc0ecd5",\n "timestamp": "2017-12-05T22:18:15.563Z",\n "lang": "en",\n "result": {\n "source": "agent",\n "resolvedQuery": "hi",\n "action": "input.welcome",\n "actionIncomplete": false,\n "parameters": {},\n "contexts": [],\n "metadata": {\n "intentId": "8406ea3a-a0c9-4470-829f-aba0ce2da2e5",\n "webhookUsed": "false",\n "webhookForSlotFillingUsed": "false",\n "intentName": "Default Welcome Intent"\n },\n "fulfillment": {\n "speech": "Hi there!",\n "messages": [\n {\n "type": 0,\n "speech": "Hi there!"\n }\n ]\n },\n "score": 1.0\n },\n "alternateResult": {\n "source": "domains",\n "resolvedQuery": "hi",\n "action": "smalltalk.greetings.hello",\n "actionIncomplete": false,\n "parameters": {},\n "contexts": [],\n "metadata": {},\n "fulfillment": {\n "speech": "Hey!",\n "source": "agent"\n },\n "score": 1.0\n },\n "status": {\n "code": 200,\n "errorType": "success",\n "webhookTimedOut": false\n },\n "sessionId": "mr.9000"\n}'

Upvotes: 1

Views: 3415

Answers (2)

Jacob Celestine
Jacob Celestine

Reputation: 1789

Just try like this to get the message:

response = json.loads(request.getresponse().read().decode('utf-8'))
message = response['result']['fulfillment']['speech']
print (message)

Don't forget to add

import json

at the beginning. If you haven't installed it yet, install it. You'll have to if you want to deal with json in python. Tell me if it works

Upvotes: 3

mattcarrollcode
mattcarrollcode

Reputation: 3469

Looks like you're using Dialogflow's query API. The format of the response is documented here. You'll have to parse the JSON. The most common way to do this would be ...

  1. Use the json module which you'll need to import (i.e. import json at the top of you file).
  2. Next you'll need to load the JSON string you're received with the load method (i.e. add a line after you define response: response_dict = json.loads(response.read())
  3. Lastly you'll need to retrieve the correct string from the response_dict object: print(response_dict.['result']['resolvedQuery'])

Upvotes: 0

Related Questions