In this tutorial, we will construct a straightforward endpoint using Python and Flask, followed by making this local webhook endpoint
accessible on the internet. Next, we will link it to DeepReel webhook events, allowing us to begin gathering events at our endpoint.
Let’s get started!
We’ll start by creating a basic endpoint application using Python and Flask. Ensure you have the Flask library installed on your system.
You can install it using the following command:
Bash
Copy
pip install Flask
Next, set up your ‘webhook_event_listener’ endpoint. We’ll configure it to accept POST requests, as DeepReel will send POST requests to your endpoint.
Copy
from flask import Flask, requestapp = Flask(__name__)@app.route("/webhook_event_listener", methods=["POST"])def webhook_event_listener(): """ Processes POST requests at the '/webhook_event_listener' endpoint. """ print(request.json) return "Success!", 200if __name__ == "__main__": app.run(debug=True)
After creating this Python file, save it and run your Flask application using the following command:
Bash
Copy
python app.py
Now, your local endpoint is operational. When a POST request is sent to 127.0.0.1:5000/webhook_event_listener with a JSON body,
you will be able to see the output displayed in your console.
In a development environment, your Flask application typically runs on localhost, which isn’t accessible from the internet. To receive webhooks from external sources, tools like Ngrok, LocalTunnel, Cloudflare Tunnel, or Smee.io can be used to make your local server internet-accessible.For our needs, we’ll use Smee.io, as it is free and provides a stable endpoint address. This is beneficial since we will be registering our endpoint with DeepReel.
Bash
Copy
npm install -g smee-client
Now, visit https://smee.io/new to acquire a new endpoint address. After that, you can use the following command to make your local endpoint accessible:
To finalize the implementation, it’s crucial to enhance your endpoint’s security by verifying DeepReel’s requests using the secret key provided.Upon successful request reception, we will display the event type and event data that DeepReel has sent to our endpoint.
Copy
from flask import Flask, requestimport hmacfrom hashlib import sha256app = Flask(__name__)@app.route("/webhook_event_listener", methods=["POST"])def webhook_event_listener(): # Secret key received from DeepReel for verifying the request wh_secret = "<secret>" # Extracting the content of the request content_str = (request.data or b"").decode() # Extracting the signature from the request headers signature = request.headers.get("signature", "") # Calculating the HMAC of the content with the secret key mac = hmac.new( wh_secret.encode("utf-8"), msg=content_str.encode("utf-8"), digestmod=sha256, ) computed_signature = mac.hexdigest() # Checking if the computed signature matches the received signature if computed_signature != signature: raise Exception("Invalid request") # Processing the event data from the request JSON req_json = request.json event_type = req_json.get('type') event_error = req_json.get('error') event_data = req_json.get('data') # Printing event information if event_error is None: print(f"Event Type: {event_type}; Event Data: {event_data}") else: print(f"Event Type: {event_type}; Event Error: {event_error}") return "Success!", 200if __name__ == "__main__": app.run(debug=True)
Now, you are equipped to handle webhook events from DeepReel and open up a realm of opportunities for your application. Congratulations on successfully completing this integration journey!
In this guide, you’ve successfully established a powerful webhook endpoint for DeepReel using Python and Flask. This endpoint enables you to effortlessly receive and manage video information, paving the way for interactive and data-driven applications. Your creative integration with DeepReel’s features is set to elevate user experiences, adding dynamism and engagement to your projects.