Python Webhook:通过 URL + payload

Python Webhook: Passing through a URL + payload

我是 Python 的初学者,我正在尝试构建一个从 api.ai 获取信息的服务,将其传递给 API,然后 returns 确认消息来自 JSON 它 returns。

app.py:

#!/usr/bin/env python

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

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

import json
import os
import sys
import logging

from flask import Flask, render_template
from flask import request
from flask import make_response

# Flask app should start in global layout
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)

@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):
    if req.get("result").get("action") != "bookMyConference":
        return {}

    #oauth
    orequest = req.get("originalRequest") # work down the tree
    odata = orequest.get("data") # work down the tree
    user = odata.get("user") # work down the tree
    access_token = user.get("access_token")

    #data
    result = req.get("result") # work down the tree
    parameters = result.get("parameters") # work down the tree
    startdate = parameters.get("start-date")
    meetingname = parameters.get("meeting-name")

    payload = {
        "start-date": startdate,
        "end-date": startdate,
        "meeting-name": meetingname
    }

    # POST info to join.me
    baseurl = "https://api.join.me/v1/meetings"
    p = Request(baseurl)
    p.add_header('Content-Type', 'application/json; charset=utf-8')
    p.add_header('Authorization', 'Bearer ' + access_token) #from oauth
    jsondata = json.dumps(payload)
    jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes
    jresult = urlopen(p, jsondataasbytes).read()
    data = json.loads(jresult)
    res = makeWebhookResult(data)
    return res

def makeWebhookResult(data):

    speech = "Appointment scheduled!"

    print("Response:")
    print(speech)

    return {
        "speech": speech,
        "displayText": speech,
        # "data": data,
        "source": "heroku-bookmyconference"
    }


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')

编辑 4:这是我在 Heroku 日志中遇到的错误:

2017-03-21T19:06:09.383612+00:00 app[web.1]: HTTPError: HTTP Error 400: Bad Request

借鉴 here,使用 processRequest() 中的 urlib 模块,您可以将有效负载添加到 urlopen,如下所示:

req = Request(yql_url)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(payload)
jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes
result = urlopen(req, jsondataasbytes).read()
data = json.loads(result)

如果使用 requests 模块,事情会变得更简洁:

headers = {'content-type': 'application/json'}
result = requests.post(yql_url, data=json.dumps(payload), headers=headers)
data = result.json()

编辑:添加一些特定于 join.me api

的详细信息

查看 join.me docs,您需要获得一个访问令牌才能添加到您的 header。但是在获得访问令牌之前,您还需要一个应用程序授权码。您可以手动或通过链接一些重定向来获取应用程序授权码。

首先,请在您的浏览器中尝试此 url 并从回调参数中获取 code。使用您的 join.me 积分:

auth_url = 'https://secure.join.me/api/public/v1/auth/oauth2' \
    + '?client_id=' + client_id \
    + '&scope=scheduler%20start_meeting' \
    + '&redirect_uri=' + callback_url \
    + '&state=ABCD' \
    + '&response_type=code'
print(auth_url)  # try in browser

获取访问令牌:

token_url = 'https://secure.join.me/api/public/v1/auth/token'
headers = {'content-type': 'application/json'}
token_params = {
    'client_id': client_id,
    'client_secret': client_secret,
    'code': auth_code,
    'redirect_uri': callback_url,
    'grant_type': 'authorization_code'
}
result = requests.post(token_url, data=json.dumps(token_params), headers=headers)
access_token = result.json().get('access_token')

然后您的 header 用于 post 到 /meetings 需要看起来像:

headers = {
    'content-type': 'application/json',
    'Authorization': 'Bearer ' + access_token
}