Listening for External Requests
To handle and listen for requests that may come from any source via the Channel App Template, a basic HTTP server is required. Any popular development framework can be preferred to listen for requests coming through this server. In this document, FastAPI has been used as the development framework.
NOTE
FastAPI has been used as the development framework, but it is not mandatory to use FastAPI.
Listening for a request using FastAPI is quite simple. First, add the FastAPI library to your project. Then, create a server as shown in the example below.
Example Code
from fastapi import FastAPI
app = FastAPI()
@app.post("/webhook")
async def handle_webhook(payload: dict):
"""
Process the data sent by the webhook.
"""
# Payload represents the data coming from the webhook here.
# Perform your operations.
return {"message": "Webhook received!"}
The example above listens for POST requests to the /webhook
path and takes the incoming data as payload
. Here, specified dictionary named payload
is expected, but you can adjust this according to the incoming data.
Notes
This example handles and processes incoming webhook data; however, in real projects, security, error handling, and other conditions should also be considered.
Before running this code, you need to install and run FastAPI. Additionally, this code requires Python version 3.6 or newer.
$ pip install fastapi
$ uvicorn main:app --reload
This example uses the /webhook
path to listen for the webhook. Ensure that the data sent by the webhook matches the expected format.
warning
In real projects, security measures, error handling, and other conditions should be taken into account.
By creating a FastAPI application in this way, you can listen for webhooks and perform operations based on the incoming data.
Additionally, after such development, the Procfile
should be updated to indicate that FastAPI is being used as the development framework:
web: uvicorn channel_app_template.main:app --address=0.0.0.0 --port=8008