hadler event for comments in fb agents
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
db72590b8d
commit
27b940284d
@ -1,14 +1,18 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.http import JsonResponse, HttpResponse
|
||||
from .services import FacebookService
|
||||
from django.conf import settings
|
||||
|
||||
from .services import FacebookService
|
||||
from .models import FacebookPageAssistant, EventType, FacebookEvent
|
||||
|
||||
# Configure logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
PAGE_ACCESS_TOKEN = settings.PAGE_ACCESS_TOKEN
|
||||
|
||||
|
||||
def verify_webhook_token(mode, token, challenge, verify_token):
|
||||
"""
|
||||
Verifies the webhook token and mode.
|
||||
@ -37,8 +41,11 @@ def parse_webhook_payload(payload):
|
||||
|
||||
def handle_comment_event(page_id, sender_id, data):
|
||||
"""
|
||||
Handles incoming comment events.
|
||||
Handles incoming comment events:
|
||||
1) Persiste el evento en la BD.
|
||||
2) Responde al comentario vía FacebookService.
|
||||
"""
|
||||
# Si el comentario viene de la propia página, lo saltamos
|
||||
if sender_id == page_id:
|
||||
logger.info(f"Skipping self-generated comment. Sender ID: {sender_id}, Page ID: {page_id}")
|
||||
return JsonResponse({"status": "skipped"}, status=200)
|
||||
@ -46,26 +53,63 @@ def handle_comment_event(page_id, sender_id, data):
|
||||
comment_id = data.get("comment_id")
|
||||
original_message = data.get("message")
|
||||
|
||||
# 1) Registrar en la base de datos
|
||||
try:
|
||||
page = FacebookPageAssistant.objects.get(page_id=page_id)
|
||||
et = EventType.objects.get(code="comment")
|
||||
FacebookEvent.objects.create(
|
||||
page=page,
|
||||
event_type=et,
|
||||
sender_id=sender_id,
|
||||
object_id=comment_id,
|
||||
message=original_message
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error logging FB comment event: {e}")
|
||||
|
||||
# 2) Responder al comentario via API de Facebook
|
||||
if comment_id and original_message:
|
||||
fb_service = FacebookService(PAGE_ACCESS_TOKEN)
|
||||
fb_service.reply_to_comment(page_id, comment_id, original_message)
|
||||
logger.info(f"Successfully replied to comment ID: {comment_id} with bot response: {original_message}")
|
||||
try:
|
||||
fb_service = FacebookService(PAGE_ACCESS_TOKEN)
|
||||
fb_service.reply_to_comment(page_id, comment_id, original_message)
|
||||
logger.info(f"Successfully replied to comment ID: {comment_id} with bot response: {original_message}")
|
||||
except Exception as e:
|
||||
logger.error(f"Error replying to comment via FacebookService: {e}")
|
||||
|
||||
return JsonResponse({"status": "comment_handled"}, status=200)
|
||||
|
||||
|
||||
def handle_share_event(page_id, data):
|
||||
"""
|
||||
Handles incoming share events.
|
||||
Handles incoming share events:
|
||||
1) Persiste el evento en la BD.
|
||||
2) Publica un comentario en la share vía FacebookService.
|
||||
"""
|
||||
share_link = data.get("link")
|
||||
post_id = data.get("post_id")
|
||||
share_id = data.get("share_id")
|
||||
post_id = data.get("post_id")
|
||||
original_message = data.get("message")
|
||||
share_link = data.get("link")
|
||||
|
||||
logger.info(f"Post {post_id} was shared. Share ID: {share_id}, Link: {share_link}")
|
||||
|
||||
fb_service = FacebookService(PAGE_ACCESS_TOKEN)
|
||||
fb_service.post_comment_on_share(page_id, post_id, original_message)
|
||||
# 1) Registrar en la base de datos
|
||||
try:
|
||||
page = FacebookPageAssistant.objects.get(page_id=page_id)
|
||||
et = EventType.objects.get(code="share")
|
||||
FacebookEvent.objects.create(
|
||||
page=page,
|
||||
event_type=et,
|
||||
object_id=share_id,
|
||||
message=original_message
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error logging FB share event: {e}")
|
||||
|
||||
# 2) Comentar en la publicación compartida via API de Facebook
|
||||
try:
|
||||
fb_service = FacebookService(PAGE_ACCESS_TOKEN)
|
||||
fb_service.post_comment_on_share(page_id, post_id, original_message)
|
||||
except Exception as e:
|
||||
logger.error(f"Error posting comment on share via FacebookService: {e}")
|
||||
|
||||
return JsonResponse({"status": "share_logged"}, status=200)
|
||||
|
Loading…
x
Reference in New Issue
Block a user