Skip to content

Instantly share code, notes, and snippets.

@ujjkumsi
Created May 9, 2018 13:57
Show Gist options
  • Select an option

  • Save ujjkumsi/90c99658680c03a72d568202ce43825e to your computer and use it in GitHub Desktop.

Select an option

Save ujjkumsi/90c99658680c03a72d568202ce43825e to your computer and use it in GitHub Desktop.
Challenge Handler for Slack in Golang
package main
import (
"encoding/json"
"log"
"net/http"
)
type Data struct {
Category string `json:"type"`
Token string `json:"token"`
Challenge string `json:"challenge"`
}
type Challenge struct {
Challenge string `json:"challenge"`
}
func challengeHandler(w http.ResponseWriter, r *http.Request) {
challenge := &Data{}
err := json.NewDecoder(r.Body).Decode(challenge)
if err != nil {
log.Printf("[ERROR] %s", err)
panic(err)
}
log.Printf("[CHALLENGE] %s", challenge.Challenge)
resp := &Challenge{}
resp.Challenge = challenge.Challenge
challengeJson, err := json.Marshal(resp)
if err != nil {
panic(err)
}
//set content-type to json
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(challengeJson)
}
.......
http.HandleFunc("/", challengeHandler)
.......
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment