-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
103 lines (81 loc) · 2.12 KB
/
slack.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
package main
import (
"encoding/json"
"fmt"
"github.com/umbrellium/mario/Godeps/_workspace/src/golang.org/x/net/websocket"
"io/ioutil"
"log"
"net/http"
"sync/atomic"
)
type chatAgent interface {
getMessage() (Message, error)
postMessage(msg Message) error
}
type Slack struct {
Socket *websocket.Conn
}
type slackResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
Url string `json:"url"`
Userdata userID `json:"self"`
}
type userID struct {
Id string `json:"id"`
}
// Message struct use to generate Slack messages
type Message struct {
Id uint64 `json:"id"`
Type string `json:"type"`
Channel string `json:"channel"`
Text string `json:"text"`
}
var counter uint64
// ConnectToSlack starts Slack real time messaging and opens a websocket
// Returns a websocket, a userID, an error
func connectToSlack(token string) (*websocket.Conn, string, error) {
url := "https://slack.com/api/rtm.start?token=" + token
// connect to rtm
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
// store get response
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
// assign get response to slackResponse struct
var connectionResponse slackResponse
json.Unmarshal(body, &connectionResponse)
if !connectionResponse.Ok {
log.Fatal(err)
}
// connect to slack
socket, err := websocket.Dial(connectionResponse.Url, "", "https://api.slack.com/")
if err != nil {
err = fmt.Errorf("Error: cannot open slack websocket")
return socket, "", err
}
return socket, connectionResponse.Userdata.Id, nil
}
// GetMessage listens to Slack messages
// Returns the message or an error
func (s *Slack) getMessage() (Message, error) {
// the message to return
var msg Message
err := websocket.JSON.Receive(s.Socket, &msg)
if err != nil {
fmt.Errorf("Error: cannot get message")
return msg, err
}
return msg, err
}
// PostMessage publishes a message on Slack
// Returns an error if it couldn't complete the operation
func (s *Slack) postMessage(msg Message) error {
msg.Id = atomic.AddUint64(&counter, 1)
err := websocket.JSON.Send(s.Socket, msg)
return err
}