diff --git a/pxy_meta_pages/services.py b/pxy_meta_pages/services.py index 5ec0f59..5f92214 100644 --- a/pxy_meta_pages/services.py +++ b/pxy_meta_pages/services.py @@ -73,6 +73,7 @@ class FacebookService: description = post_details.get("description", "") 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 try: @@ -106,7 +107,7 @@ class FacebookService: openai_service = OpenAIService(name=openai_assistant_model.name) bot_response = openai_service.handle_message(prompt) - sender_id = parent_id + 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) @@ -140,21 +141,34 @@ class FacebookService: 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: response = requests.get(url) response.raise_for_status() data = response.json() + attachments = data.get("attachments", {}).get("data", [{}]) description = attachments[0].get("description", "") if attachments else "" 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: logger.error(f"Failed to fetch post details for {post_id}. Error: {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.