无法在Heroku上部署flask-assistant

时间:2018-02-14 16:04:27

标签: python heroku flask actions-on-google dialogflow

我尝试在heroku上部署我的程序,但它不起作用。我仍然是初学者,所以这个问题似乎没有必要。 我第一次使用this 框架,并尝试在Heroku上部署它。 这是我的代码:

import logging
import os
from flask import Flask
from flask_assistant import Assistant, ask, tell, context_manager

app = Flask(__name__)
assist = Assistant(app)
logging.getLogger('flask_assistant').setLevel(logging.DEBUG)


@assist.action('greeting')
def greet_and_start():
    speech = "Hey! Are you male or female?"
    return ask(speech)


@assist.action("give-gender")
def ask_for_color(gender):
    if gender == 'male':
        gender_msg = 'Sup bro!'

    else:
        gender_msg = 'Haay gurl!'

    speech = gender_msg + ' What is your favorite color?'
    return ask(speech)


@assist.action('give-color', mapping={'color': 'sys.color'})
def repeat_color(color):
    speech = 'Ok, {} is an okay color I guess.'.format(color)
    return ask(speech)


if __name__ == '__main__':
    port = int(os.getenv('PORT', 5000))

    print("Starting app on port %d" % port)

app.run(debug=True, port=port, host='0.0.0.0')

这是我的日志:

