AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Parameters:
  VerificationToken:
    Description: Verification token from Slack app settings
    Type: String
    NoEcho: true
    
  AccessToken:
    Description: OAuth access token from Slack app settings
    Type: String
    NoEcho: true

Resources:
  HotdogDetectorAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      DefinitionBody:
        swagger: "2.0"
        info:
          version: "2017-06-11T02:20:25Z"
          title: !Ref AWS::StackName
        basePath: "/Prod"
        schemes:
        - "https"
        paths:
          /:
            post:
              produces:
              - "application/json"
              responses:
                "200":
                  description: "200 response"
                  schema:
                    $ref: "#/definitions/Empty"
              x-amazon-apigateway-integration:
                responses:
                  default:
                    statusCode: "200"
                uri: !Sub |-
                  arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HotdogDetectorFunction.Arn}/invocations
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                contentHandling: "CONVERT_TO_TEXT"
                type: "aws"
        definitions:
          Empty:
            type: "object"
            title: "Empty Schema"


  HotdogDetectorFunction:
    Type: AWS::Serverless::Function
    Properties:
      Description: Checks Slack message.channels events for images. If an image is found uses Rekognition to determine if it contains a hotdog.
      Handler: detect_hotdog.lambda_handler
      Runtime: python3.6
      Timeout: 10
      CodeUri: ../lambda_functions/
      Environment:
        Variables:
          VERIFICATION_TOKEN: !Ref VerificationToken
          ACCESS_TOKEN: !Ref AccessToken
      Events:
        HotdogDetectorAPIPost:
          Type: Api
          Properties:
            Path: /
            Method: post
            RestApiId: !Ref HotdogDetectorAPI
      Policies: 
      - RekognitionLabelsPolicy: {}

Outputs:
  RequestURL:
    Description: Prod URL for API to use as Request URL for Slack app
    Value: !Sub |-
       https://${HotdogDetectorAPI}.execute-api.${AWS::Region}.amazonaws.com/Prod/