我正在对谷歌助手采取行动。通过使用Flask在Python中建立webhook,我能够接收JSON格式的请求。但我不知道如何将回复发送回助理。 enter image description hereenter 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生成适当的类以获得额外的语法支持架构文件还包括内部类型的定义
相关问题 更多 >
编程相关推荐