mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Fix escaping in inbound text messages from MMG
One of our providers gives us messages with special characters escaped, ie a newline comes through as `\n`, not a literal newline. We shouldn’t be showing these backslashes to any of our users. We also have examples of real inbound messages containing `👍` and `’`, so we should continue to display these properly. It’s a bit tricky, because the strings we get from this provider are a mixture of escape sequences (eg `\n`) and unicode characters (eg `😨`). So we have to first convert the unicode character `😨` into an escape sequence, `\U0001f628` in this example. We do this by encoding with the `raw_unicode_escape` codec: > Latin-1 encoding with \uXXXX and \UXXXXXXXX for other code points. > Existing backslashes are not escaped in any way. It is used in the > Python pickle protocol. – https://docs.python.org/3/library/codecs.html#text-encodings Then we turn this back into a string using the `unicode_escape` codec, which transforms all escape sequences into their literal representations (eg `\U0001f628` becomes `😨` and `\n` becomes a newline).
This commit is contained in:
@@ -82,7 +82,11 @@ def receive_firetext_sms():
|
||||
|
||||
|
||||
def format_mmg_message(message):
|
||||
return unquote(message.replace('+', ' '))
|
||||
return unescape_string(unquote(message.replace('+', ' ')))
|
||||
|
||||
|
||||
def unescape_string(string):
|
||||
return string.encode('raw_unicode_escape').decode('unicode_escape')
|
||||
|
||||
|
||||
def format_mmg_datetime(date):
|
||||
|
||||
Reference in New Issue
Block a user