amazon-web-servicesaws-lambdatwilioaws-api-gatewayapache-velocity

Apache Velocity Template Language (VTL): Deal with colons


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
}

Solution

  • 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))