【发布时间】:2018-04-05 21:46:11
【问题描述】:
我一直在尝试编辑一个最初用于天气 API 的 webhook,以便与邮政编码/邮政编码 API 一起使用。原文件在这里:https://github.com/dialogflow/fulfillment-webhook-weather-python/blob/master/app.py 我不明白我的不同之处,当我用引号替换 urlencode 时,我以为我已经解决了它,但是唉,这还不够。 该问题不太可能与在 postcodeValue() 中收集邮政编码的源 json 请求有关。当您在浏览器中输入 api url 时,它会显示正确,并且显示得非常简单。 https://api.getaddress.io/find/SW11%201th?api-key=I98umgPiu02GEMmHdmfg3w12959 格式是否正确?也许我需要将它转换成更多的 JSON,然后它已经是。这个问题本质上是我希望有人可以拯救我的一天结束的大脑转储。
from __future__ import print_function
from future.standard_library import install_aliases
install_aliases()
from urllib.parse import urlparse, urlencode, quote
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__)
#this line is just naming conventions I reckon with a reference to expect to receive data as POST
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
#who knows where this is getting printed
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):
if req.get("result").get("action") != "yahooWeatherForecast":
return {}
baseurl = "https://api.getaddress.io/find/"
apikey = "?api-key=I98umgPiu02GEMmHdmfg3w12959"
yql_query = postcodeValue(req)
if yql_query is None:
return {}
#this line is the actual api request
yql_url = baseurl + quote(yql_query) + apikey
result = urlopen(yql_url).read()
data = json.loads(result)
res = makeWebhookResult(data)
return res
#this function extracts an individual parameter and turns it into a string
def postcodeValue(req):
result = req.get("result")
parameters = result.get("parameters")
postcode = parameters.get("postcode")
if postcode is None:
return None
return postcode
#def housenoValue(req):
# result = req.get("result")
#parameters = result.get("parameters")
#houseno = parameters.get("houseno")
#if houseno is None:
# return None
#return houseno
def makeWebhookResult(data):
longitude = data.get("longitude")
if longitude is None:
return {}
#def makeWebhookResult(data):
# query = data.get('query')
# if query is None:
# return {}
# result = query.get('results')
# if result is None:
# return {}
# channel = result.get('channel')
# if channel is None:
# return {}
# item = channel.get('item')
# location = channel.get('location')
# units = channel.get('units')
# if (location is None) or (item is None) or (units is None):
# return {}
# condition = item.get('condition')
# if condition is None:
# return {}
# print(json.dumps(item, indent=4))
speech = "Sausage face " + longitude
print("Response:")
print(speech)
return {
"speech": speech,
"displayText": speech,
# "data": data,
# "contextOut": [],
"source": "apiai-weather-webhook-sample"
}
#More flask specific stuff
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')
【问题讨论】:
-
也许尝试打印 before 做其他事情(即读取 json)。它也可能有助于打印请求的正文。您是否尝试过使用 wget 或类似工具向此 webhook 发送 POST?
-
干杯! Alex 的本地 webhook 链接以及您的评论启发我下载 ngrok,我发现问题是我没有输入 str(longitude)。我只选择了经度作为占位符,我什至没有想到它是一个浮点数!不过,Ngrok 是上帝派来的,谢谢!
标签: python json flask webhooks dialogflow-es