Giter Club home page Giter Club logo

aws-serverless-chatbot-sample's Introduction

AWS Serverless Chatbot Sample

Example code for running a Slack chatbot using AWS Lambda and Amazon API Gateway.

Getting Started

  1. Create your Lambda function that will process all Slack Requests
  2. Create an API Gateway API
  3. Create a method of type: POST
  4. Select Integration Type: Lambda
  5. Select the region in which you created your Lambda function
  6. Select the Lambda Function you created
  7. Click "Integration Request"
  8. At the bottom of this Page select "Add mapping Template"
  9. For content type please specify: "application/x-www-form-urlencoded"
  10. Insert the template code below into the text field for the template. This code converts a URL Encoded form post into JSON for your Lambda function to parse
  11. Deploy your API
  12. In Slack, Go to Apps and Integrations
  13. Click Build in the top right
  14. Select Make a Custom Integration
  15. Select Outgoing Webhooks
  16. Pick a trigger word for your Bot!
  17. In URL, put the URL created by your API Gateway Deployment

Template code for Integration Request:

## convert HTML POST data or HTTP GET query string to JSON
 
## get the raw post data from the AWS built-in variable and give it a nicer name
#if ($context.httpMethod == "POST")
 #set($rawAPIData = $input.path('$'))
#elseif ($context.httpMethod == "GET")
 #set($rawAPIData = $input.params().querystring)
 #set($rawAPIData = $rawAPIData.toString())
 #set($rawAPIDataLength = $rawAPIData.length() - 1)
 #set($rawAPIData = $rawAPIData.substring(1, $rawAPIDataLength))
 #set($rawAPIData = $rawAPIData.replace(", ", "&"))
#else
 #set($rawAPIData = "")
#end
 
## first we get the number of "&" in the string, this tells us if there is more than one key value pair
#set($countAmpersands = $rawAPIData.length() - $rawAPIData.replace("&", "").length())
 
## if there are no "&" at all then we have only one key value pair.
## we append an ampersand to the string so that we can tokenise it the same way as multiple kv pairs.
## the "empty" kv pair to the right of the ampersand will be ignored anyway.
#if ($countAmpersands == 0)
 #set($rawPostData = $rawAPIData + "&")
#end
 
## now we tokenise using the ampersand(s)
#set($tokenisedAmpersand = $rawAPIData.split("&"))
 
## we set up a variable to hold the valid key value pairs
#set($tokenisedEquals = [])
 
## now we set up a loop to find the valid key value pairs, which must contain only one "="
#foreach( $kvPair in $tokenisedAmpersand )
 #set($countEquals = $kvPair.length() - $kvPair.replace("=", "").length())
 #if ($countEquals == 1)
  #set($kvTokenised = $kvPair.split("="))
  #if ($kvTokenised[0].length() > 0)
   ## we found a valid key value pair. add it to the list.
   #set($devNull = $tokenisedEquals.add($kvPair))
  #end
 #end
#end
 
## next we set up our loop inside the output structure "{" and "}"
{
#foreach( $kvPair in $tokenisedEquals )
  ## finally we output the JSON for this pair and append a comma if this isn't the last pair
  #set($kvTokenised = $kvPair.split("="))
 "$util.urlDecode($kvTokenised[0])" : #if($kvTokenised[1].length() > 0)"$util.urlDecode($kvTokenised[1])"#{else}""#end#if( $foreach.hasNext ),#end
#end
}

aws-serverless-chatbot-sample's People

Contributors

aaronkao avatar hyandell avatar listonb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-serverless-chatbot-sample's Issues

Sample Integration Request Template doesn't transform Slack's Interactive Button payload to JSON

Hi,

The Integration Request template provided in your README is great, and works perfectly for Slack's slash commands, which have payloads that look like this:

token=ABC123ABC123ABC123&team_id=T12345678&...

But the template fails for requests that come from Slack's Interactive Buttons, which have payloads that look like this:

payload=%7B%22actions%22%3A%5B%7B%22name%22%3A%22start%22%2C%22... <- escaped JSON inside a single url param, 'payload'.

Both come through with Content-Type=application/x-www-form-urlencoded headers, so it'll be necessary to set up separate API Gateway resources for slash commands and interactive messages.

I realize that interactive buttons are outside the scope of your sample application here, but I think an additional mapping template that addresses this alternate payload format would be helpful for others who reference your README for the competition. I was delighted to find your mapping template, which worked flawlessly for the original payloads, but then I was set back a few hours while I tried to figure out why interactive button payloads were failing.

I have a hacky solution in place for dealing with the other format, but perhaps you can come up with something more elegant. I'd be happy to put together a pull request if you'd like, but I wanted to discuss here first before moving to that.

Integration Request Mapping Template for Slack's Interactive Messages:

## The 8 simply strips off the 'payload=' characters at the beginning of the input
#set ($encodedJSON = $input.body.substring(8))
$util.urlDecode(${encodedJSON})

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.