How do I receive Github Webhooks in Python

后端 未结 4 1492
野的像风
野的像风 2020-12-04 08:14

Github offers to send Post-receive hooks to an URL of your choice when there\'s activity on your repo. I want to write a small Python command-line/backgroun

4条回答
  •  暖寄归人
    2020-12-04 08:55

    If you are using Flask, here's a very minimal code to listen for webhooks:

    from flask import Flask, request, Response
    
    app = Flask(__name__)
    
    @app.route('/webhook', methods=['POST'])
    def respond():
        print(request.json) # Handle webhook request here
        return Response(status=200)
    

    And the same example using Django:

    from django.http import HttpResponse
    from django.views.decorators.http import require_POST
    
    @require_POST
    def example(request):
        print(request.json) # Handle webhook request here
        return HttpResponse('Hello, world. This is the webhook response.')
    

    If you need more information, here's a great tutorial on how to listen for webhooks with Python.

提交回复
热议问题