Skip to content

Facebook MCP server for automating posts, comment moderation, insights, and sentiment filtering.

Python

add tools: post img, sent dm and update post

HagaiHenffc14d2bad29
diff --git a/facebook_api.py b/facebook_api.py
index 1b4c01d..6a86f86 100644
--- a/facebook_api.py
+++ b/facebook_api.py
@@ -5,10 +5,10 @@ from config import GRAPH_API_BASE_URL, PAGE_ID, PAGE_ACCESS_TOKEN
class FacebookAPI:
# Generic Graph API request method
- def _request(self, method: str, endpoint: str, params: dict[str, Any]) -> dict[str, Any]:
+ def _request(self, method: str, endpoint: str, params: dict[str, Any], json: dict[str, Any] = None) -> dict[str, Any]:
url = f"{GRAPH_API_BASE_URL}/{endpoint}"
params["access_token"] = PAGE_ACCESS_TOKEN
- response = requests.request(method, url, params=params)
+ response = requests.request(method, url, params=params, json=json)
return response.json()
def post_message(self, message: str) -> dict[str, Any]:
@@ -35,3 +35,21 @@ class FacebookAPI:
def get_bulk_insights(self, post_id: str, metrics: list[str], period: str = "lifetime") -> dict[str, Any]:
metric_str = ",".join(metrics)
return self.get_insights(post_id, metric_str, period)
+
+ def post_image_to_facebook(self, image_url: str, caption: str) -> dict[str, Any]:
+ params = {
+ "url": image_url,
+ "caption": caption
+ }
+ return self._request("POST", f"{PAGE_ID}/photos", params)
+
+ def send_dm_to_user(self, user_id: str, message: str) -> dict[str, Any]:
+ payload = {
+ "recipient": {"id": user_id},
+ "message": {"text": message},
+ "messaging_type": "RESPONSE"
+ }
+ return self._request("POST", "me/messages", {}, json=payload)
+
+ def update_post(self, post_id: str, new_message: str) -> dict[str, Any]:
+ return self._request("POST", f"{post_id}", {"message": new_message})
\ No newline at end of file
diff --git a/manager.py b/manager.py
index 5657ac4..644ca83 100644
--- a/manager.py
+++ b/manager.py
@@ -5,6 +5,8 @@ from facebook_api import FacebookAPI
class Manager:
def __init__(self):
self.api = FacebookAPI()
+ self.handled_comments = set()
+ self.reports = []
def post_to_facebook(self, message: str) -> dict[str, Any]:
return self.api.post_message(message)
@@ -81,3 +83,21 @@ class Manager:
def get_post_reactions_anger_total(self, post_id: str) -> dict[str, Any]:
return self.api.get_insights(post_id, "post_reactions_anger_total")
+
+ def get_post_top_commenters(self, post_id: str) -> list[dict[str, Any]]:
+ comments = self.get_post_comments(post_id).get("data", [])
+ counter = {}
+ for comment in comments:
+ user_id = comment.get("from", {}).get("id")
+ if user_id:
+ counter[user_id] = counter.get(user_id, 0) + 1
+ return sorted([{"user_id": k, "count": v} for k, v in counter.items()], key=lambda x: x["count"], reverse=True)
+
+ def post_image_to_facebook(self, image_url: str, caption: str) -> dict[str, Any]:
+ return self.api.post_image_to_facebook(image_url, caption)
+
+ def send_dm_to_user(self, user_id: str, message: str) -> dict[str, Any]:
+ return self.api.send_dm_to_user(user_id, message)
+
+ def update_post(self, post_id: str, new_message: str) -> dict[str, Any]:
+ return self.api.update_post(post_id, new_message)
\ No newline at end of file
diff --git a/server.py b/server.py
index 3caa8a0..3fe13c3 100644
--- a/server.py
+++ b/server.py
@@ -188,3 +188,35 @@ def get_post_reactions_anger_total(post_id: str) -> dict[str, Any]:
Output: dict with anger count
"""
return manager.get_post_reactions_anger_total(post_id)
+
+def get_post_top_commenters(post_id: str) -> list[dict[str, Any]]:
+ """Get the top commenters on a post.
+ Input: post_id (str)
+ Output: list of user IDs with comment counts
+ """
+ return manager.get_post_top_commenters(post_id)
+
+def post_image_to_facebook(image_url: str, caption: str) -> dict[str, Any]:
+ """Post an image with a caption to the Facebook page.
+ Input: image_url (str), caption (str)
+ Output: dict of post result
+ """
+ return manager.post_image_to_facebook(image_url, caption)
+
+def send_dm_to_user(user_id: str, message: str) -> dict[str, Any]:
+ """Send a direct message to a user.
+ Input: user_id (str), message (str)
+ Output: dict of result from Messenger API
+ """
+ return manager.send_dm_to_user(user_id, message)
+
+def update_post(post_id: str, new_message: str) -> dict[str, Any]:
+ """Updates an existing post's message.
+ Input: post_id (str), new_message (str)
+ Output: dict of update result
+ """
+ return manager.update_post(post_id, new_message)
\ No newline at end of file