| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/slack-go/slack" |
| 10 | ) |
| 11 | |
| 12 | func main() { |
| 13 | var token, channel string |
| 14 | var ok bool |
| 15 | token, ok = os.LookupEnv("SLACK_TOKEN") |
| 16 | if !ok { |
| 17 | fmt.Println("Missing SLACK_TOKEN in environment") |
| 18 | os.Exit(1) |
| 19 | } |
| 20 | channel, ok = os.LookupEnv("SLACK_CHANNEL") |
| 21 | if !ok { |
| 22 | fmt.Println("Missing SLACK_CHANNEL in environment") |
| 23 | os.Exit(1) |
| 24 | } |
| 25 | api := slack.New(token) |
| 26 | attachment := slack.Attachment{ |
| 27 | Pretext: "pretext", |
| 28 | Fallback: "We don't currently support your client", |
| 29 | CallbackID: "accept_or_reject", |
| 30 | Color: "#3AA3E3", |
| 31 | Actions: []slack.AttachmentAction{ |
| 32 | slack.AttachmentAction{ |
| 33 | Name: "accept", |
| 34 | Text: "Accept", |
| 35 | Type: "button", |
| 36 | Value: "accept", |
| 37 | }, |
| 38 | slack.AttachmentAction{ |
| 39 | Name: "reject", |
| 40 | Text: "Reject", |
| 41 | Type: "button", |
| 42 | Value: "reject", |
| 43 | Style: "danger", |
| 44 | }, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | message := slack.MsgOptionAttachments(attachment) |
| 49 | channelID, timestamp, err := api.PostMessage(channel, slack.MsgOptionText("", false), message) |
| 50 | if err != nil { |
| 51 | fmt.Printf("Could not send message: %v", err) |
| 52 | } |
| 53 | fmt.Printf("Message with buttons sucessfully sent to channel %s at %s", channelID, timestamp) |
| 54 | http.HandleFunc("/actions", actionHandler) |
| 55 | http.ListenAndServe(":3000", nil) |
| 56 | } |
| 57 | |
| 58 | func actionHandler(w http.ResponseWriter, r *http.Request) { |
| 59 | var payload slack.InteractionCallback |
| 60 | err := json.Unmarshal([]byte(r.FormValue("payload")), &payload) |
| 61 | if err != nil { |
| 62 | fmt.Printf("Could not parse action response JSON: %v", err) |
| 63 | } |
| 64 | fmt.Printf("Message button pressed by user %s with value %s", payload.User.Name, payload.ActionCallback.AttachmentActions[0].Value) |
| 65 | } |