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
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
FACEBOOK_GRAPH_API_URL = "https://graph.facebook.com/v19.0"
PAGE_ACCESS_TOKEN = os.environ['PAGE_ACCESS_TOKEN']
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"."""
Created: | Last Updated:
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
Host: your-lazy-app-url.com
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!