Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@james-lawrence @ACollectionOfAtoms
46 lines (40 sloc) 1.26 KB
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/nlopes/slack"
"github.com/nlopes/slack/slackevents"
)
// You more than likely want your "Bot User OAuth Access Token" which starts with "xoxb-"
var api = slack.New("TOKEN")
func main() {
http.HandleFunc("/events-endpoint", func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
body := buf.String()
eventsAPIEvent, e := slackevents.ParseEvent(json.RawMessage(body), slackevents.OptionVerifyToken(&slackevents.TokenComparator{VerificationToken: "TOKEN"}))
if e != nil {
w.WriteHeader(http.StatusInternalServerError)
}
if eventsAPIEvent.Type == slackevents.URLVerification {
var r *slackevents.ChallengeResponse
err := json.Unmarshal([]byte(body), &r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "text")
w.Write([]byte(r.Challenge))
}
if eventsAPIEvent.Type == slackevents.CallbackEvent {
innerEvent := eventsAPIEvent.InnerEvent
switch ev := innerEvent.Data.(type) {
case *slackevents.AppMentionEvent:
api.PostMessage(ev.Channel, slack.MsgOptionText("Yes, hello.", false))
}
}
})
fmt.Println("[INFO] Server listening")
http.ListenAndServe(":3000", nil)
}
You can’t perform that action at this time.