This commit is contained in:
parent
8885f1baa1
commit
0a0dd4904e
@ -73,6 +73,7 @@ class FacebookService:
|
|||||||
|
|
||||||
description = post_details.get("description", "")
|
description = post_details.get("description", "")
|
||||||
parent_id = post_details.get("parent_id", None)
|
parent_id = post_details.get("parent_id", None)
|
||||||
|
author_page_id = post_details.get("from_id", None)
|
||||||
|
|
||||||
# Fetch the appropriate OpenAI assistant for the page
|
# Fetch the appropriate OpenAI assistant for the page
|
||||||
try:
|
try:
|
||||||
@ -106,7 +107,7 @@ 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 = parent_id
|
sender_id = author_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, sender_id)
|
shared_comment_response = self._post_facebook_comment(post_id, bot_response, page_access_token, sender_id)
|
||||||
|
|
||||||
@ -140,21 +141,34 @@ class FacebookService:
|
|||||||
|
|
||||||
def get_post_details(self, post_id, access_token):
|
def get_post_details(self, post_id, access_token):
|
||||||
"""
|
"""
|
||||||
Retrieves details of a post, including description and parent_id.
|
Retrieves details of a post, including description, parent_id, and author Page ID (from_id).
|
||||||
"""
|
"""
|
||||||
url = f"{self.base_url}/{post_id}?fields=attachments.limit(10){{description,media,media_type,target,url}},parent_id&access_token={access_token}"
|
url = (
|
||||||
|
f"{self.base_url}/{post_id}"
|
||||||
|
"?fields=attachments.limit(10){description,media,media_type,target,url},"
|
||||||
|
"parent_id,from"
|
||||||
|
f"&access_token={access_token}"
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
response = requests.get(url)
|
response = requests.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
attachments = data.get("attachments", {}).get("data", [{}])
|
attachments = data.get("attachments", {}).get("data", [{}])
|
||||||
description = attachments[0].get("description", "") if attachments else ""
|
description = attachments[0].get("description", "") if attachments else ""
|
||||||
parent_id = data.get("parent_id", None)
|
parent_id = data.get("parent_id", None)
|
||||||
return {"description": description, "parent_id": parent_id}
|
from_id = data.get("from", {}).get("id")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"description": description,
|
||||||
|
"parent_id": parent_id,
|
||||||
|
"from_id": from_id
|
||||||
|
}
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
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, sender_id=None):
|
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.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user