This repository was archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (55 loc) · 1.57 KB
/
main.go
File metadata and controls
64 lines (55 loc) · 1.57 KB
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/eldeal/collab/data"
"github.com/gorilla/mux"
)
//SlackMessage is a subset of the information sent in the message body of a
//request from Slack
type SlackMessage struct {
ID string `json:"user_id"`
Name string `json:"user_name"`
Text string `json:"text"` //googlebot: What is the air-speed velocity of an unladen swallow?
Trigger string `json:"trigger_word"` //googlebot:
}
func main() {
data.StartSession()
r := mux.NewRouter()
s := r.PathPrefix("/collab").Subrouter()
s.HandleFunc("/add", add).Methods("POST")
//s.Methods("POST").HandleFunc("/user/{name}/", findUser)
//s.Methods("POST").HandleFunc("/technology/{name}", findTechnology)
//s.Methods("POST").HandleFunc("/learning/{name}", findLearners)
http.Handle("/", r)
}
func add(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
var msg SlackMessage
if err := decoder.Decode(&msg); err != nil {
panic(err)
}
for _, tech := range msg.split() {
doc := data.DB.FindTechnology(tech)
if doc == nil {
data.DB.NewTechnology(tech, msg.Name, msg.Trigger)
continue
}
switch msg.Trigger {
case "tech:":
doc.AddUser(msg.Name, tech)
case "learn:":
doc.AddLearner(msg.Name, tech)
default:
fmt.Println("How very invalid of you. Try 'tech:' or 'learn:', I don't understand anything else... yet!")
}
}
}
func (msg *SlackMessage) split() []string {
s := strings.TrimPrefix(msg.Text, msg.Trigger)
s = strings.TrimSpace(s)
return strings.Split(s, ",")
}