102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
from django.db import models
|
||
from pxy_openai.assistants import OpenAIAssistant
|
||
|
||
class FacebookPageAssistant(models.Model):
|
||
"""
|
||
Model to link a Facebook page to an OpenAI assistant and manage webhook subscriptions.
|
||
"""
|
||
page_id = models.CharField(max_length=100, unique=True, help_text="Unique ID of the Facebook page")
|
||
page_name = models.CharField(max_length=200, help_text="Name of the Facebook page")
|
||
assistant = models.ForeignKey('pxy_openai.OpenAIAssistant', on_delete=models.CASCADE, related_name="facebook_pages")
|
||
is_subscribed = models.BooleanField(default=False, help_text="Indicates if the page is subscribed to webhooks")
|
||
created_at = models.DateTimeField(auto_now_add=True)
|
||
updated_at = models.DateTimeField(auto_now=True)
|
||
|
||
def __str__(self):
|
||
return f"{self.page_name} ({self.page_id}) -> {self.assistant.name}"
|
||
|
||
class EventType(models.Model):
|
||
"""
|
||
Catalog of possible event types for FacebookPageAssistant.
|
||
"""
|
||
code = models.CharField(
|
||
max_length=50,
|
||
unique=True,
|
||
help_text="Machine-readable code for the event type (e.g. 'comment', 'share')"
|
||
)
|
||
label = models.CharField(
|
||
max_length=100,
|
||
help_text="Human-readable label for the event type"
|
||
)
|
||
|
||
def __str__(self):
|
||
return self.label
|
||
|
||
|
||
class FacebookEvent(models.Model):
|
||
"""
|
||
Log of individual events (comments, shares, etc.) for a Facebook page.
|
||
"""
|
||
page = models.ForeignKey(
|
||
FacebookPageAssistant,
|
||
on_delete=models.CASCADE,
|
||
related_name="events"
|
||
)
|
||
event_type = models.ForeignKey(
|
||
EventType,
|
||
on_delete=models.PROTECT,
|
||
related_name="facebook_events"
|
||
)
|
||
sender_id = models.CharField(
|
||
max_length=100,
|
||
blank=True, null=True,
|
||
help_text="ID of the user who triggered the event"
|
||
)
|
||
object_id = models.CharField(
|
||
max_length=100,
|
||
help_text="Identifier of the comment or share object"
|
||
)
|
||
message = models.TextField(
|
||
blank=True, null=True,
|
||
help_text="Text content (for comments) or message attached to the event"
|
||
)
|
||
timestamp = models.DateTimeField(auto_now_add=True)
|
||
|
||
def __str__(self):
|
||
return f"{self.page.page_name}: {self.event_type.code} @ {self.timestamp:%Y-%m-%d %H:%M}"
|
||
|
||
|
||
class BotInteraction(models.Model):
|
||
"""
|
||
Stores each AI‐generated comment: which object we replied to, the prompt,
|
||
the response, and link back to the original (parent) post if any.
|
||
"""
|
||
page = models.ForeignKey(
|
||
FacebookPageAssistant,
|
||
on_delete=models.CASCADE,
|
||
related_name="bot_interactions"
|
||
)
|
||
object_id = models.CharField(
|
||
max_length=100,
|
||
help_text="The Facebook post or comment ID that we commented on"
|
||
)
|
||
parent_object_id = models.CharField(
|
||
max_length=100,
|
||
blank=True,
|
||
null=True,
|
||
help_text="If this comment was on a share, the original post’s ID"
|
||
)
|
||
prompt = models.TextField(
|
||
help_text="Exact prompt sent to OpenAI for this comment"
|
||
)
|
||
bot_response = models.TextField(
|
||
help_text="The text that the bot actually posted"
|
||
)
|
||
platform = models.CharField(
|
||
max_length=50,
|
||
help_text="E.g. 'Facebook' or 'Facebook (Original Post)'"
|
||
)
|
||
created_at = models.DateTimeField(auto_now_add=True)
|
||
|
||
def __str__(self):
|
||
return f"{self.page.page_name} → {self.object_id} @ {self.created_at:%Y-%m-%d %H:%M}" |