Build your own Webhooks-Listener
Webhooks-Listener can help you and your team with creating automations, logging and integrations between your tools .
For the sake of simplifying the guide, I will take a sample tool which I will listen to with the Webhooks-Listener
And the chosen tool is … (A dramatic tune in the background)…
I love Flask as my framework for APIs and Websites, you can use Hug module of Python which recommended for APIs as well.
Install the Flask Module:
pip install Flask
Create script with basic POST method function which will get the requests:
from flask import Flask, request
import jsonapp = Flask(__name__)@app.route("/", methods=['POST'])
def listen():
payload = json.loads(request.data)
return (payload)if __name__ == "__main__":
app.run()
Configure the Listener in your repository webhooks:
Choose the required events & save:
Run the script:
$ python my_listener.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now, the script is listening to the Github repository events.
Let’s parser the payload and run conditions according the values, for example:
If the event is an “opening of a pull-request” (event-type = pull-request, action=opened) → run some command :
from flask import Flask, request
import jsonapp = Flask(__name__)@app.route("/", methods=['POST'])
def listen():
payload = json.loads(request.data)
if ('pull_request' not in payload):
print("This is not a Pull-request event")
else:
action = payload['action']
if action == "opened":
# run action here
return (payload)if __name__ == "__main__":
app.run()
Enjoy coding!
More details about Github API & payloads : https://developer.github.com/v4/