Skip to content

Instantly share code, notes, and snippets.

@cobaltgit
Last active January 29, 2022 11:51
Show Gist options
  • Save cobaltgit/98dd64313a7ed942ed928c1df508a6d5 to your computer and use it in GitHub Desktop.
Save cobaltgit/98dd64313a7ed942ed928c1df508a6d5 to your computer and use it in GitHub Desktop.
Receive webhooks using Sanic
from sanic import Sanic
from sanic.response import json
from sanic.exceptions import InvalidUsage
app = Sanic(__name__)
def webhook_actions():
"""Actions to perform when webhook receives POST request"""
pass
@app.post("/")
async def webhook(request):
if request.headers.get("Content-Type") == "application/json":
webhook_actions()
return json(request.json)
else:
raise InvalidUsage(f"Invalid content type - expected application/json, got {request.headers["Content-Type"]})
if __name__ == "__main__":
app.run(port=5000, host="0.0.0.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment