AWSTemplateFormatVersion: 2010-09-09
Description: 'This template creates the artefacts required by the Deter Spam Calls blog'
Parameters:
    AmazonConnectInstanceARN:
        Type: String
        Description: Amazon Connect ARN - Looks like...
        Default: "arn:aws:connect:<Region>:<Account ID>:instance/<Instance ID>"    

Resources:
  #####################################################
  # IAM Role 
  #####################################################
  IAMRoleDeterSpamCallls:
        Type: AWS::IAM::Role
        DeletionPolicy: Delete
        Properties:
            AssumeRolePolicyDocument:
                Version: '2012-10-17'
                Statement:
                    - Effect: Allow
                      Principal:
                        Service:
                        - lambda.amazonaws.com
                      Action:
                      - sts:AssumeRole
            ManagedPolicyArns: 
              - Fn::Join:
                  - ""
                  - - "arn:"
                    - Ref: AWS::Partition
                    - :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
            RoleName: !Join ['', ['IAMRl-deter-spam-', !Ref  'AWS::StackName']]

  #####################################################
  # Lambda 2: Connect event handler
  #####################################################
  FnDeterSpamCalls:
    Type: AWS::Lambda::Function
    Properties:
        Runtime: python3.11 
        Description: Lambda to upload customer data
        Handler: index.lambda_handler
        FunctionName: !Join ['', ['fn-deter-spam-', !Ref  'AWS::StackName']]
        Code:
            ZipFile: |
                  import random
                  from random import randint

                  START_SEED  = 1000
                  END_SEED    = 9999

                  def lambda_handler(event, context):
                      print(event)
                      return {
                          'status': 200,
                          'randomNumber': randint(START_SEED, END_SEED)
                      }
                          
        Role: !GetAtt IAMRoleDeterSpamCallls.Arn
        MemorySize: 128
        Timeout: 10

#####################################################
# Lambda resource based policy for Connect
#####################################################  
  ConnectResourcePolicy:
        Type: AWS::Lambda::Permission
        Properties:
            FunctionName: !GetAtt FnDeterSpamCalls.Arn
            Action: lambda:InvokeFunction
            Principal: connect.amazonaws.com
            SourceArn: 
                Ref: AmazonConnectInstanceARN 

