Skip to content

Instantly share code, notes, and snippets.

@alsmola
Created March 6, 2021 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsmola/d4eb29e726a911f0f38a3a38c34a7049 to your computer and use it in GitHub Desktop.
Save alsmola/d4eb29e726a911f0f38a3a38c34a7049 to your computer and use it in GitHub Desktop.
Template for Lambda auth callback handler for Serverless Slack Block Kit application
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/slack-go/slack"
)
// Handler is our lambda handler invoked by the `lambda.Start` function call
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
code, _ := request.QueryStringParameters["code"]
clientID := "your-slack-client-id"
clientSecret := "your-slack-client-secret"
oAuthV2Response, err := slack.GetOAuthV2Response(http.DefaultClient, clientID, clientSecret, code, "")
if err != nil {
return events.APIGatewayProxyResponse{}, err
}
client := slack.New(oAuthV2Response.AccessToken)
teamInfo, err := client.GetTeamInfo()
if err != nil {
return events.APIGatewayProxyResponse{}, err
}
resp := events.APIGatewayProxyResponse{
StatusCode: 302,
IsBase64Encoded: false,
Body: "",
Headers: map[string]string{
"Content-Type": "text/html",
"X-MyCompany-Func-Reply": "pupster", // your service name
"Location": fmt.Sprintf("https://%v.slack.com", teamInfo.Domain),
},
}
return resp, nil
}
func main() {
lambda.Start(Handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment