Skip to content

Commit

Permalink
feature: adds on create translation new button type OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky committed Sep 15, 2023
1 parent ed3fa0b commit 2ee668f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 22 deletions.
52 changes: 52 additions & 0 deletions marketplace/wpp_templates/migrations/0007_auto_20230915_1035.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 3.2.4 on 2023-09-15 13:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("wpp_templates", "0006_templatetranslation_message_template_id"),
]

operations = [
migrations.AddField(
model_name="templatebutton",
name="autofill_text",
field=models.CharField(max_length=255, null=True),
),
migrations.AddField(
model_name="templatebutton",
name="otp_type",
field=models.CharField(
choices=[("COPY_CODE", "Copy Code"), ("ONE_TAP", "One Tap")],
max_length=10,
null=True,
),
),
migrations.AddField(
model_name="templatebutton",
name="package_name",
field=models.CharField(max_length=255, null=True),
),
migrations.AddField(
model_name="templatebutton",
name="signature_hash",
field=models.CharField(max_length=255, null=True),
),
migrations.AlterField(
model_name="templatebutton",
name="button_type",
field=models.CharField(
choices=[
("QUICK_REPLY", "WhatsApp.data.templates.buttons.type.quick_reply"),
(
"PHONE_NUMBER",
"WhatsApp.data.templates.buttons.type.phone_number",
),
("URL", "WhatsApp.data.templates.buttons.type.url"),
("OTP", "WhatsApp.data.templates.buttons.type.otp"),
],
max_length=20,
),
),
]
12 changes: 10 additions & 2 deletions marketplace/wpp_templates/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,27 @@ class TemplateButton(models.Model):
("QUICK_REPLY", "WhatsApp.data.templates.buttons.type.quick_reply"),
("PHONE_NUMBER", "WhatsApp.data.templates.buttons.type.phone_number"),
("URL", "WhatsApp.data.templates.buttons.type.url"),
("OTP", "WhatsApp.data.templates.buttons.type.otp"),
)
OTP_TYPE_CHOICES = (
("COPY_CODE", "Copy Code"),
("ONE_TAP", "One Tap"),
)

uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

translation = models.ForeignKey(
TemplateTranslation, on_delete=models.CASCADE, related_name="buttons"
)

button_type = models.CharField(max_length=20, choices=BUTTON_TYPE_CHOICES)
text = models.CharField(max_length=30, null=True)
country_code = models.IntegerField(null=True)
phone_number = models.CharField(max_length=20, null=True)
url = models.CharField(max_length=2000, null=True)
# to authentication category
otp_type = models.CharField(max_length=10, choices=OTP_TYPE_CHOICES, null=True)
package_name = models.CharField(max_length=255, null=True)
signature_hash = models.CharField(max_length=255, null=True)
autofill_text = models.CharField(max_length=255, null=True)


class TemplateHeader(models.Model):
Expand Down
14 changes: 7 additions & 7 deletions marketplace/wpp_templates/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ def create_template_message(
params = dict(
name=name,
category=category,
components=str(components),
components=components,
language=language,
access_token=self._access_token,
)
response = requests.post(
url=f"https://graph.facebook.com/{WHATSAPP_VERSION}/{waba_id}/message_templates",
params=params,
json=params,
)
if response.status_code != 200:
raise FacebookApiException(response.json())

return response.json()

def update_template_message(self, message_template_id: str, name: str, components: str) -> dict:
def update_template_message(
self, message_template_id: str, name: str, components: str
) -> dict:
params = dict(
name=name,
components=str(components),
access_token=self._access_token
name=name, components=str(components), access_token=self._access_token
)
response = requests.post(
url=f"https://graph.facebook.com/{WHATSAPP_VERSION}/{message_template_id}",
params=params,
)
)
if response.status_code != 200:
raise FacebookApiException(response.json())

Expand Down
71 changes: 58 additions & 13 deletions marketplace/wpp_templates/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,26 @@ class ButtonSerializer(serializers.ModelSerializer):
phone_number = serializers.CharField(required=False)
url = serializers.CharField(required=False)

otp_type = serializers.ChoiceField(
choices=[("COPY_CODE", "COPY_CODE"), ("ONE_TAP", "ONE_TAP")], required=False
)
package_name = serializers.CharField(required=False)
signature_hash = serializers.CharField(required=False)
autofill_text = serializers.CharField(required=False)

class Meta:
model = TemplateButton
fields = ["button_type", "text", "country_code", "phone_number", "url"]
fields = [
"button_type",
"text",
"country_code",
"phone_number",
"url",
"otp_type",
"package_name",
"signature_hash",
"autofill_text",
]


class TemplateTranslationSerializer(serializers.Serializer):
Expand Down Expand Up @@ -138,19 +155,47 @@ 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"
] = f'+{button.get("country_code")} {button.get("phone_number")}'

button_component = button
button_component.pop("button_type")

if button_component.get("country_code"):
button_component.pop("country_code")

buttons_component.get("buttons").append(button_component)
# Specific treatment for "OTP" type buttons in the "AUTHENTICATION" category
if (
template.category == "AUTHENTICATION"
and button.get("button_type") == "OTP"
):
button["type"] = "OTP"
if button.get("otp_type") == "COPY_CODE":
button = {

Check warning on line 166 in marketplace/wpp_templates/serializers.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/serializers.py#L164-L166

Added lines #L164 - L166 were not covered by tests
"type": "OTP",
"otp_type": "COPY_CODE",
}

elif button.get("otp_type") == "ONE_TAP":
if not all(k in button for k in ["package_name", "signature_hash"]):
raise ValidationError(

Check warning on line 173 in marketplace/wpp_templates/serializers.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/serializers.py#L171-L173

Added lines #L171 - L173 were not covered by tests
"For ONE_TAP buttons, 'package_name' and 'signature_hash' are required."
)

button = {

Check warning on line 177 in marketplace/wpp_templates/serializers.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/serializers.py#L177

Added line #L177 was not covered by tests
"type": "OTP",
"otp_type": "ONE_TAP",
"package_name": button.get("package_name"),
"signature_hash": button.get("signature_hash"),
}

# Only add autofill_text if it's provided
if "autofill_text" in button:
button["autofill_text"] = button.get("autofill_text")

Check warning on line 186 in marketplace/wpp_templates/serializers.py

View check run for this annotation

Codecov / codecov/patch

marketplace/wpp_templates/serializers.py#L185-L186

Added lines #L185 - L186 were not covered by tests

else:
if button.get("phone_number"):
button[
"phone_number"
] = f'+{button.get("country_code")} {button.get("phone_number")}'
button["type"] = button.get("button_type")
button.pop("button_type", None)
if button.get("country_code"):
button.pop("country_code")

buttons_component.get("buttons").append(button)

if buttons_component.get("buttons"):
components = self.append_to_components(components, buttons_component)
Expand Down

0 comments on commit 2ee668f

Please sign in to comment.