This commit is contained in:
parent
03906fe90b
commit
d6de058777
@ -19,38 +19,49 @@ class FacebookService:
|
||||
self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}"
|
||||
self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection
|
||||
|
||||
def get_system_user_id(self):
|
||||
def reply_to_comment(self, page_id, comment_id, message):
|
||||
"""
|
||||
Retrieves the system user ID using the user access token.
|
||||
Replies to a specific comment using the Facebook API and OpenAI Assistant.
|
||||
"""
|
||||
try:
|
||||
return '122106889202727657'
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"Error fetching system user ID: {e}")
|
||||
# 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
|
||||
|
||||
def get_page_access_token(self, page_id):
|
||||
"""
|
||||
Retrieves the Page Access Token for a specific Page ID.
|
||||
"""
|
||||
url = f"{self.base_url}/122106889202727657/accounts?access_token={self.user_access_token}"
|
||||
# Fetch the appropriate OpenAI assistant for the page
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
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?"
|
||||
|
||||
if "data" in data:
|
||||
for page in data["data"]:
|
||||
if page.get("id") == str(page_id):
|
||||
page_name = page.get("name", "Unknown")
|
||||
access_token = page.get("access_token", "No Token")
|
||||
logger.info(f"Retrieved access token for page {page_id}: {page_name}")
|
||||
return access_token
|
||||
logger.error(f"Error: Page ID {page_id} not found.")
|
||||
else:
|
||||
logger.error("Error: Unexpected response format from Facebook API.")
|
||||
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"Error fetching Page Access Token: {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):
|
||||
@ -60,13 +71,13 @@ class FacebookService:
|
||||
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)
|
||||
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)
|
||||
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
|
||||
@ -139,7 +150,42 @@ class FacebookService:
|
||||
|
||||
return shared_comment_response
|
||||
|
||||
def get_post_details(self, post_id, access_token):
|
||||
|
||||
def get_system_user_id(self):
|
||||
"""
|
||||
Retrieves the system user ID using the user access token.
|
||||
"""
|
||||
try:
|
||||
return '122106889202727657'
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"Error fetching system user ID: {e}")
|
||||
return None
|
||||
|
||||
def _get_page_access_token(self, page_id):
|
||||
"""
|
||||
Retrieves the Page Access Token for a specific Page ID.
|
||||
"""
|
||||
url = f"{self.base_url}/122106889202727657/accounts?access_token={self.user_access_token}"
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
if "data" in data:
|
||||
for page in data["data"]:
|
||||
if page.get("id") == str(page_id):
|
||||
page_name = page.get("name", "Unknown")
|
||||
access_token = page.get("access_token", "No Token")
|
||||
logger.info(f"Retrieved access token for page {page_id}: {page_name}")
|
||||
return access_token
|
||||
logger.error(f"Error: Page ID {page_id} not found.")
|
||||
else:
|
||||
logger.error("Error: Unexpected response format from Facebook API.")
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"Error fetching Page Access Token: {e}")
|
||||
return None
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user