gpt4 book ai didi

python - Dialogflow 履行 Webhook 连接失败

转载 作者:行者123 更新时间:2023-12-01 06:45:23 26 4
gpt4 key购买 nike

我正在尝试使用 DialogFlow 实现 Webhook 来访问我的应用程序,但每当我发出请求时,它都会返回:

POST /webhook 500 INTERNAL SERVER ERROR

来自ngrok。我已经检查了关于dialogflow和ngrok连接的几乎所有内容,但一无所获

WebHook URL

https://3aa04ed7.ngrok.io/webhook

ma​​in.py

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

print("Request:")
print(json.dumps(req, indent=4))

res = processRequest(req)
res = json.dumps(res, indent=4)

print("Response:")
print(res)

response = make_response(res)
response.headers['Content-Type'] = 'application/json'
return response


def processRequest(req):
print("Processing request")
result = req.get("result")
# contexts = result.get("contexts")
action = result.get("action")

if action == "givesymptoms":
return giveSymptoms(req)
elif action == "followup":
return followUp(req)
return result

终端错误

Processing request
127.0.0.1 - - [09/Dec/2019 04:56:40] "POST /webhook HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\flask\app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\User\Desktop\HomeDoctor-master\HomeDoctor-master\main.py", line 41, in webhook
res = processRequest(req)
File "C:\Users\User\Desktop\HomeDoctor-master\HomeDoctor-master\main.py", line 56, in processRequest
action = result.get("action")
AttributeError: 'NoneType' object has no attribute 'get'

最佳答案

在您的processRequest函数中

result = req.get("result")

返回 None,因此当您尝试在 None 类型对象中调用 get('action') 时,它会引发 'NoneType' 对象没有属性 'get'

所以你可以使用这样的东西

result = req.get("result", {})
# if there is no key with 'result' in that dict req.get("result", {}) returns {}
action = result.get("action")
# if there is no key with "action" in result object action carries None type

关于python - Dialogflow 履行 Webhook 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59242069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com