This commit is contained in:
parent
d6de058777
commit
d52dfe75a2
@ -65,90 +65,112 @@ class FacebookService:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def post_comment_on_share(self, page_id, post_id, message, sender_id=None):
|
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 and then on the original post.
|
||||||
Fetches post details (description, parent_id) to improve the comment.
|
"""
|
||||||
If parent_id exists, posts the same comment on the original post.
|
# 1) Fetch token
|
||||||
"""
|
page_access_token = self._get_page_access_token(page_id)
|
||||||
# Retrieve the Page Access Token dynamically
|
if not page_access_token:
|
||||||
page_access_token = self._get_page_access_token(page_id)
|
logger.error(f"Unable to retrieve access token for page ID: {page_id}")
|
||||||
if not page_access_token:
|
return None
|
||||||
logger.error(f"Unable to retrieve access token for page ID: {page_id}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Fetch post details (description, parent_id)
|
# 2) Fetch details
|
||||||
post_details = self._get_post_details(post_id, page_access_token)
|
post_details = self._get_post_details(post_id, page_access_token)
|
||||||
if not post_details:
|
if not post_details:
|
||||||
logger.error(f"Failed to retrieve post details for post ID: {post_id}")
|
logger.error(f"Failed to retrieve post details for post ID: {post_id}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
description = post_details.get("parent_message", None)
|
description = post_details.get("parent_message")
|
||||||
parent_id = post_details.get("parent_id", None)
|
parent_id = post_details.get("parent_id")
|
||||||
author_page_id = post_details.get("parent_from_id", None)
|
author_page_id = post_details.get("parent_from_id")
|
||||||
|
|
||||||
|
# 3) Load assistant
|
||||||
|
try:
|
||||||
|
page_assistant = FacebookPageAssistant.objects.get(page_id=page_id)
|
||||||
|
openai_assistant_model = page_assistant.assistant
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
logger.error(f"No assistant configured for page ID: {page_id}")
|
||||||
|
return None
|
||||||
|
|
||||||
# Fetch the appropriate OpenAI assistant for the page
|
# 4) Build prompt & get AI response
|
||||||
try:
|
prompt = self._build_prompt(message, description)
|
||||||
page_assistant = FacebookPageAssistant.objects.get(page_id=page_id)
|
openai_svc = OpenAIService(name=openai_assistant_model.name)
|
||||||
openai_assistant_model = page_assistant.assistant
|
bot_response = openai_svc.handle_message(prompt)
|
||||||
logger.info(f"Using assistant '{openai_assistant_model.name}' for page '{page_assistant.page_name}'")
|
|
||||||
except ObjectDoesNotExist:
|
|
||||||
logger.error(f"No assistant configured for page ID: {page_id}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Generate a meaningful comment based on available data
|
# 5) Post on shared post
|
||||||
if not message or message.strip() == "":
|
shared = self._post_facebook_comment(
|
||||||
if description:
|
post_id,
|
||||||
prompt = (
|
bot_response,
|
||||||
f"Dr. Dr. Ekaropolus previously said: '{description}'. "
|
page_access_token,
|
||||||
"Based on this, write an insightful response in the most appropriate language that engages people in scientific discussion."
|
sender_id=author_page_id
|
||||||
)
|
)
|
||||||
else:
|
if shared:
|
||||||
prompt = "Say something truly inspiring about science, a fact or idea that will amaze people."
|
self._store_interaction(
|
||||||
|
page_id,
|
||||||
|
user_message=description or "Shared post comment",
|
||||||
|
bot_response=bot_response,
|
||||||
|
platform="Facebook"
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
# 6) Post on original post
|
||||||
if description:
|
if parent_id and shared:
|
||||||
prompt = (
|
orig = self._post_facebook_comment(parent_id, bot_response, page_access_token)
|
||||||
f"Dr. Dr. Ekaropolus previously said: '{message}', "
|
if orig:
|
||||||
f"and the shared post describes: '{description}'. "
|
self._store_interaction(
|
||||||
"Combine these thoughts into an engaging, fun, and insightful response in the most appropriate language."
|
page_id,
|
||||||
)
|
user_message=description or "Original post comment",
|
||||||
else:
|
|
||||||
prompt = f"Dr. Dr. Ekaropolus said: '{message}'. Expand on this with an insightful scientific thought."
|
|
||||||
|
|
||||||
openai_service = OpenAIService(name=openai_assistant_model.name)
|
|
||||||
bot_response = openai_service.handle_message(prompt)
|
|
||||||
|
|
||||||
sender_id = author_page_id
|
|
||||||
# Post a comment on the shared post
|
|
||||||
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:
|
|
||||||
self.neo4j_db.store_interaction(
|
|
||||||
user_id=f"fb_bot_{page_id}",
|
|
||||||
bot_id=f"fb_bot_{page_id}",
|
|
||||||
user_message=description if description else "Shared post comment",
|
|
||||||
bot_response=bot_response,
|
bot_response=bot_response,
|
||||||
platform="Facebook"
|
platform="Facebook (Original Post)"
|
||||||
)
|
)
|
||||||
|
|
||||||
# If parent_id exists and the first comment was successful, post the same comment on the original post
|
return shared
|
||||||
if parent_id and shared_comment_response:
|
|
||||||
logger.info(f"Also commenting on the original post: {parent_id}")
|
|
||||||
original_comment_response = self._post_facebook_comment(parent_id, bot_response, page_access_token)
|
|
||||||
|
|
||||||
# If the comment on the original post was successful, store in Neo4j
|
# ─── Helper: prompt builder ────────────────────────────────────────────
|
||||||
if original_comment_response:
|
|
||||||
self.neo4j_db.store_interaction(
|
def _build_prompt(self, message: str, description: str) -> str:
|
||||||
user_id=f"fb_bot_{page_id}",
|
"""
|
||||||
bot_id=f"fb_bot_{page_id}",
|
Generate the same prompt logic you had inline.
|
||||||
user_message=description if description else "Original post comment",
|
"""
|
||||||
bot_response=bot_response,
|
if not message or message.strip() == "":
|
||||||
platform="Facebook (Original Post)"
|
if description:
|
||||||
)
|
return (
|
||||||
|
f"Dr. Dr. Ekaropolus previously said: '{description}'. "
|
||||||
|
"Based on this, write an insightful response in the most appropriate language that engages people in scientific discussion."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return "Say something truly inspiring about science, a fact or idea that will amaze people."
|
||||||
|
else:
|
||||||
|
if description:
|
||||||
|
return (
|
||||||
|
f"Dr. Dr. Ekaropolus previously said: '{message}', "
|
||||||
|
f"and the shared post describes: '{description}'. "
|
||||||
|
"Combine these thoughts into an engaging, fun, and insightful response in the most appropriate language."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return f"Dr. Dr. Ekaropolus said: '{message}'. Expand on this with an insightful scientific thought."
|
||||||
|
|
||||||
|
|
||||||
|
def _store_interaction(
|
||||||
|
self,
|
||||||
|
page_id: str,
|
||||||
|
user_message: str,
|
||||||
|
bot_response: str,
|
||||||
|
platform: str
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Encapsulates the repeated Neo4j store_interaction calls.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.neo4j_db.store_interaction(
|
||||||
|
user_id = f"fb_bot_{page_id}",
|
||||||
|
bot_id = f"fb_bot_{page_id}",
|
||||||
|
user_message = user_message,
|
||||||
|
bot_response = bot_response,
|
||||||
|
platform = platform
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error logging interaction for page {page_id}: {e}")
|
||||||
|
|
||||||
return shared_comment_response
|
|
||||||
|
|
||||||
|
|
||||||
def get_system_user_id(self):
|
def get_system_user_id(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user