This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
110 lines (102 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"math/rand"
"net/http"
"os"
"regexp"
"strings"
"time"
)
const (
tokenConfig = "INCOMING_SLACK_TOKEN"
webhookConfig = "INCOMING_SLACK_WEBHOOK"
// Incoming payload form will have the following keys:
// (See: https://api.slack.com/slash-commands)
keyToken = "token"
keyTeamID = "team_id"
keyChannelId = "channel_id"
keyChannelName = "channel_name"
keyUserID = "user_id"
keyUserName = "user_name"
keyCommand = "command"
keyText = "text"
)
type slackMsg struct {
Text string `json:"text"`
Username string `json:"username"` // Anonymous animal sender
Channel string `json:"channel"` // Recipient
}
var (
port int
// Random animals cribbed from Google Drive's "Anonymous [Animal]" notifications
animals = []string{
"alligator", "anteater", "armadillo", "auroch", "axolotl", "badger", "bat", "beaver", "buffalo",
"camel", "chameleon", "cheetah", "chipmunk", "chinchilla", "chupacabra", "cormorant", "coyote",
"crow", "dingo", "dinosaur", "dolphin", "duck", "elephant", "ferret", "fox", "frog", "giraffe",
"gopher", "grizzly", "hedgehog", "hippo", "hyena", "jackal", "ibex", "ifrit", "iguana", "koala",
"kraken", "lemur", "leopard", "liger", "llama", "manatee", "mink", "monkey", "narwhal", "nyan cat",
"orangutan", "otter", "panda", "penguin", "platypus", "python", "pumpkin", "quagga", "rabbit", "raccoon",
"rhino", "sheep", "shrew", "skunk", "slow loris", "squirrel", "turtle", "walrus", "wolf", "wolverine", "wombat",
}
// Username must be first.
payloadExp = regexp.MustCompile(`([@#][^\s]+):?(.*)`)
)
// readAnonymousMessage parses the username and re-routes
// the message to the user from an anonymous animal
func readAnonymousMessage(r *http.Request) string {
err := r.ParseForm()
// TODO: Change HTTP status code
if err != nil {
return string(err.Error())
}
// Incoming POST's token should match the one set in Heroku
if len(r.Form[keyToken]) == 0 || r.Form[keyToken][0] != os.Getenv(tokenConfig) {
return "Config error."
}
if len(r.Form[keyText]) == 0 {
return "Slack bug; inform the team."
}
msg := strings.TrimSpace(r.Form[keyText][0])
matches := payloadExp.FindStringSubmatch(msg)
if matches == nil {
return "Failed; message should be like: /anon @ashwin hey what's up?"
}
user := matches[1]
msg = strings.TrimSpace(matches[2])
err = sendAnonymousMessage(user, msg)
if err != nil {
return "Failed to send message."
}
return fmt.Sprintf("Anonymously sent [%s] to %s", msg, user)
}
// sendAnonymousMessage uses an incoming hook to Direct Message
// the given user the message, from a random animal.
func sendAnonymousMessage(username, message string) error {
url := os.Getenv(webhookConfig)
payload, err := json.Marshal(slackMsg{
Text: message,
Channel: username,
Username: fmt.Sprintf("an anonymous %s", animals[rand.Intn(len(animals))]),
})
if err != nil {
return err
}
_, err = http.Post(url, "application/json", bytes.NewBuffer(payload))
return err
}
func main() {
rand.Seed(time.Now().UnixNano())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
result := readAnonymousMessage(r)
fmt.Fprintf(w, result)
})
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func init() {
flag.IntVar(&port, "port", 5000, "HTTP server port")
flag.Parse()
}