#####################################################
# Basic flow
#####################################################
  FLOWDETERSPAMS:
    Type: AWS::Connect::ContactFlow
    Properties:
      Name: !Join ['', [!Ref 'AWS::StackName', '-FLOWDETERSPAMS']]
      Description: Basic Flow to demonstrate deterring spam calls
      InstanceArn: !Ref AmazonConnectInstanceARN
      Type: CONTACT_FLOW
      Content: !Sub  
        - 
          "{\"Version\":\"2019-10-30\",\"StartAction\":\"479a8aac-313d-46c2-8bb2-b5736298407e\",\"Metadata\":{\"entryPointPosition\":{\"x\":40,\"y\":40},\"ActionMetadata\":{\"dd4eed7d-55ad-4e54-aac4-4fb97edc9b06\":{\"position\":{\"x\":641.6,\"y\":134.4},\"parameters\":{\"Attributes\":{\"random_number\":{\"useDynamic\":true}}},\"dynamicParams\":[\"random_number\"]},\"589b9e41-7ad4-47e0-beea-278d2e27dd26\":{\"position\":{\"x\":645.6,\"y\":362.4},\"parameters\":{\"Attributes\":{\"caller_entry\":{\"useDynamic\":true}}},\"dynamicParams\":[\"caller_entry\"]},\"f9d2415d-5dcd-45e4-8cf0-5cf258bbd64d\":{\"position\":{\"x\":420.8,\"y\":364.8},\"conditionMetadata\":[],\"countryCodePrefix\":\"+1\"},\"611e0f46-d4f9-470c-9f1e-a85e1b4dda2d\":{\"position\":{\"x\":682.4,\"y\":578.4}},\"de04d541-62a8-4f94-b448-d7398d88a38c\":{\"position\":{\"x\":964,\"y\":583.2}},\"8d237f5f-3f46-4e9a-b6a6-1d55179ee250\":{\"position\":{\"x\":424.8,\"y\":590.4},\"conditions\":[],\"conditionMetadata\":[{\"id\":\"59feef0d-9e63-4ff5-a6b7-792c4f73fa29\",\"operator\":{\"name\":\"Equals\",\"value\":\"Equals\",\"shortDisplay\":\"=\"},\"value\":\"$.Attributes.caller_entry\"}]},\"6ea927a4-7eda-43d7-9393-8e858dadc4c7\":{\"position\":{\"x\":682.4,\"y\":769.6}},\"479a8aac-313d-46c2-8bb2-b5736298407e\":{\"position\":{\"x\":139.2,\"y\":132}},\"20204986-2027-463c-ab9a-a93048e1093b\":{\"position\":{\"x\":398.4,\"y\":134.4},\"parameters\":{\"LambdaFunctionARN\":{\"useDynamic\":true}},\"dynamicMetadata\":{},\"useDynamic\":true},\"148b613b-ea04-4d95-b1a0-5d4126d723d4\":{\"position\":{\"x\":136.8,\"y\":326.4},\"dynamicParams\":[]}},\"Annotations\":[]},\"Actions\":[{\"Parameters\":{\"Attributes\":{\"random_number\":\"$.External.randomNumber\"},\"TargetContact\":\"Current\"},\"Identifier\":\"dd4eed7d-55ad-4e54-aac4-4fb97edc9b06\",\"Type\":\"UpdateContactAttributes\",\"Transitions\":{\"NextAction\":\"f9d2415d-5dcd-45e4-8cf0-5cf258bbd64d\",\"Errors\":[{\"NextAction\":\"f9d2415d-5dcd-45e4-8cf0-5cf258bbd64d\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{\"Attributes\":{\"caller_entry\":\"$.StoredCustomerInput\"},\"TargetContact\":\"Current\"},\"Identifier\":\"589b9e41-7ad4-47e0-beea-278d2e27dd26\",\"Type\":\"UpdateContactAttributes\",\"Transitions\":{\"NextAction\":\"8d237f5f-3f46-4e9a-b6a6-1d55179ee250\",\"Errors\":[{\"NextAction\":\"8d237f5f-3f46-4e9a-b6a6-1d55179ee250\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{\"StoreInput\":\"True\",\"InputTimeLimitSeconds\":\"5\",\"SSML\":\"<speak>Thank you for calling. Please use your phone keypad to enter <say-as interpret-as=\\\"digits\\\">$.Attributes.random_number </say-as> to continue. \\n</speak>\",\"DTMFConfiguration\":{\"DisableCancelKey\":\"False\"},\"InputValidation\":{\"CustomValidation\":{\"MaximumLength\":\"4\"}}},\"Identifier\":\"f9d2415d-5dcd-45e4-8cf0-5cf258bbd64d\",\"Type\":\"GetParticipantInput\",\"Transitions\":{\"NextAction\":\"589b9e41-7ad4-47e0-beea-278d2e27dd26\",\"Errors\":[{\"NextAction\":\"589b9e41-7ad4-47e0-beea-278d2e27dd26\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{\"Text\":\"Thank you. In real world, this call would then proceed as normal. Thank you for calling. Goodbye.\"},\"Identifier\":\"611e0f46-d4f9-470c-9f1e-a85e1b4dda2d\",\"Type\":\"MessageParticipant\",\"Transitions\":{\"NextAction\":\"de04d541-62a8-4f94-b448-d7398d88a38c\",\"Errors\":[{\"NextAction\":\"de04d541-62a8-4f94-b448-d7398d88a38c\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{},\"Identifier\":\"de04d541-62a8-4f94-b448-d7398d88a38c\",\"Type\":\"DisconnectParticipant\",\"Transitions\":{}},{\"Parameters\":{\"ComparisonValue\":\"$.Attributes.random_number\"},\"Identifier\":\"8d237f5f-3f46-4e9a-b6a6-1d55179ee250\",\"Type\":\"Compare\",\"Transitions\":{\"NextAction\":\"6ea927a4-7eda-43d7-9393-8e858dadc4c7\",\"Conditions\":[{\"NextAction\":\"611e0f46-d4f9-470c-9f1e-a85e1b4dda2d\",\"Condition\":{\"Operator\":\"Equals\",\"Operands\":[\"$.Attributes.caller_entry\"]}}],\"Errors\":[{\"NextAction\":\"6ea927a4-7eda-43d7-9393-8e858dadc4c7\",\"ErrorType\":\"NoMatchingCondition\"}]}},{\"Parameters\":{\"Text\":\"Thank you. Your entry did not match. In real world, this would be treated as a spam call. Thank you for calling. Goodbye.\"},\"Identifier\":\"6ea927a4-7eda-43d7-9393-8e858dadc4c7\",\"Type\":\"MessageParticipant\",\"Transitions\":{\"NextAction\":\"de04d541-62a8-4f94-b448-d7398d88a38c\",\"Errors\":[{\"NextAction\":\"de04d541-62a8-4f94-b448-d7398d88a38c\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{\"FlowLoggingBehavior\":\"Enabled\"},\"Identifier\":\"479a8aac-313d-46c2-8bb2-b5736298407e\",\"Type\":\"UpdateFlowLoggingBehavior\",\"Transitions\":{\"NextAction\":\"148b613b-ea04-4d95-b1a0-5d4126d723d4\"}},{\"Parameters\":{\"LambdaFunctionARN\":\"$.Attributes.lambda_function\",\"InvocationTimeLimitSeconds\":\"3\",\"ResponseValidation\":{\"ResponseType\":\"STRING_MAP\"}},\"Identifier\":\"20204986-2027-463c-ab9a-a93048e1093b\",\"Type\":\"InvokeLambdaFunction\",\"Transitions\":{\"NextAction\":\"dd4eed7d-55ad-4e54-aac4-4fb97edc9b06\",\"Errors\":[{\"NextAction\":\"dd4eed7d-55ad-4e54-aac4-4fb97edc9b06\",\"ErrorType\":\"NoMatchingError\"}]}},{\"Parameters\":{\"Attributes\":{\"lambda_function\":\"${FUNCTION_ARN}\"},\"TargetContact\":\"Current\"},\"Identifier\":\"148b613b-ea04-4d95-b1a0-5d4126d723d4\",\"Type\":\"UpdateContactAttributes\",\"Transitions\":{\"NextAction\":\"20204986-2027-463c-ab9a-a93048e1093b\",\"Errors\":[{\"NextAction\":\"20204986-2027-463c-ab9a-a93048e1093b\",\"ErrorType\":\"NoMatchingError\"}]}}]}"
        - FUNCTION_ARN: !GetAtt FnDeterSpamCalls.Arn


