python stackdriver google function webhook listener

时间:2019-01-23 11:10:11

标签: python google-cloud-functions stackdriver google-cloud-stackdriver

我正在尝试使用以下脚本在google cloud函数中创建一个stackdriver webhook侦听器:

import sys
import logging
import json

from flask import Flask
from flask import Response, request

def webhook(request):
    logging.info("Stackdriver ga360_merge_ready starting up on %s" % (str.replace(sys.version, '\n', ' ')))

    app = Flask(__name__)


    @app.route('/', methods=['POST'])
    def simple_handler():
        """ Handle a webhook post with no authentication method """
        json_data = json.loads(request.data)
        logging.info(json.dumps(json_data, indent=4))
        return Response("OK")

对于上述内容,我有以下网址:

https://xxxxx.cloudfunctions.net/webhook

“ webhook”是云功能名称。当我按照代码将URL末尾加一个斜杠时,它似乎并没有从stackdriver跨消息发送,本质上,我希望消息也能够通过,目前,我得到的只是以下三个消息日志条目:

enter image description here

不确定我缺少什么,我是python / webhooks世界的新手

1 个答案:

答案 0 :(得分:1)

您的simple_handler从未被调用,因为request从未被路由到您创建的app

是否有这样设置您的功能的原因?我希望它是这样的:

import sys
import logging
import json

logging.info("Stackdriver ga360_merge_ready starting up on %s" % (str.replace(sys.version, '\n', ' ')))

def webhook(request):
    """ Handle a webhook post with no authentication method """
    logging.info(json.dumps(request.get_json(), indent=4))
    return Response("OK")