Created
March 6, 2021 22:25
-
-
Save alsmola/d4eb29e726a911f0f38a3a38c34a7049 to your computer and use it in GitHub Desktop.
Template for Lambda auth callback handler for Serverless Slack Block Kit application
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
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