0
votes

Am trying to Create a SMS bot using help of Twilio, Aws API gateway and AWS lambda.

I ve setup the twilio phone number, AWS API for and lambda. I configured twilio to call my API for every sms i send.

I can see that twilio calling my API Gateway with below details.

URL: https://XXXXXXX.execute-api.us-west-2.amazonaws.com/latest/

Parameters:: ApiVersion=2010-04-01&SmsSid=SM446302f23feac00bdd980eb94af16431&SmsStatus=received&SmsMessageSid=SM446302f23feac00bdd980eb94af16431&NumSegments=1&From=%2B18563135226&ToState=NJ&MessageSid=SM446302f23feac00bdd980eb94af16431&AccountSid=AC056ba4aedfd58f83ad1f8d1827351d3b&ToZip=08057&FromCountry=US&ToCity=MOORESTOWN&FromCity=MOORESTOWN&To=%2B18569246402&FromZip=08057&Body=Hello&ToCountry=US&FromState=NJ&NumMedia=0

Message Text:: Msg=Bad+Request&sourceComponent=14100&ErrorCode=11200&EmailNotification=false&httpResponse=400&LogLevel=ERROR&url=https%3A%2F%2Fzwnu2wzf07.execute-api.us-west-2.amazonaws.com%2Flatest%2F

The API Gateway is not able to process the message.Below is the response from API Gateway.

{"message": "Could not parse request body into json: Unrecognized token \'ToCountry\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@6cbe391e; line: 1, column: 11]"}

3
Are you trying to parse the incoming request as JSON somewhere? Twilio requests are made as URL encoded form requests. - philnash
Thanks for the response. Can you please help me what has to be the response format. Same as Application URL encoded ? - kalyan chakravarthy
Can you also provide the CloudWatch log for the request? - Ka Hou Ieong
Twilio makes request in the form application/www-x-form-urlencoded. If you are going to make a response to Twilio, then it expects the response to be XML, using TwiML. - philnash

3 Answers

3
votes

Thanks for all the Comments Above. Twilio Sends Request in the format of "application/www-x-form-urlencoded" So we need to have below in the Api Gateway Integration Request.

Integration Request in Api Gateway to send Twilio request to AWS LAmbda

On the Response Side Twilio accepts "application/xml" Below needs to be configured in the Integration Response of Api Gateway. I have just hardcoded my response but we can configure more in the response box

Integration Response in Api Gateway to send back to twilio from lambda

1
votes

I am a bit late to the party, but I used following mapping template:

{ #foreach( $token in $input.body.split('&') ) #set( $keyVal = $token.split('=') ) #set( $keyValSize = $keyVal.size() ) #if( $keyValSize >= 1 ) #set( $key = $util.urlDecode($keyVal[0]) ) #set($key = $key.substring(0,1).toLowerCase() + $key.substring(1)) #if( $keyValSize >= 2 ) #set( $val = $util.urlDecode($keyVal[1]) ) #else #set( $val = '' ) #end "$key": "$util.escapeJavaScript($val)"#if($foreach.hasNext),#end #end #end }

The request body will be url decoded and submitted as JSON to your Lambda. In case you are using Java you only have to create a POJO to use the actual values.

0
votes

I just had this problem when going through the Lambda - API Gateway tutorial and using $input.path('$') in the Integration Response as they mention there.

I switched the Integration Response to the following after seeing what was in this tutorial and it worked.

#set($inputRoot = $input.path('$'))
$inputRoot.body