首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中向actions-on-google发送响应?

如何在Python中向actions-on-google发送响应?
EN

Stack Overflow用户
提问于 2020-10-28 16:35:04
回答 1查看 156关注 0票数 0

我在做一个google上的操作助手。通过使用Flask在Python中建立webhook,我能够接收JSON格式的请求。但我不知道如何将响应发送回助理。enter image description here enter image description here

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-10-30 02:44:09

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

您应该参考HandlerResponse类型的JSON schema。因为它是JSON schema,所以您可以使用a tool like Quicktype来生成适当的类,以获得额外的语法支持。

架构文件还包括内部类型的定义。

代码语言:javascript
复制
"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"
    }
  }
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64569101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档