diff --git a/pxy_meta_pages/services.py b/pxy_meta_pages/services.py index 7522763..9b857bf 100644 --- a/pxy_meta_pages/services.py +++ b/pxy_meta_pages/services.py @@ -71,10 +71,11 @@ class FacebookService: logger.error(f"Failed to retrieve post details for post ID: {post_id}") return None - description = post_details.get("description", "") + description = post_details.get("parent_message", None) parent_id = post_details.get("parent_id", None) author_page_id = post_details.get("parent_from_id", None) + # Fetch the appropriate OpenAI assistant for the page try: page_assistant = FacebookPageAssistant.objects.get(page_id=page_id) @@ -142,56 +143,61 @@ class FacebookService: def get_post_details(self, post_id, access_token): """ Retrieves details of a post, including: - - description (from attachments) - - parent_id (if it’s a share) - - from_id (author of THIS post) - - parent_from_id (author of the ORIGINAL post, if shared) + - message (the post’s own text) + - description (from attachments) + - parent_id (if it’s a share) + - 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: - response = requests.get(url) - response.raise_for_status() - data = response.json() + # 1st call: get this post’s text, description, parent_id, and author + fields = ( + "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 "" parent_id = data.get("parent_id") from_id = data.get("from", {}).get("id") - # default if there is no parent - parent_from_id = None + # Defaults if there is no parent + 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: - parent_url = ( - f"{self.base_url}/{parent_id}" - "?fields=from" - f"&access_token={access_token}" - ) + parent_fields = "from,message" + parent_url = f"{self.base_url}/{parent_id}?fields={parent_fields}&access_token={access_token}" p_resp = requests.get(parent_url) p_resp.raise_for_status() 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 { - "description": description, - "parent_id": parent_id, - "from_id": from_id, - "parent_from_id": parent_from_id + "message": message, + "description": description, + "parent_id": parent_id, + "from_id": from_id, + "parent_from_id": parent_from_id, + "parent_message": parent_message, } 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 {} + def _post_facebook_comment(self, post_id, message, access_token, sender_id=None): """ Helper function to post a comment to a specific post.