diff --git a/pxy_meta_pages/README.md b/pxy_meta_pages/README.md new file mode 100644 index 0000000..a60df3d --- /dev/null +++ b/pxy_meta_pages/README.md @@ -0,0 +1,78 @@ +### ๐Ÿง  **Core Purpose** + +The app links **Facebook Pages** to **OpenAI assistants**, listens to webhook events from Facebook (like comments or shares), and responds automatically with **AI-generated replies**. It logs all interactions for analytics or auditing. + +--- + +### ๐Ÿงฉ **Key Components and Their Roles** + +#### 1. **Models (`models.py`)** + +* `FacebookPageAssistant`: Links a Facebook Page (`page_id`, `page_name`) to an `OpenAIAssistant`, and tracks whether it's subscribed to webhooks. +* `EventType`: Catalog of event types like `comment`, `share`. +* `FacebookEvent`: Stores each received event, with sender, message, and timestamp. + +#### 2. **Webhook Entry Point (`views.py` + `urls.py`)** + +* URL: `/webhook/` +* Accepts POST requests from Facebook. +* Uses `item_type` from the event payload to route to: + + * `handle_comment_event` + * `handle_share_event` + +#### 3. **Webhook Logic (`webhook_handlers.py`)** + +* `verify_webhook_token`: (Not currently used in the URL) for GET verification if needed. +* `handle_comment_event`: + + * Ignores self-comments. + * Logs event in DB. + * Uses OpenAI assistant to generate a reply to a comment. +* `handle_share_event`: + + * Logs share event. + * Generates a thoughtful comment on the shared post (and optionally its parent). + +#### 4. **Service Layer (`services.py`)** + +* **FacebookService**: Wraps Facebook Graph API calls. + + * Retrieves page access tokens. + * Fetches post details (description, parent). + * Posts comments or replies using OpenAI-generated responses. + * Logs interaction in Neo4j via `Neo4jDatabase.store_interaction(...)`. + +--- + +### ๐Ÿค– **AI Integration** + +* Uses `OpenAIAssistant` from `pxy_openai` to generate context-aware replies based on: + + * Original message or description of the post. + * The assistant's configured personality or style. +* Bot personas like โ€œDr. Dr. Ekaropolusโ€ are used to create engaging, scientifically-oriented content. + +--- + +### ๐Ÿ”— **Data Flow** + +``` +Facebook Webhook โ”€โ”€โ–ถ /webhook/ + โ””โ”€โ–ถ parse payload + โ””โ”€โ–ถ handle_comment_event() + โ”œโ”€โ–ถ store in DB + โ”œโ”€โ–ถ generate reply via OpenAI + โ””โ”€โ–ถ post to Facebook + log to Neo4j +``` + +--- + +### ๐Ÿ—‚๏ธ **Persistence** + +* Uses Django ORM to store: + + * Page-assistant bindings. + * Events (comments, shares). +* Logs interactions into a Neo4j graph DB for tracing conversations or influence paths. + diff --git a/pxy_meta_pages/services.py b/pxy_meta_pages/services.py index 11e4dcb..02e6da8 100644 --- a/pxy_meta_pages/services.py +++ b/pxy_meta_pages/services.py @@ -53,7 +53,7 @@ class FacebookService: logger.error(f"Error fetching Page Access Token: {e}") return None - def post_comment_on_share(self, page_id, post_id, message): + def post_comment_on_share(self, page_id, post_id, message, sender_id=None): """ Posts a comment on a shared post using the Facebook API. Fetches post details (description, parent_id) to improve the comment. @@ -106,8 +106,9 @@ class FacebookService: openai_service = OpenAIService(name=openai_assistant_model.name) bot_response = openai_service.handle_message(prompt) + sender_id = page_id # Post a comment on the shared post - shared_comment_response = self._post_facebook_comment(post_id, bot_response, page_access_token) + shared_comment_response = self._post_facebook_comment(post_id, bot_response, page_access_token, sender_id) # If the comment on the shared post was successful, store in Neo4j if shared_comment_response: @@ -154,10 +155,15 @@ class FacebookService: logger.error(f"Failed to fetch post details for {post_id}. Error: {e}") return {} - def _post_facebook_comment(self, post_id, message, access_token): + def _post_facebook_comment(self, post_id, message, access_token, sender_id=None): """ Helper function to post a comment to a specific post. """ + # Prepend mention if sender_id provided + if sender_id: + mention = f"@[{sender_id}:0] " + message = f"{mention}{message}" + url = f"{self.base_url}/{post_id}/comments" payload = {"message": message, "access_token": access_token} try: