0

I'm making an action-on-google assistant. I'm able to receive the request in JSON format by using Flask to establish webhook in Python. But I've no idea how to send the response back to the assistant. enter image description here enter image description here

import os, sys
from flask import Flask, request, send_from_directory, make_response
from googleactions import AppRequest, AppResponse, SimpleResponse

class operation():

    def justPrint(self):
        print("Hi dear user")
        print(AppResponse('告訴我故事發生什麼事吧').json())

app = Flask(__name__)

@app.route('/', methods=['GET'])
def verify():
    return "hello world"

@app.route('/', methods=['POST'])
def webhook():
    req = request.get_json()

    print(req)
    op = operation()
    getattr(op, req['handler']['name'])()
    return 'ok', 200

if __name__ == "__main__":
    app.run(debug=True, port=8080)
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
L MG
  • 1

1 Answers1

0

Your Flask server should return a JSON response in the correct format. It looks like you may be using the googleactions package, but unfortunately that package seems to be out-of-date with the response format expected by Actions Builder.

You should consult the JSON schema for the HandlerResponse type. As it is JSON schema, you can use a tool like Quicktype to generate the appropriate classes for additional syntax support.

The schema file also includes definitions for the internal types.

"HandlerResponse": {
  "description": "Represents a response sent from a developer's fulfillment to Actions on\nGoogle.",
  "type": "object",
  "properties": {
    "prompt": {
      "description": "Optional. Represents the prompts to be sent to the user, these prompts\nwill be appended to previously added messages unless explicitly\noverwritten.",
      "$ref": "#/definitions/Prompt"
    },
    "scene": {
      "description": "Optional. Represents the current and next scene. If `Scene.next` is set\nthe runtime will immediately transition to the specified scene.",
      "$ref": "#/definitions/Scene"
    },
    "session": {
      "description": "Optional. Describes data for the current session, session\nparameters can be created, updated, or removed by the fulfillment.",
      "$ref": "#/definitions/Session"
    },
    "user": {
      "description": "Optional. Use to specify user parameters to send back.",
      "$ref": "#/definitions/User"
    },
    "home": {
      "description": "Optional. Used to specify parameters related to the HomeGraph structure\nthat the target device belongs to. See\nhttps://developers.google.com/actions/smarthome/concepts/homegraph.",
      "$ref": "#/definitions/Home"
    },
    "device": {
      "description": "Optional. Use to move between Assistant devices the user has access to.",
      "$ref": "#/definitions/Device"
    },
    "expected": {
      "description": "Optional. Describes the expectations for the next dialog turn.",
      "$ref": "#/definitions/Expected"
    }
  }
},
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • Thank you for your reply. I guess there is no API for python to achieve my requirement which is to send the response back to the actions on google. I'm going to try node.js to see if the method can do the trick. – L MG Oct 31 '20 at 07:52
  • There's no quick-to-import API, but following the JSON format you can just send responses directly – Nick Felker Nov 03 '20 at 16:54