Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add route to create library template messages #610

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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 @@
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(

Check warning on line 261 in marketplace/services/facebook/service.py

View check run for this annotation

Codecov / codecov/patch

marketplace/services/facebook/service.py#L260-L261

Added lines #L260 - L261 were not covered by tests
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 @@
)

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)

Check warning on line 275 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L275

Added line #L275 was not covered by tests

try:

Check warning on line 277 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L277

Added line #L277 was not covered by tests
# Instantiate the template service with a Facebook client
template_service = TemplateService(

Check warning on line 279 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L279

Added line #L279 was not covered by tests
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(

Check warning on line 284 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L284

Added line #L284 was not covered by tests
app=app, template_data=request.data
)

return Response(response_data, status=status.HTTP_201_CREATED)

Check warning on line 288 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L288

Added line #L288 was not covered by tests

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

Check warning on line 291 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L290-L291

Added lines #L290 - L291 were not covered by tests

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

Check warning on line 294 in marketplace/wpp_templates/views.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/views.py#L293-L294

Added lines #L293 - L294 were not covered by tests
Loading