Skip to content

Commit

Permalink
feature: add route to create library template messages
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Feb 5, 2025
1 parent 5d27767 commit a7ce399
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions marketplace/clients/facebook/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ def create_template_message(

return response.json()

def create_library_template_message(
self, waba_id: str, template_data: dict
) -> dict:
url = f"{self.get_url}/{waba_id}/message_templates"
response = self.make_request(
url, method="POST", json=template_data, headers=self._get_headers()
)

return response.json()

def get_template_analytics(self, waba_id, fields):
url = f"{self.get_url}/{waba_id}/template_analytics"
headers = self._get_headers()
Expand Down
6 changes: 6 additions & 0 deletions marketplace/interfaces/facebook/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def update_template_message(
def delete_template_message(self, waba_id: str, name: str) -> Dict[str, Any]:
pass

@abstractmethod
def create_library_template_message(
self, waba_id: str, template_data: dict
) -> Dict[str, Any]:
pass


class PhotoAPIRequestsInterface(ABC):
@abstractmethod
Expand Down
12 changes: 12 additions & 0 deletions marketplace/services/facebook/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import List, Dict, Any

from marketplace.applications.models import App
from marketplace.wpp_products.models import Catalog
from marketplace.interfaces.facebook.interfaces import (
TemplatesRequestsInterface,
Expand Down Expand Up @@ -251,6 +252,17 @@ def _clean_text(self, text):
text = text.replace("\xa0", " ")
return text.strip()

def create_library_template_message(
self,
app: App,
template_data: dict,
) -> Dict[str, Any]:
waba_id = app.config["wa_waba_id"]
return self.client.create_library_template_message(
waba_id=waba_id,
template_data=template_data,
)


class PhotoAPIService:
def __init__(self, client: PhotoAPIRequestsInterface):
Expand Down
36 changes: 36 additions & 0 deletions marketplace/wpp_templates/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import get_object_or_404

from marketplace.applications.models import App
from marketplace.core.types import APPTYPES
from marketplace.core.types.channels.whatsapp_base.mixins import QueryParamsParser
from marketplace.services.facebook.service import TemplateService, PhotoAPIService
from marketplace.clients.facebook.client import FacebookClient
from marketplace.accounts.permissions import ProjectManagePermission
from marketplace.clients.exceptions import CustomAPIException

from .models import TemplateHeader, TemplateMessage, TemplateTranslation, TemplateButton
from .serializers import TemplateMessageSerializer, TemplateTranslationSerializer
Expand Down Expand Up @@ -256,3 +258,37 @@ def partial_update(self, request, app_uuid=None, uuid=None):
)

return Response(response)

@action(detail=False, methods=["POST"])
def create_library_templates(self, request, app_uuid=None, uuid=None):
"""
Creates a library template message for the given app.
Args:
request (Request): The request object containing template data.
app_uuid (str): UUID of the app.
uuid (str, optional): Additional UUID parameter, not used in the function.
Returns:
Response: API response with the template creation status.
"""
app = get_object_or_404(App, uuid=app_uuid)

try:
# Instantiate the template service with a Facebook client
template_service = TemplateService(
client=FacebookClient(app.apptype.get_system_access_token(app))
)

# Create the template using the provided request data
response_data = template_service.create_library_template_message(
app=app, template_data=request.data
)

return Response(response_data, status=status.HTTP_201_CREATED)

except KeyError as e:
raise ValidationError(f"Missing configuration: {e}")

except CustomAPIException as e:
return Response({"detail": str(e)}, status=e.status_code)

0 comments on commit a7ce399

Please sign in to comment.