Skip to content

Commit

Permalink
feat: add dynamic url to message template button
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Jan 7, 2025
1 parent 0ef9b57 commit f788947
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.4 on 2025-01-07 17:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("wpp_templates", "0007_alter_templatetranslation_message_template_id"),
]

operations = [
migrations.AddField(
model_name="templatebutton",
name="example",
field=models.CharField(default=None, max_length=2000, null=True),
),
]
1 change: 1 addition & 0 deletions marketplace/wpp_templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class TemplateButton(models.Model):
country_code = models.IntegerField(null=True)
phone_number = models.CharField(max_length=20, null=True)
url = models.CharField(max_length=2000, null=True)
example = models.CharField(max_length=2000, default=None, null=True)


class TemplateHeader(models.Model):
Expand Down
20 changes: 14 additions & 6 deletions marketplace/wpp_templates/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ class ButtonSerializer(serializers.ModelSerializer):
country_code = serializers.CharField(required=False)
phone_number = serializers.CharField(required=False)
url = serializers.CharField(required=False)
example = serializers.ListField(required=False)

class Meta:
model = TemplateButton
fields = ["button_type", "text", "country_code", "phone_number", "url"]
fields = [
"button_type",
"text",
"country_code",
"phone_number",
"url",
"example",
]


class TemplateTranslationSerializer(serializers.Serializer):
Expand Down Expand Up @@ -81,17 +89,15 @@ def create(self, validated_data: dict) -> None:
components = [validated_data.get("body", {})]
header = validated_data.get("header")

# Process Header
if header:
header = dict(header)
header["type"] = "HEADER"
header["format"] = header.get("header_type", "TEXT")
header.pop("header_type")

if (
header.get("format") == "IMAGE"
or header.get("format") == "DOCUMENT"
or header.get("format") == "VIDEO"
):
# Handle Media Uploads
if header.get("format") in ["IMAGE", "DOCUMENT", "VIDEO"]:
photo_api_request = PhotoAPIService(client=FacebookClient(access_token))
photo = header.get("example")
file_type = re.search("(?<=data:)(.*)(?=;base64)", photo).group(0)
Expand All @@ -113,6 +119,7 @@ def create(self, validated_data: dict) -> None:
components = self.append_to_components(components, validated_data.get("footer"))
buttons = validated_data.get("buttons", {})

# Process Buttons
buttons_component = {
"type": "BUTTONS",
"buttons": [],
Expand All @@ -121,6 +128,7 @@ def create(self, validated_data: dict) -> None:
for button in buttons:
button = dict(button)
button["type"] = button.get("button_type")

if button.get("phone_number"):
button[
"phone_number"
Expand Down

0 comments on commit f788947

Please sign in to comment.