Adding sender name to comments on page share fb
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
41fdd96225
commit
22840c0102
78
pxy_meta_pages/README.md
Normal file
78
pxy_meta_pages/README.md
Normal file
@ -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.
|
||||||
|
|
@ -53,7 +53,7 @@ class FacebookService:
|
|||||||
logger.error(f"Error fetching Page Access Token: {e}")
|
logger.error(f"Error fetching Page Access Token: {e}")
|
||||||
return None
|
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.
|
Posts a comment on a shared post using the Facebook API.
|
||||||
Fetches post details (description, parent_id) to improve the comment.
|
Fetches post details (description, parent_id) to improve the comment.
|
||||||
@ -106,8 +106,9 @@ class FacebookService:
|
|||||||
openai_service = OpenAIService(name=openai_assistant_model.name)
|
openai_service = OpenAIService(name=openai_assistant_model.name)
|
||||||
bot_response = openai_service.handle_message(prompt)
|
bot_response = openai_service.handle_message(prompt)
|
||||||
|
|
||||||
|
sender_id = page_id
|
||||||
# Post a comment on the shared post
|
# 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 the comment on the shared post was successful, store in Neo4j
|
||||||
if shared_comment_response:
|
if shared_comment_response:
|
||||||
@ -154,10 +155,15 @@ class FacebookService:
|
|||||||
logger.error(f"Failed to fetch post details for {post_id}. Error: {e}")
|
logger.error(f"Failed to fetch post details for {post_id}. Error: {e}")
|
||||||
return {}
|
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.
|
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"
|
url = f"{self.base_url}/{post_id}/comments"
|
||||||
payload = {"message": message, "access_token": access_token}
|
payload = {"message": message, "access_token": access_token}
|
||||||
try:
|
try:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user