微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Python发送对Google动作的响应?

如何解决如何使用Python发送对Google动作的响应?

我正在做一个Google行动助手。通过使用Flask在Python中建立webhook,我可以接收JSON格式的请求。但是我不知道如何将响应发送回助手。 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)

解决方法

您的Flask服务器应以正确的格式返回JSON响应。看来您可能正在使用googleactions程序包,但不幸的是,该程序包似乎已过期,而Actions Builder期望的响应格式是这样。

对于HandlerResponse类型,应咨询JSON schema。由于它是JSON模式,因此可以使用a tool like 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"
    }
  }
},
,

我也使用Python,据说可以正确地响应Json格式,但是,我按照https://developers.google.com/assistant/conversational/df-asdk/rich-responses#basic_card的文档进行操作,但是出现了错误。

实际上,我使用了另一种具有基本格式的Json格式,它可以工作,但是某些丰富的内容响应为FAIL,有人可以显示正确的规范/文档吗?

非常感谢Vin

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。