genting the text form the original post
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
7290e575f6
commit
71a6dd2767
@ -71,10 +71,11 @@ class FacebookService:
|
|||||||
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("description", "")
|
description = post_details.get("parent_message", None)
|
||||||
parent_id = post_details.get("parent_id", None)
|
parent_id = post_details.get("parent_id", None)
|
||||||
author_page_id = post_details.get("parent_from_id", None)
|
author_page_id = post_details.get("parent_from_id", None)
|
||||||
|
|
||||||
|
|
||||||
# Fetch the appropriate OpenAI assistant for the page
|
# Fetch the appropriate OpenAI assistant for the page
|
||||||
try:
|
try:
|
||||||
page_assistant = FacebookPageAssistant.objects.get(page_id=page_id)
|
page_assistant = FacebookPageAssistant.objects.get(page_id=page_id)
|
||||||
@ -142,56 +143,61 @@ 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:
|
Retrieves details of a post, including:
|
||||||
- description (from attachments)
|
- message (the post’s own text)
|
||||||
- parent_id (if it’s a share)
|
- description (from attachments)
|
||||||
- from_id (author of THIS post)
|
- parent_id (if it’s a share)
|
||||||
- parent_from_id (author of the ORIGINAL post, if shared)
|
- from_id (author of THIS post)
|
||||||
|
- parent_from_id (author of the ORIGINAL post, if shared)
|
||||||
|
- parent_message (text of the ORIGINAL post, if shared)
|
||||||
"""
|
"""
|
||||||
# 1st call: get this post’s description, parent_id, and its own from()
|
|
||||||
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)
|
# 1st call: get this post’s text, description, parent_id, and author
|
||||||
response.raise_for_status()
|
fields = (
|
||||||
data = response.json()
|
"message,"
|
||||||
|
"attachments.limit(10){description,media,media_type,target,url},"
|
||||||
|
"parent_id,from"
|
||||||
|
)
|
||||||
|
url = f"{self.base_url}/{post_id}?fields={fields}&access_token={access_token}"
|
||||||
|
resp = requests.get(url)
|
||||||
|
resp.raise_for_status()
|
||||||
|
data = resp.json()
|
||||||
|
|
||||||
attachments = data.get("attachments", {}).get("data", [{}])
|
message = data.get("message", "")
|
||||||
|
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")
|
parent_id = data.get("parent_id")
|
||||||
from_id = data.get("from", {}).get("id")
|
from_id = data.get("from", {}).get("id")
|
||||||
|
|
||||||
# default if there is no parent
|
# Defaults if there is no parent
|
||||||
parent_from_id = None
|
parent_from_id = None
|
||||||
|
parent_message = None
|
||||||
|
|
||||||
# If this is a share, fetch the ORIGINAL post’s author
|
# 2nd call: if this is a share, fetch the ORIGINAL post’s author and text
|
||||||
if parent_id:
|
if parent_id:
|
||||||
parent_url = (
|
parent_fields = "from,message"
|
||||||
f"{self.base_url}/{parent_id}"
|
parent_url = f"{self.base_url}/{parent_id}?fields={parent_fields}&access_token={access_token}"
|
||||||
"?fields=from"
|
|
||||||
f"&access_token={access_token}"
|
|
||||||
)
|
|
||||||
p_resp = requests.get(parent_url)
|
p_resp = requests.get(parent_url)
|
||||||
p_resp.raise_for_status()
|
p_resp.raise_for_status()
|
||||||
p_data = p_resp.json()
|
p_data = p_resp.json()
|
||||||
parent_from_id = p_data.get("from", {}).get("id")
|
parent_from_id = p_data.get("from", {}).get("id")
|
||||||
|
parent_message = p_data.get("message", "")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"description": description,
|
"message": message,
|
||||||
"parent_id": parent_id,
|
"description": description,
|
||||||
"from_id": from_id,
|
"parent_id": parent_id,
|
||||||
"parent_from_id": parent_from_id
|
"from_id": from_id,
|
||||||
|
"parent_from_id": parent_from_id,
|
||||||
|
"parent_message": parent_message,
|
||||||
}
|
}
|
||||||
|
|
||||||
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}: {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