chore: Fix 360Dialog template message breakage (#3750)

Template parsing fails when regexp characters are present in templates.

Fixes: #3587
This commit is contained in:
Sojan Jose
2022-01-12 17:41:42 -08:00
committed by GitHub
parent 94209d29cb
commit 1c99294c8c
3 changed files with 52 additions and 9 deletions

View File

@@ -56,7 +56,16 @@ class Whatsapp::SendOnWhatsappService < Base::SendOnChannelService
def build_template_match_regex(template_text)
# Converts the whatsapp template to a comparable regex string to check against the message content
# the variables are of the format {{num}} ex:{{1}}
template_match_string = "^#{template_text.gsub(/{{\d}}/, '(.*)')}$"
# transform the template text into a regex string
# we need to replace the {{num}} with matchers that can be used to capture the variables
template_text = template_text.gsub(/{{\d}}/, '(.*)')
# escape if there are regex characters in the template text
template_text = Regexp.escape(template_text)
# ensuring only the variables remain as capture groups
template_text = template_text.gsub(Regexp.escape('(.*)'), '(.*)')
template_match_string = "^#{template_text}$"
Regexp.new template_match_string
end