Moderate Comments on Facebook Page

 import logging
import os
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from typing import List, Optional
import requests

logger = logging.getLogger(__name__)

app = FastAPI()


class Comment(BaseModel):
    message: str
    id: str

from abilities import llm_prompt

def moderate_comment(comment: str) -> bool:
    prompt = f"""Please analyze this comment for inappropriate content: '{comment}'. 
    Respond with "inappropriate" if the comment is inappropriate, otherwise response with "ok"."""

About this template

An app that moderates comments on your page feed using Facebook API and webhooks. This app uses the FastAPI to create a webhook endpoint for moderating posts on your page. A facebook access token for the page will be needed to make the API call. The permission scopes you will need for the access token are `pages_read_engagement` and `pages_manage_posts permissions` as an admin of the page you are posting to. The app moderates comments on Facebook ads/posts by removing and flagging hateful speech.

Introduction to the Facebook Post Moderator Template

Welcome to the Facebook Post Moderator template! This template is designed to help you moderate comments on your Facebook page feed using the Facebook API and webhooks. It utilizes FastAPI to create a webhook endpoint that automatically moderates posts on your page, flagging or removing any inappropriate comments. Before you begin, ensure you have admin access to the Facebook page you wish to moderate, as you will need this to obtain the necessary access token.

Getting Started

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

Initial Setup: Adding Environment Secrets

Before testing the template, you'll need to set up an environment secret for the PAGE_ACCESS_TOKEN. Here's how to obtain and set up your PAGE_ACCESS_TOKEN:

  • Go to the Facebook Developer Portal and create a new app if you haven't already.
  • Under the app's settings, navigate to the "Add Product" section and set up "Facebook Login."
  • Ensure you have the `pages_read_engagement` and `pages_manage_posts` permissions.
  • Generate a Page Access Token by going to "Token Generation" in the "Messenger" settings.
  • Copy the generated Page Access Token.
  • In the Lazy Builder interface, go to the Environment Secrets tab.
  • Create a new secret with the key `PAGE_ACCESS_TOKEN` and paste the token you copied as the value.

Test: Pressing the Test Button

Once you have set up your environment secret, press the "Test" button in the Lazy builder interface. This will begin the deployment of the app and launch the Lazy CLI.

Using the App

After pressing the "Test" button, Lazy will handle the deployment of your application. You will be provided with a dedicated server link to use the API. Since this template uses FastAPI, you will also receive a link to the API documentation, which you can use to interact with your new Facebook Post Moderator app.

Integrating the App

To integrate the Facebook Post Moderator into your Facebook page, you will need to set up a webhook in your Facebook app settings:

  • Go to the "Webhooks" section in your Facebook app's settings.
  • Click "Edit Subscription" for the Page you want to moderate.
  • Enter the URL provided by Lazy as the Callback URL.
  • Enter a Verify Token of your choice (you will need to update your code to include this token).
  • Select the "messages" and "messaging_postbacks" subscription fields.
  • Click "Verify and Save" to set up the webhook.

Now, your Facebook Post Moderator app will receive webhook events whenever there is activity on your page, and it will automatically moderate comments based on the logic you've set up.

Remember, you don't need to worry about installing libraries or setting up your environment. Lazy handles all of that for you, so you can focus on building your application.

Here's a sample request you might send to your app's API:

POST /webhook/ HTTP/1.1
Content-Type: application/json

  "object": "page",
  "entry": [
      "id": "page-id",
      "time": 1234567890,
      "messaging": [
          "sender": {
            "id": "user-id"
          "recipient": {
            "id": "page-id"
          "timestamp": 1234567890,
          "message": {
            "mid": "mid.1234567890",
            "text": "hello, world!"

And a sample response indicating successful moderation:

  "message": "Comment moderated successfully."

With this template and the Lazy platform, you're now equipped to create a robust comment moderation system for your Facebook page. Happy building!

Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.


A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .


WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

Moderate Comments on Facebook Page