Get Facebook Post Comments Using API

Test this app for free
115
import logging
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
import os
from facebook_comments_fetcher import fetch_facebook_post_comments

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)

app = FastAPI()

class PostID(BaseModel):
    post_id: str

@app.post("/fetch-comments/")
def fetch_comments(post_data: PostID):
    access_token = os.environ.get("FACEBOOK_ACCESS_TOKEN")
    if not access_token:
        raise HTTPException(status_code=500, detail="Facebook access token is not configured.")
    try:
        comments = fetch_facebook_post_comments(post_data.post_id, access_token)
        return comments
    except HTTPException as e:
        raise HTTPException(status_code=e.status_code, detail=e.detail)
Get full code

Created: | Last Updated:

An app for fetching a post's comments using the Facebook API. The permission scopes you will need are `pages_read_engagement`, `pages_manage_metadata` and `pages_read_user_content`. This app uses the FastAPI to create an endpoint to call the Facebook API for getting a post's comments according to the post id and access token we provide the app. Using this app, we can retrieve and analyze comments on Facebook posts using the Facebook API.

Introduction to the Fetch Facebook Post Comments Template

Welcome to the Lazy template guide for fetching comments from a Facebook post using the Facebook API. This template is designed to help you quickly set up an application that can retrieve comments from a specific Facebook post. It's perfect for social media managers, marketers, or developers who need to analyze engagement on their Facebook pages.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial Setup: Adding Environment Secrets

Before you can fetch comments from Facebook, you'll need to set up an environment secret for your Facebook access token. This token is required to authenticate your requests to the Facebook API.

  • Go to the Facebook Developer Portal and create an app if you haven't already.
  • Under your app's settings, navigate to the "Permissions and Features" section.
  • Request and obtain the following permissions: pages_read_engagement, pages_manage_metadata, and pages_read_user_content.
  • Once you have the permissions, generate an access token for your app.
  • In the Lazy Builder interface, go to the Environment Secrets tab.
  • Create a new secret with the key FACEBOOK_ACCESS_TOKEN and paste your generated access token as the value.

Test: Pressing the Test Button

With your environment secret set, you're ready to test the application. Press the "Test" button on the Lazy platform. This will deploy your app and launch the Lazy CLI.

Entering Input: Filling in User Input

After pressing the "Test" button, if the application requires any user input, the Lazy CLI will prompt you to provide it. For this template, you will need to enter the Facebook post ID for which you want to fetch comments.

Using the App

Once the app is running, Lazy will provide you with a dedicated server link to use the API. Additionally, since this app uses FastAPI, you will also receive a link to the API documentation. This documentation will guide you on how to make requests to your new endpoint and fetch comments from a Facebook post.

Integrating the App

After successfully fetching comments, you may want to integrate this functionality into your existing systems or frontend applications. Here's how you can do that:

  • Use the server link provided by Lazy to make HTTP POST requests to the /fetch-comments/ endpoint from your frontend or service.
  • Include the Facebook post ID in the body of your request as JSON.
  • Handle the response from the API, which will include the comments data, in your application.

Here's a sample request you might make from another application:

POST /fetch-comments/ HTTP/1.1<br> Host: [Your Lazy Server Link]<br> Content-Type: application/json<br> <br> {<br>   "post_id": "1234567890123456"<br> } And a sample response you might receive:

{<br>   "data": [<br>     {<br>       "from": {<br>         "name": "User Name",<br>         "id": "1234567890"<br>       },<br>       "message": "This is a comment!",<br>       "id": "0987654321"<br>     },<br>     ...<br>   ],<br>   "paging": { ... },<br>   "summary": { ... }<br> } By following these steps, you can seamlessly integrate the Fetch Facebook Post Comments app into your workflow, allowing you to gather valuable insights from your Facebook audience.

Technologies

FastAPI Templates and Webhooks FastAPI Templates and Webhooks

Similar templates

FastAPI endpoint for Text Classification using OpenAI GPT 4

This API will classify incoming text items into categories using the Open AI's GPT 4 model. If the model is unsure about the category of a text item, it will respond with an empty string. The categories are parameters that the API endpoint accepts. The GPT 4 model will classify the items on its own with a prompt like this: "Classify the following item {item} into one of these categories {categories}". There is no maximum number of categories a text item can belong to in the multiple categories classification. The API will use the llm_prompt ability to ask the LLM to classify the item and respond with the category. The API will take the LLM's response as is and will not handle situations where the model identifies multiple categories for a text item in the single category classification. If the model is unsure about the category of a text item in the multiple categories classification, it will respond with an empty string for that item. The API will use Python's concurrent.futures module to parallelize the classification of text items. The API will handle timeouts and exceptions by leaving the items unclassified. The API will parse the LLM's response for the multiple categories classification and match it to the list of categories provided in the API parameters. The API will convert the LLM's response and the categories to lowercase before matching them. The API will split the LLM's response on both ':' and ',' to remove the "Category" word from the response. The temperature of the GPT model is set to a minimal value to make the output more deterministic. The API will return all matching categories for a text item in the multiple categories classification. The API will strip any leading or trailing whitespace from the categories in the LLM's response before matching them to the list of categories provided in the API parameters. The API will accept lists as answers from the LLM. If the LLM responds with a string that's formatted like a list, the API will parse it and match it to the list of categories provided in the API parameters.

Icon 1 Icon 1
218

We found some blogs you might like...