add storage option wit neo4j and postgress DB
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
b77133fe83
commit
0c55724caa
@ -4,6 +4,7 @@ import logging
|
||||
from .models import FacebookPageAssistant
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from pxy_neo4j.neo4j_connector import Neo4jDatabase
|
||||
from .storage_backends import Neo4jStorage, PostgresStorage
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -18,6 +19,10 @@ class FacebookService:
|
||||
self.facebook_api_version = facebook_api_version
|
||||
self.base_url = f"https://graph.facebook.com/{self.facebook_api_version}"
|
||||
self.neo4j_db = Neo4jDatabase() # Initialize Neo4j connection
|
||||
self._storages = [
|
||||
Neo4jStorage(), # always log to Neo4j
|
||||
PostgresStorage(), # always log to Postgres
|
||||
]
|
||||
|
||||
def reply_to_comment(self, page_id, comment_id, message):
|
||||
"""
|
||||
@ -106,10 +111,12 @@ class FacebookService:
|
||||
)
|
||||
if shared:
|
||||
self._store_interaction(
|
||||
page_id,
|
||||
user_message=description or "Shared post comment",
|
||||
bot_response=bot_response,
|
||||
platform="Facebook"
|
||||
page_id = page_id,
|
||||
object_id = post_id,
|
||||
parent_object_id = parent_id,
|
||||
prompt = prompt,
|
||||
bot_response = bot_response,
|
||||
platform = "Facebook"
|
||||
)
|
||||
|
||||
# 6) Post on original post
|
||||
@ -117,10 +124,12 @@ class FacebookService:
|
||||
orig = self._post_facebook_comment(parent_id, bot_response, page_access_token)
|
||||
if orig:
|
||||
self._store_interaction(
|
||||
page_id,
|
||||
user_message=description or "Original post comment",
|
||||
bot_response=bot_response,
|
||||
platform="Facebook (Original Post)"
|
||||
page_id = page_id,
|
||||
object_id = post_id,
|
||||
parent_object_id = parent_id,
|
||||
prompt = prompt,
|
||||
bot_response = bot_response,
|
||||
platform = "Facebook"
|
||||
)
|
||||
|
||||
return shared
|
||||
@ -153,23 +162,24 @@ class FacebookService:
|
||||
def _store_interaction(
|
||||
self,
|
||||
page_id: str,
|
||||
user_message: str,
|
||||
object_id: str,
|
||||
parent_object_id: str | None,
|
||||
prompt: str,
|
||||
bot_response: str,
|
||||
platform: str
|
||||
) -> None:
|
||||
"""
|
||||
Encapsulates the repeated Neo4j store_interaction calls.
|
||||
Dispatch this interaction to all registered storage backends.
|
||||
"""
|
||||
try:
|
||||
self.neo4j_db.store_interaction(
|
||||
user_id = f"fb_bot_{page_id}",
|
||||
bot_id = f"fb_bot_{page_id}",
|
||||
user_message = user_message,
|
||||
bot_response = bot_response,
|
||||
platform = platform
|
||||
for backend in self._storages:
|
||||
backend.store(
|
||||
page_id=page_id,
|
||||
object_id=object_id,
|
||||
parent_object_id=parent_object_id,
|
||||
prompt=prompt,
|
||||
bot_response=bot_response,
|
||||
platform=platform
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error logging interaction for page {page_id}: {e}")
|
||||
|
||||
|
||||
|
||||
|
46
pxy_meta_pages/storage_backends.py
Normal file
46
pxy_meta_pages/storage_backends.py
Normal file
@ -0,0 +1,46 @@
|
||||
from .models import BotInteraction, FacebookPageAssistant
|
||||
from pxy_neo4j.neo4j_connector import Neo4jDatabase
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class InteractionStorageBackend:
|
||||
"""Interface: implement `.store(...)`."""
|
||||
def store(self, *, page_id, object_id, parent_object_id,
|
||||
prompt, bot_response, platform):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class Neo4jStorage(InteractionStorageBackend):
|
||||
def __init__(self):
|
||||
self.neo4j_db = Neo4jDatabase()
|
||||
|
||||
def store(self, *, page_id, object_id, parent_object_id,
|
||||
prompt, bot_response, platform):
|
||||
try:
|
||||
self.neo4j_db.store_interaction(
|
||||
user_id = f"fb_bot_{page_id}",
|
||||
bot_id = f"fb_bot_{page_id}",
|
||||
user_message = prompt,
|
||||
bot_response = bot_response,
|
||||
platform = platform
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"[Neo4j] Error saving interaction: {e}")
|
||||
|
||||
|
||||
class PostgresStorage(InteractionStorageBackend):
|
||||
def store(self, *, page_id, object_id, parent_object_id,
|
||||
prompt, bot_response, platform):
|
||||
try:
|
||||
page = FacebookPageAssistant.objects.get(page_id=page_id)
|
||||
BotInteraction.objects.create(
|
||||
page = page,
|
||||
object_id = object_id or "",
|
||||
parent_object_id = parent_object_id,
|
||||
prompt = prompt,
|
||||
bot_response = bot_response,
|
||||
platform = platform
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"[Postgres] Error saving BotInteraction: {e}")
|
Loading…
x
Reference in New Issue
Block a user