[英]Heroku-Not Found (Python)
As a beginner, I just can't get any further. 作为一个初学者,我无法再进一步了。 I am trying to load a python program on heroku, but somehow only the following will always appear:
我试图在heroku上加载python程序,但是总会出现以下情况:
My logs: 我的日志:
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 -
My program (a dialogflow webhook, could also be the problem): 我的程序(一个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')
My Procfile: 我的个人档案:
web: python app.py
My requirements.txt: 我的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
What am I doing wrong? 我究竟做错了什么? Thank you very much in advance
提前非常感谢你
The code has been changed as suggested. 代码已根据建议进行了更改。
Log: 日志:
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 from dialogflow: 来自对话框流程的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"
}
It looks like you don't have a route set for the /
request, just for a POST to the /webhook
path. 似乎您没有为
/
请求设置路由,只是为/webhook
路径的POST设置了路由。 It looks like you're trying to test it by hand by entering in the address to your browser - which won't really help you a lot. 看来您是在尝试通过输入浏览器的地址来进行手动测试-并不能真正帮助您。
Similarly, it looks like you haven't configured the webhook in Dialogflow to use the /webhook
path. 同样,您似乎尚未在Dialogflow中将Webhook配置为使用
/webhook
路径。 In the fulfillment section, you should set the URL for the full URL - including path. 在实现部分中,您应该设置完整URL的URL-包括路径。 It might look something like this (note how it ends with /webhook):
它可能看起来像这样(请注意以/ webhook结尾):
https://YourHostNameHere:50619/webhook
You should be able to use a tool such as curl to hit the URL using a POST. 您应该能够使用curl之类的工具通过POST来访问URL。 You'll need to make sure it is via HTTPS and that you're sending JSON with a valid header.
您需要确保它是通过HTTPS进行的,并且要发送带有有效标头的JSON。 Possibly something like this
可能是这样的
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://YourHostNameHere:50619/webhook
or, if you have the JSON in a file 或者,如果文件中包含JSON
curl -d "@file.json" -X POST https://YourHostNameHere:50619/webhook
You can also try through Dialogflow's test sidebar - if the pattern matches something that should be fulfilled through a webhook, it will send it there. 您还可以尝试Dialogflow的测试侧边栏-如果该模式与应通过Webhook实现的内容匹配,它将发送到那里。
But this still may not quite work as you expect. 但是,这可能仍然无法如您所愿。 It doesn't look like the return from
makeWebhookResult()
is valid JSON, which will confuse all sorts of things in your code and Dialogflow's. 看起来
makeWebhookResult()
的返回makeWebhookResult()
不是有效的JSON,它将使您的代码和Dialogflow中的所有事情混淆。
Update to explain your code a little bit 更新以解释您的代码
These lines in your code: 您的代码中的这些行:
@app.route('/webhook', methods=['POST'])
def webhook():
very roughly say "I'm defining a function called webhook()
. You should call this function when you get a POST request at the URL for this service that ends with the string /webhook
." 非常粗略地说:“我正在定义一个名为
webhook()
函数。当在该服务的URL处以字符串/webhook
结尾的POST请求时,应调用此函数。”
So if you were hosting this at heroku.example.com
on port 59123 using HTTPS, then the full URL for the webhook would be https://heroku.example.com:59123/webhook
, and this is the URL you would need to enter this into the Dialogflow fulfillment page. 因此,如果您是使用HTTPS在端口59123上的
heroku.example.com
上托管的,则Webhook的完整URL为https://heroku.example.com:59123/webhook
,这是您需要使用的URL将其输入Dialogflow履行页面。
Your logs indicate you are calling this without the /webhook
part. 您的日志表明您不带
/webhook
部分调用此命令。 In particular, this line 特别是这条线
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
Suggests that, although it is being called at your host using HTTPS and POST, the path it is trying to route to is "/" rather than "/webhook". 建议,尽管正在使用HTTPS和POST在您的主机上调用它,但它试图路由到的路径是“ /”而不是“ / webhook”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.