找不到Heroku(Python)

里里里

作为一个初学者,我无法再进一步了。我试图在heroku上加载python程序,但是总会出现以下情况:

未找到

在服务器上找不到请求的URL。如果您手动输入网址,请检查拼写,然后重试。

我的日志:

2018-02-12T11:33:54.658010+00:00 heroku[web.1]: Starting process with 
command `python app.py`
2018-02-12T11:33:56.853591+00:00 heroku[web.1]: State changed from starting 
to up
2018-02-12T11:33:56.619420+00:00 app[web.1]: Starting app on port 50619
2018-02-12T11:33:56.661819+00:00 app[web.1]:  * Running on 
http://0.0.0.0:50619/ (Press CTRL+C to quit)
2018-02-12T11:34:48.437183+00:00 app[web.1]: 10.5.180.220 - - [12/Feb/2018 
11:34:48] "GET / HTTP/1.1" 404 -
2018-02-12T11:34:48.445571+00:00 heroku[router]: at=info method=GET 
path="/" host=protected-citadel-58416.herokuapp.com request_id=e8d60622-
5acd-4ddf-9b98-87307b1266b7 fwd="79.213.120.187" dyno=web.1 connect=1ms 
service=29ms status=404 bytes=380 protocol=https
2018-02-12T11:40:15.749446+00:00 heroku[router]: at=info method=GET path="/" 
host=protected-citadel-58416.herokuapp.com request_id=8e5c9f8e-c750-48bd-
a8ae-d40e7d3ea1d6 fwd="79.213.120.187" dyno=web.1 connect=0ms service=3ms 
status=404 bytes=380 protocol=https
2018-02-12T11:40:15.743311+00:00 app[web.1]: 10.79.228.240 - - [12/Feb/2018 
11:40:15] "GET / HTTP/1.1" 404 -
2018-02-12T11:40:57.718508+00:00 heroku[router]: at=info method=GET path="/" 
host=protected-citadel-58416.herokuapp.com request_id=312900cd-79d6-4f9c-
9c5b-a40d356219bb fwd="79.213.120.187" dyno=web.1 connect=0ms service=3ms 
status=404 bytes=380 protocol=https
2018-02-12T11:40:57.712039+00:00 app[web.1]: 10.79.228.240 - - [12/Feb/2018 
11:40:57] "GET / HTTP/1.1" 404 -

我的程序(一个dialogflow webhook,也可能是问题所在):

from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()

from urllib.parse import urlparse, urlencode, quote_plus
from urllib.request import urlopen, Request
from urllib.error import HTTPError

import json
import os

from flask import Flask
from flask import request
from flask import make_response

# Flask app should start in global layout
app = Flask(__name__)


@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(res)
    r = make_response(res)
    r.headers['Content-Type'] = 'application/json'
    return r


def processRequest(req):
    print("Request:")
    print(json.dumps(req, indent=4))
    if req.get("result").get("action") == "getpassword":
         baseurl = "http://www.passwordrandom.com/query?command=password&format=json&count=1"
         if yql_query is None:
             return {}
         yql_url = baseurl 
         result = urlopen(yql_url).read()
         data = json.loads(result)
         res = makeWebhookResult(data)
    else:
        return {}
    return res

def makeWebhookResult(data):
    query = data.get('queryresult')
    if queryresult is None:
        return {}

    password = query.get('char')


    # print(json.dumps(item, indent=4))

    speech = "Your new password is " + password

    print("Response:")
    print(speech)

    return {
        "speech": speech,
        "displayText": speech,
        "data": {...},
        "contextOut": [...],
        "source": "WolframAlpha"
    }


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

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

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

我的个人档案:

web: python app.py

我的requirements.txt:

certifi==2018.1.18
chardet==3.0.4
click==6.7
flake8==3.5.0
Flask==0.12.2
future==0.16.0
idna==2.6
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mccabe==0.6.1
pew==1.1.2
pipenv==9.0.3
psutil==5.3.1
pycodestyle==2.3.1
pyflakes==1.6.0
requests==2.18.4
urllib3==1.22
virtualenv==15.1.0
virtualenv-clone==0.2.6
Werkzeug==0.14.1

我究竟做错了什么?提前非常感谢你

这是我的模板

更新

代码已根据建议进行了更改。

日志:

2018-02-12T15:12:39.069747+00:00 app[web.1]: Request:
2018-02-12T15:12:39.069877+00:00 app[web.1]: {
2018-02-12T15:12:39.069880+00:00 app[web.1]:     "id": "14fe0cb2-ce54-48cd-9569-87fc55b6cacc",
2018-02-12T15:12:39.069882+00:00 app[web.1]:     "timestamp": "2018-02-12T15:12:38.929Z",
2018-02-12T15:12:39.069884+00:00 app[web.1]:     "lang": "en",
2018-02-12T15:12:39.069885+00:00 app[web.1]:     "result": {
2018-02-12T15:12:39.069886+00:00 app[web.1]:         "source": "agent",
2018-02-12T15:12:39.069887+00:00 app[web.1]:         "resolvedQuery": "make a password",
2018-02-12T15:12:39.069889+00:00 app[web.1]:         "speech": "",
2018-02-12T15:12:39.069890+00:00 app[web.1]:         "action": "getpassword",
2018-02-12T15:12:39.069891+00:00 app[web.1]:         "actionIncomplete": false,
2018-02-12T15:12:39.069892+00:00 app[web.1]:         "parameters": {
2018-02-12T15:12:39.069894+00:00 app[web.1]:             "passwordlenght": ""
2018-02-12T15:12:39.069895+00:00 app[web.1]:         },
2018-02-12T15:12:39.069896+00:00 app[web.1]:         "contexts": [],
2018-02-12T15:12:39.069897+00:00 app[web.1]:         "metadata": {
2018-02-12T15:12:39.069898+00:00 app[web.1]:             "intentId": "f46eb1ca-e0e3-428f-8442-8e6327b935d7",
2018-02-12T15:12:39.069900+00:00 app[web.1]:             "webhookUsed": "true",
2018-02-12T15:12:39.069901+00:00 app[web.1]:             "webhookForSlotFillingUsed": "false",
2018-02-12T15:12:39.069902+00:00 app[web.1]:             "intentName": "GeneratePassword"
2018-02-12T15:12:39.069903+00:00 app[web.1]:         },
2018-02-12T15:12:39.069904+00:00 app[web.1]:         "fulfillment": {
2018-02-12T15:12:39.069906+00:00 app[web.1]:             "speech": "The service is currently not aviable.",
2018-02-12T15:12:39.069907+00:00 app[web.1]:             "messages": [
2018-02-12T15:12:39.069908+00:00 app[web.1]:                 {
2018-02-12T15:12:39.069909+00:00 app[web.1]:                     "type": 0,
2018-02-12T15:12:39.069911+00:00 app[web.1]:                     "speech": "The service is currently not aviable."
2018-02-12T15:12:39.069913+00:00 app[web.1]:                 }
2018-02-12T15:12:39.069915+00:00 app[web.1]:             ]
2018-02-12T15:12:39.069916+00:00 app[web.1]:         },
2018-02-12T15:12:39.069918+00:00 app[web.1]:         "score": 1.0
2018-02-12T15:12:39.069919+00:00 app[web.1]:     },
2018-02-12T15:12:39.069921+00:00 app[web.1]:     "status": {
2018-02-12T15:12:39.069922+00:00 app[web.1]:         "code": 200,
2018-02-12T15:12:39.069924+00:00 app[web.1]:         "errorType": "success",
2018-02-12T15:12:39.069925+00:00 app[web.1]:         "webhookTimedOut": false
2018-02-12T15:12:39.069927+00:00 app[web.1]:     },
2018-02-12T15:12:39.069929+00:00 app[web.1]:     "sessionId": "dbb472ca-cd7d-4c03-b666-66d90b458f22"
2018-02-12T15:12:39.069934+00:00 app[web.1]: }
2018-02-12T15:12:39.341869+00:00 app[web.1]: 10.45.77.75 - - [12/Feb/2018 15:12:39] "POST / HTTP/1.1" 200 -
2018-02-12T15:12:39.344514+00:00 heroku[router]: at=info method=POST path="/" host=protected-citadel-58416.herokuapp.com request_id=842f0891-6768-48e9-a6d8-740b8dc4a8d6 fwd="35.202.154.33" dyno=web.1 connect=2ms service=275ms status=200 bytes=147 protocol=https

