Created
December 4, 2018 17:06
cloudformation template for simple slack bot (pingpong)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
AWSTemplateFormatVersion: 2010-09-09 | |
Description: "Slack bot" | |
Parameters: | |
SlackBotUserOAuthAccessToken: | |
Type: String | |
Description: "Slack Bot User OAuth Access Token" | |
Resources: | |
# Lambda | |
LambdaExecutionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
AssumeRolePolicyDocument: | |
Statement: | |
- Action: | |
- sts:AssumeRole | |
Effect: Allow | |
Principal: | |
Service: | |
- lambda.amazonaws.com | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/PowerUserAccess | |
Path: / | |
RoleName: lambdaRole | |
LambdaFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Code: | |
ZipFile: | | |
const https = require('https') | |
exports.handler = (slack_event, context, callback) => { | |
if(slack_event.type == 'url_verification'){ | |
callback(null, { challenge: slack_event.challenge }) | |
return | |
} | |
if(slack_event.subtype == 'bot_message') { | |
callback(null, "OK") | |
return | |
} | |
if(!/ping/.test(slack_event.event.text)) { | |
callback(null, "OK") | |
return | |
} | |
postToSlack ( | |
'pong', | |
slack_event.event.channel, | |
process.env['BOT_USER_OAUTH_ACCESS_TOKEN'] | |
) | |
callback(null, "OK") | |
} | |
function postToSlack (message, channel, access_token) { | |
const data = JSON.stringify({ | |
token: access_token, | |
channel: channel, | |
text: message, | |
}) | |
const options = { | |
host: 'slack.com', | |
port: 443, | |
path: '/api/chat.postMessage', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json; charset=UTF-8', | |
'Authorization': `Bearer ${access_token}` | |
} | |
} | |
const req = https.request(options); | |
req.on('error', (e) => { console.log(e.message) }) | |
req.write(data) | |
req.end() | |
} | |
Description: "Slack bot" | |
FunctionName: slack_bot | |
Handler: index.handler | |
MemorySize: 128 | |
Role: !GetAtt LambdaExecutionRole.Arn | |
Runtime: nodejs6.10 | |
Timeout: 3 | |
Environment: | |
Variables: | |
BOT_USER_OAUTH_ACCESS_TOKEN: !Sub ${SlackBotUserOAuthAccessToken} | |
# ApiGateway | |
Api: | |
Type: "AWS::ApiGateway::RestApi" | |
Properties: | |
Name: "api" | |
Resource: | |
Type: "AWS::ApiGateway::Resource" | |
DependsOn: "LambdaFunction" | |
Properties: | |
RestApiId: !Ref Api | |
ParentId: !GetAtt Api.RootResourceId | |
PathPart: "slack_bot" | |
LambdaPermission: | |
Type: "AWS::Lambda::Permission" | |
DependsOn: "LambdaFunction" | |
Properties: | |
FunctionName: !Ref LambdaFunction | |
Action: "lambda:InvokeFunction" | |
Principal: "apigateway.amazonaws.com" | |
SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${Api}/*/POST/${LambdaFunction}" | |
ResourceMethod: | |
Type: "AWS::ApiGateway::Method" | |
Properties: | |
RestApiId: !Ref Api | |
ResourceId: !Ref Resource | |
AuthorizationType: "None" | |
HttpMethod: "POST" | |
MethodResponses: | |
- StatusCode: 200 | |
ResponseModels: { "application/json": "Empty" } | |
Integration: | |
Type: "AWS" | |
IntegrationResponses: | |
- StatusCode: 200 | |
ResponseParameters: {} | |
ResponseTemplates: {} | |
IntegrationHttpMethod: "POST" | |
Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations" | |
DependsOn: "LambdaPermission" | |
Deployment: | |
Type: "AWS::ApiGateway::Deployment" | |
DependsOn: "ResourceMethod" | |
Properties: | |
Description: "Api gateway Deployment" | |
RestApiId: { "Ref" : "Api" } | |
StageName: "prod" | |
StageDescription: | |
Description: "Production Stage" | |
MethodSettings: | |
- ResourcePath: "/slack_bot" | |
HttpMethod: "POST" | |
Outputs: | |
ApiEndpoint: | |
Value: !Sub "https://${Api}.execute-api.${AWS::Region}.amazonaws.com/prod/slack_bot" | |
Description: "Slack Api Endpoint" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment