From d6de05877760651ea9b9b18bda945751aaa1dae4 Mon Sep 17 00:00:00 2001 From: Ekaropolus Date: Tue, 22 Jul 2025 20:52:20 -0600 Subject: [PATCH] refactoring slight --- pxy_meta_pages/services.py | 269 +++++++++++++++++++------------------ 1 file changed, 136 insertions(+), 133 deletions(-) diff --git a/pxy_meta_pages/services.py b/pxy_meta_pages/services.py index 0678726..c10923f 100644 --- a/pxy_meta_pages/services.py +++ b/pxy_meta_pages/services.py @@ -19,6 +19,138 @@ class FacebookService: self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}" self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection + def reply_to_comment(self, page_id, comment_id, message): + """ + Replies to a specific comment using the Facebook API and OpenAI Assistant. + """ + # Retrieve the Page Access Token dynamically + page_access_token = self._get_page_access_token(page_id) + if not page_access_token: + logger.error(f"Unable to retrieve access token for page ID: {page_id}") + return None + + # Fetch the appropriate OpenAI assistant for the page + try: + page_assistant = FacebookPageAssistant.objects.get(page_id=page_id) + openai_assistant_model = page_assistant.assistant + 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 + # Use a default message if the received message is empty + if not message or message.strip() == "": + message = "Thank you for sharing this comment! What do you think about it?" + + openai_service = OpenAIService(name=openai_assistant_model.name) # Pass the model's name to the service + bot_response = openai_service.handle_message(message) + + # Send the response to Facebook + url = f"{self.base_url}/{comment_id}/comments" + payload = {"message": bot_response, "access_token": page_access_token} + try: + response = requests.post(url, data=payload) + response.raise_for_status() + logger.info(f"Replied to comment ID: {comment_id}") + # Store the interaction in Neo4j + self.neo4j_db.store_interaction( + user_id=f"fb_user_{comment_id}", + bot_id=f"fb_bot_{page_id}", + user_message=message, + bot_response=bot_response, + platform="Facebook" + ) + return response.json() + except requests.exceptions.RequestException as e: + logger.error(f"Failed to reply to comment ID: {comment_id}. Error: {e}") + return 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. + Fetches post details (description, parent_id) to improve the comment. + If parent_id exists, posts the same comment on the original post. + """ + # Retrieve the Page Access Token dynamically + page_access_token = self._get_page_access_token(page_id) + if not page_access_token: + logger.error(f"Unable to retrieve access token for page ID: {page_id}") + return None + + # Fetch post details (description, parent_id) + post_details = self._get_post_details(post_id, page_access_token) + if not post_details: + logger.error(f"Failed to retrieve post details for post ID: {post_id}") + return None + + 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) + openai_assistant_model = page_assistant.assistant + 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 + if not message or message.strip() == "": + if description: + prompt = ( + 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: + prompt = "Say something truly inspiring about science, a fact or idea that will amaze people." + + else: + if description: + prompt = ( + 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: + 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, + platform="Facebook" + ) + + # If parent_id exists and the first comment was successful, post the same comment on the original post + 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 + if original_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 "Original post comment", + bot_response=bot_response, + platform="Facebook (Original Post)" + ) + + return shared_comment_response + + def get_system_user_id(self): """ Retrieves the system user ID using the user access token. @@ -29,7 +161,7 @@ class FacebookService: logger.error(f"Error fetching system user ID: {e}") return None - def get_page_access_token(self, page_id): + def _get_page_access_token(self, page_id): """ Retrieves the Page Access Token for a specific Page ID. """ @@ -52,94 +184,8 @@ class FacebookService: except requests.exceptions.RequestException as e: logger.error(f"Error fetching Page Access Token: {e}") return 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. - Fetches post details (description, parent_id) to improve the comment. - If parent_id exists, posts the same comment on the original post. - """ - # Retrieve the Page Access Token dynamically - page_access_token = self.get_page_access_token(page_id) - if not page_access_token: - logger.error(f"Unable to retrieve access token for page ID: {page_id}") - return None - - # Fetch post details (description, parent_id) - post_details = self.get_post_details(post_id, page_access_token) - if not post_details: - logger.error(f"Failed to retrieve post details for post ID: {post_id}") - return None - - 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) - openai_assistant_model = page_assistant.assistant - 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 - if not message or message.strip() == "": - if description: - prompt = ( - 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: - prompt = "Say something truly inspiring about science, a fact or idea that will amaze people." - - else: - if description: - prompt = ( - 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: - 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, - platform="Facebook" - ) - - # If parent_id exists and the first comment was successful, post the same comment on the original post - 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 - if original_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 "Original post comment", - bot_response=bot_response, - platform="Facebook (Original Post)" - ) - - return shared_comment_response - - def get_post_details(self, post_id, access_token): + + def _get_post_details(self, post_id, access_token): """ Retrieves details of a post, including: - message (the post’s own text) @@ -222,47 +268,4 @@ class FacebookService: logger.error(f"Failed to comment on post ID: {post_id}. Error: {e}") return None - def reply_to_comment(self, page_id, comment_id, message): - """ - Replies to a specific comment using the Facebook API and OpenAI Assistant. - """ - # Retrieve the Page Access Token dynamically - page_access_token = self.get_page_access_token(page_id) - if not page_access_token: - logger.error(f"Unable to retrieve access token for page ID: {page_id}") - return None - - # Fetch the appropriate OpenAI assistant for the page - try: - page_assistant = FacebookPageAssistant.objects.get(page_id=page_id) - openai_assistant_model = page_assistant.assistant - 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 - # Use a default message if the received message is empty - if not message or message.strip() == "": - message = "Thank you for sharing this comment! What do you think about it?" - - openai_service = OpenAIService(name=openai_assistant_model.name) # Pass the model's name to the service - bot_response = openai_service.handle_message(message) - - # Send the response to Facebook - url = f"{self.base_url}/{comment_id}/comments" - payload = {"message": bot_response, "access_token": page_access_token} - try: - response = requests.post(url, data=payload) - response.raise_for_status() - logger.info(f"Replied to comment ID: {comment_id}") - # Store the interaction in Neo4j - self.neo4j_db.store_interaction( - user_id=f"fb_user_{comment_id}", - bot_id=f"fb_bot_{page_id}", - user_message=message, - bot_response=bot_response, - platform="Facebook" - ) - return response.json() - except requests.exceptions.RequestException as e: - logger.error(f"Failed to reply to comment ID: {comment_id}. Error: {e}") - return None + \ No newline at end of file