来自对话流的JSON:

{
  "id": "947ae098-542c-4208-b49a-eed69fb001fd",
  "timestamp": "2018-02-12T15:30:24.471Z",
  "lang": "en",
  "result": {
    "source": "agent",
    "resolvedQuery": "make a password",
    "action": "getpassword",
    "actionIncomplete": false,
    "parameters": {
      "passwordlenght": ""
    },
    "contexts": [],
    "metadata": {
      "intentId": "f46eb1ca-e0e3-428f-8442-8e6327b935d7",
      "webhookUsed": "true",
      "webhookForSlotFillingUsed": "false",
      "webhookResponseTime": 290,
      "intentName": "GeneratePassword"
    },
    "fulfillment": {
      "messages": [
        {
          "type": 0,
          "speech": "The service is currently not aviable."
        }
      ]
    },
    "score": 1
  },
  "status": {
    "code": 200,
    "errorType": "success",
    "webhookTimedOut": false
  },
  "sessionId": "dbb472ca-cd7d-4c03-b666-66d90b458f22"
}
囚犯

看来您没有为/请求设置路由,只是为/webhook路径的POST设置了路由。看来您是在尝试通过输入浏览器地址来手工对其进行测试-并不能真正帮到您很多。

同样,您似乎尚未在Dialogflow中将Webhook配置为使用/webhook路径。在实现部分中,您应该设置完整URL的URL-包括路径。它可能看起来像这样(请注意以/ webhook结尾):

https://YourHostNameHere:50619/webhook

您应该能够使用curl之类的工具通过POST来访问URL。您需要确保它是通过HTTPS进行的,并且要发送带有有效标头的JSON。可能是这样的

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://YourHostNameHere:50619/webhook

或者,如果文件中包含JSON

curl -d "@file.json" -X POST https://YourHostNameHere:50619/webhook

您也可以尝试Dialogflow的测试侧边栏-如果该模式与应通过Webhook实现的内容匹配,它将发送到那里。

但这可能仍不如您期望的那样有效。看起来好像返回的makeWebhookResult()不是有效的JSON,它将使您的代码和Dialogflow的所有事情混淆。

更新以解释您的代码

这些行在您的代码中:

@app.route('/webhook', methods=['POST'])
def webhook():

非常粗略地说:“我正在定义一个名为的函数webhook()。在此服务的URL处以字符串结尾的POST请求时,应调用此函数/webhook。”

因此,如果heroku.example.com使用HTTPS将其托管在端口59123上,则Webhook的完整URL将为https://heroku.example.com:59123/webhook,这是您需要在Dialogflow履行页面中输入的URL。

您的日志表明您正在调用此程序而没有任何内容/webhook特别是这条线

2018-02-12T14:23:15.577096+00:00 heroku[router]: at=info method=POST path="/" host=protected-citadel-58416.herokuapp.com request_id=b1b7e6d8-fd75-44e5-a2ab-1a73ea303f80 fwd="35.192.209.96" dyno=web.1 connect=0ms service=57ms status=404 bytes=380 protocol=https

建议,尽管正在使用HTTPS和POST在您的主机上调用它,但它试图路由到的路径是“ /”而不是“ / webhook”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章