0

My setup is an AWS Lambda function that receives POST data from API Gateway. The webhook is sent from Twilio when I send a WhatsApp message to my Twilio number.

Can someone explains to me what happens here? Is there a way to avoid that colons are transformed to %3A%2B. Or is that something I have to do on a deeper level with Pyhton?

AWS API Gateway, Integration Request - Mapping Template

  #set($httpPost = $input.path('$').split("&"))
    {
    #foreach( $kvPair in $httpPost )
     #set($kvTokenised = $kvPair.split("="))
     #if( $kvTokenised.size() > 1 )
       "$kvTokenised[0]" : "$kvTokenised[1]"#if( $foreach.hasNext ),#end
     #else
       "$kvTokenised[0]" : ""#if( $foreach.hasNext ),#end
     #end
    #end
    }

AWS Lambda function

def lambda_handler(event, context):
    print("Received event: " + str(event))
    print("Received Body: " + str(event.get('Body')))
    return 200
    # return '<?xml version=\"1.0\" encoding=\"UTF-8\"?>'\
    #       '<Response><Message>Hello world! -Lambda</Message></Response>'

Print in my console. whatsapp%3A%2B491573599343 should be whatsapp:491573599343

Received event: {'SmsMessageSid': 'SM851aa11c912d3775d7941143d8b935f9', 'NumMedia': '0', 'SmsSid': 'SM851aa11c912d3775d7941143d8b935f9', 'SmsStatus': 'received', 'Body': 'Hi', 'To': 'whatsapp%3A%2B4915735992273', 'NumSegments': '1', 'MessageSid': 'SM851aa11c912d3775d7941143d8b935f9', 'AccountSid': 'AC358aa1d18557365a9e1f5e2ffcbcebe0', 'From': 'whatsapp%3A%2B49160343202', 'ApiVersion': '2010-04-01'}

Update:

#set($httpPost = $input.path('$').split("&"))
{
#foreach( $kvPair in $httpPost )
 #set($kvTokenised = $kvPair.split("="))
 #if( $kvTokenised.size() > 1 )
   "$kvTokenised[0]" : "$esc.unurl($kvTokenised[1])"#if( $foreach.hasNext ),#end
 #else
   "$kvTokenised[0]" : ""#if( $foreach.hasNext ),#end
 #end
#end
}
Joey Coder
  • 3,199
  • 8
  • 28
  • 60

2 Answers2

1

Actually this has nothing to do with VTL but with URL decoding.

In Python 3+, You can URL decode any string using the unquote() function provided by urllib.parse package. The unquote() function uses UTF-8 encoding by default.

So in your case you would do like this in your lambda function :

>>> import urllib.parse
>>> encodedStr = 'whatsapp%3A%2B491573599343'
>>> urllib.parse.unquote(encodedStr)
'whatsapp:+491573599343'

You can find a good article here : https://www.urldecoder.io/python/

BTL
  • 4,311
  • 3
  • 24
  • 29
1

What you're asking for is basically is unescaping url-encoded characters. You could achieve that in two ways:

  1. Unescape the parameters in the mapping template itself by using EscapeTool (https://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/EscapeTool.html) of velocity. So your code would now look something like this:

"$kvTokenised[0]" : "$esc.unurl($kvTokenised[1]"

  1. Handling it in your AWS Lambda function itself like this (python):

urllib.parse.unquote(str(event))

Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
  • Hi Rahul, I tried 1. but I now get empty keys : `Received event: {'SmsMessageSid': '', 'NumMedia': '', 'SmsSid': '', 'SmsStatus': '', 'Body': '', 'To': '', 'NumSegments': '', 'MessageSid': '', 'AccountSid': '', 'From': '', 'ApiVersion': ''}`. Do you have any idea away? I posted my updated code above. – Joey Coder Nov 17 '19 at 18:09
  • I think that the `EscapeTool` is not enabled in your Template code. Try including that tool. – Rahul Sharma Nov 17 '19 at 18:13
  • Hi @Rahul Sharma, after some changes, I am now trying the 1. solution with EscapeTool. I wonder if you could have a look at my new post: https://stackoverflow.com/questions/59440132/velocity-template-language-how-to-import-escapetool I still struggle to import it. – Joey Coder Dec 21 '19 at 22:02