Skip to content

Commit

Permalink
Support alphanumeric variable names in twilio templates
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed May 8, 2024
1 parent 12d0948 commit 2a355d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions temba/templates/types/twilio/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .type import TwilioType


class WhatsAppTypeTest(TembaTest):
class TwilioTypeTest(TembaTest):
def setUp(self):
self.type = TwilioType()

Expand All @@ -20,7 +20,23 @@ def setUp(self):
def test_extract_variables(self):
self.assertEqual(["1", "2"], self.type._extract_variables("Hi {{2}} how are you? {{1}}"))
self.assertEqual(["1", "2"], self.type._extract_variables("Hi {{1}} how are you? {{2}} {{1}}"))
self.assertEqual([], self.type._extract_variables("Hi {there}. {{x}}"))
self.assertEqual(
["1", "flightnumber"],
self.type._extract_variables("Hello this is an update for your flight: {{flightnumber}}. Thank you {{1}}"),
)
self.assertEqual(
["1", "flightnumber"],
self.type._extract_variables(
"Hello this is an update for your flight: {{ flightnumber }}. Thank you {{1}}"
),
)
self.assertEqual(
["1"],
self.type._extract_variables(
"Hello this is an update for your flight: {{ flight number }}. Thank you {{1}}"
),
)
self.assertEqual([], self.type._extract_variables("Hi {there}. {{%#43}}"))

def test_parse_language(self):
self.assertEqual("eng", self.type._parse_language("en"))
Expand Down
4 changes: 2 additions & 2 deletions temba/templates/types/twilio/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class TwilioType(TemplateType):
slug = "whatsapp"
variable_regex = re.compile(r"{{(\d+)}}")
variable_regex = re.compile(r"{{[ ]*([A-Za-z0-9]+)[ ]*}}")

STATUS_MAPPING = {
"PENDING": TemplateTranslation.STATUS_PENDING,
Expand Down Expand Up @@ -119,7 +119,7 @@ def add_variables(names: list, typ: str) -> dict:
}
)
elif content_type == "twilio/call-to-action":
button_type = action["type"].upper()
button_type = action["type"]
if button_type == "URL":
button_url = action["url"]
button_vars = add_variables(self._extract_variables(button_url), "text")
Expand Down

0 comments on commit 2a355d6

Please sign in to comment.