-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.go
63 lines (53 loc) · 1.41 KB
/
web.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
package main
// Slack outgoing webhooks are handled here. Requests come in and are run through
// the processor to generate a response, which is sent back to Slack.
//
// Create an outgoing webhook in your Slack here:
// https://my.slack.com/services/new/outgoing-webhook
import (
"encoding/json"
"log"
"net/http"
"strconv"
)
type WebhookResponse struct {
Username string `json:"username"`
Text string `json:"text"`
}
func init() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
incomingText := r.PostFormValue("text")
log.Printf("Handling incoming request: %s", incomingText)
if r.PostFormValue("user_id") == "USLACKBOT" {
return
}
urls := extractUrls(incomingText)
for _, url := range urls {
log.Printf("Checking url: %s", url)
uses := urlsUsed.Get(url)
if len(uses) > 0 {
log.Printf("Uses: %q", uses)
var response WebhookResponse
response.Username = botUsername
response.Text = "Ooooooooooold!"
log.Printf("Sending response: %s", response.Text)
b, err := json.Marshal(response)
if err != nil {
log.Fatal(err)
}
w.Write(b)
}
urlsUsed.Add(url, r.PostFormValue("timestamp"))
}
go func() {
urlsUsed.Save(stateFile)
}()
})
}
func StartServer(port int) {
log.Printf("Starting HTTP server on %d", port)
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}