2018-02-14T15:57:58.872157+00:00 heroku[web.1]: State changed from starting to up
2018-02-14T15:57:58.886874+00:00 app[web.1]: "No API.AI Developer Access Token set.\n                You will not be able to register or retrieve resources from API.AI"
2018-02-14T15:57:58.887063+00:00 app[web.1]: "No API.AI Client Access Token set. You will be able to query the agent"
2018-02-14T15:57:58.887224+00:00 app[web.1]: WARNING:
2018-02-14T15:57:58.887227+00:00 app[web.1]:                 No route was provided for the Assistant object, but a flask `app` object given
2018-02-14T15:57:58.887229+00:00 app[web.1]:                 The Assistant will be mapped to the app's '/' endpoint.
2018-02-14T15:57:58.887230+00:00 app[web.1]:                 If this is a problem please initialize the Assitant with the 'route' parameter
2018-02-14T15:57:58.887288+00:00 app[web.1]:                 
2018-02-14T15:57:58.888136+00:00 app[web.1]: Starting app on port 33277
2018-02-14T15:57:58.941445+00:00 app[web.1]:  * Debugger is active!
2018-02-14T15:57:58.968057+00:00 app[web.1]:  * Debugger PIN: 164-002-182
2018-02-14T15:58:14.293495+00:00 app[web.1]: {
2018-02-14T15:58:14.293511+00:00 app[web.1]:   "action": "greeting",
2018-02-14T15:58:14.293513+00:00 app[web.1]:   "actionIncomplete": false,
2018-02-14T15:58:14.293515+00:00 app[web.1]:   "contexts": [],
2018-02-14T15:58:14.293517+00:00 app[web.1]:   "fulfillment": {
2018-02-14T15:58:14.293518+00:00 app[web.1]:     "messages": [
2018-02-14T15:58:14.293520+00:00 app[web.1]:       {
2018-02-14T15:58:14.293522+00:00 app[web.1]:         "speech": "",
2018-02-14T15:58:14.293523+00:00 app[web.1]:         "type": 0
2018-02-14T15:58:14.293525+00:00 app[web.1]:       }
2018-02-14T15:58:14.293527+00:00 app[web.1]:     ],
2018-02-14T15:58:14.293529+00:00 app[web.1]:     "speech": ""
2018-02-14T15:58:14.293530+00:00 app[web.1]:   },
2018-02-14T15:58:14.293532+00:00 app[web.1]:   "metadata": {
2018-02-14T15:58:14.293535+00:00 app[web.1]:     "intentId": "5d2861b5-a039-494b-8adc-fd7fb623003c",
2018-02-14T15:58:14.293537+00:00 app[web.1]:     "intentName": "Test",
2018-02-14T15:58:14.293540+00:00 app[web.1]:     "webhookForSlotFillingUsed": "false",
2018-02-14T15:58:14.293542+00:00 app[web.1]:     "webhookUsed": "true"
2018-02-14T15:58:14.293544+00:00 app[web.1]:   },
2018-02-14T15:58:14.293545+00:00 app[web.1]:   "parameters": {},
2018-02-14T15:58:14.293547+00:00 app[web.1]:   "resolvedQuery": "hello",
2018-02-14T15:58:14.293549+00:00 app[web.1]:   "score": 1.0,
2018-02-14T15:58:14.293551+00:00 app[web.1]:   "source": "agent",
2018-02-14T15:58:14.293552+00:00 app[web.1]:   "speech": ""
2018-02-14T15:58:14.293554+00:00 app[web.1]: }
2018-02-14T15:58:14.334586+00:00 app[web.1]: 10.171.115.37 - - [14/Feb/2018 15:58:14] "POST / HTTP/1.1" 500 -
2018-02-14T15:58:14.335486+00:00 app[web.1]: Traceback (most recent call last):
2018-02-14T15:58:14.335491+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
2018-02-14T15:58:14.335493+00:00 app[web.1]:     return self.wsgi_app(environ, start_response)
2018-02-14T15:58:14.335495+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
2018-02-14T15:58:14.335497+00:00 app[web.1]:     response = self.handle_exception(e)
2018-02-14T15:58:14.335505+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
2018-02-14T15:58:14.335507+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2018-02-14T15:58:14.335508+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2018-02-14T15:58:14.335510+00:00 app[web.1]:     raise value
2018-02-14T15:58:14.335512+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
2018-02-14T15:58:14.335513+00:00 app[web.1]:     response = self.full_dispatch_request()
2018-02-14T15:58:14.335515+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
2018-02-14T15:58:14.335517+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2018-02-14T15:58:14.335518+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
2018-02-14T15:58:14.335520+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2018-02-14T15:58:14.335521+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
2018-02-14T15:58:14.335523+00:00 app[web.1]:     raise value
2018-02-14T15:58:14.335524+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
2018-02-14T15:58:14.335526+00:00 app[web.1]:     rv = self.dispatch_request()
2018-02-14T15:58:14.335527+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
2018-02-14T15:58:14.335529+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2018-02-14T15:58:14.335533+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_assistant/core.py", line 259, in _flask_assitant_view_func
2018-02-14T15:58:14.335535+00:00 app[web.1]:     view_func = self._match_view_func()
2018-02-14T15:58:14.335537+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask_assistant/core.py", line 287, in _match_view_func
2018-02-14T15:58:14.335538+00:00 app[web.1]:     if not view_func and len(self._intent_action_funcs[self.intent]) == 1:
2018-02-14T15:58:14.335540+00:00 app[web.1]: KeyError: 'Test'
2018-02-14T15:58:14.342730+00:00 heroku[router]: at=info method=POST path="/" host=dry-forest-48664.herokuapp.com request_id=4a1ccef1-6a15-411b-ae94-d74d3a84c1c4 fwd="35.192.209.96" dyno=web.1 connect=1ms service=58ms status=500 bytes=22168 protocol=https

有没有人能解决问题?我将非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您遇到的错误是KeyError: 'Test'

这会出现在对服务器的API.ai(Dialogflow)请求中。

"metadata": {
    "intentId": "5d2861b5-a039-494b-8adc-fd7fb623003c",
    "intentName": "Test",
    "webhookForSlotFillingUsed": "false",
    "webhookUsed": "true"
}

您的意图名称为Test,它似乎不是@assist.action个密钥之一。我正在查看Flask Assistant的Quick Start guide(之前从未使用过),看起来他们使用Intent名称(顶部的标签)作为唯一键。

您的欢迎意图(可能包含greeting行动似乎名为Test。为了使这项工作符合预期,我认为解决方案是将意图Test重命名为greeting以匹配并允许Flask Assistant识别要运行的正确函数。