1

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}'

ssoler
  • 4,884
  • 4
  • 32
  • 33
Mushroom
  • 13
  • 1
  • 8

2 Answers2

3

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

Jacob Celestine
  • 1,758
  • 13
  • 23
  • TypeError: 'HTTPResponse' object is not subscriptable. I wish it worked. – Mushroom Dec 06 '17 at 13:03
  • name 'json' is not defined. I feel we are close somehow. – Mushroom Dec 06 '17 at 18:02
  • Oh that's easy. just _improt json_ . If its not installed, pip install it. – Jacob Celestine Dec 07 '17 at 03:12
  • how is everything # my input. how is everything # The response is the exact thing i write. Process finished with exit code 0 – Mushroom Dec 08 '17 at 09:02
  • I think instead of calling 'resolvedQuery', we should print 'speech' which is inside of 'fulfillment'. OKAY! I went with message = response['result']['fulfillment']['speech'] and I do get the speech answer right away! <33 – Mushroom Dec 08 '17 at 09:19
  • 1
    Thank you for accepting the answer! I've updated the answer to show the same so that others can refer it. :D – Jacob Celestine Dec 08 '17 at 09:39
0

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'])
mattcarrollcode
  • 3,429
  • 16
  • 16