0

我正在做一个行动谷歌助手。我可以通过使用 Flask 在 Python 中建立 webhook 来接收 JSON 格式的请求。但我不知道如何将响应发送回助手。 在此处输入图像描述 在此处 输入图像描述

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)
4

1 回答 1

0

您的 Flask 服务器应该以正确的格式返回 JSON 响应。看起来您可能正在使用googleactions包,但不幸的是,该包似乎与 Actions Builder 预期的响应格式已过时。

您应该查阅JSON 模式以了解该HandlerResponse类型。由于它是 JSON 模式,因此您可以使用Quicktype 之类的工具来生成适当的类以获得额外的语法支持。

模式文件还包括内部类型的定义。

"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"
    }
  }
},
于 2020-10-29T18:44:09.200 回答