/
slack.go
71 lines (58 loc) · 1.42 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
package slack
import (
"context"
"fmt"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/housecream/pkg/channels"
"github.com/nlopes/slack"
)
type SlackLink struct {
channels.CommonLink
ApiKey string
client *slack.Client
}
func (s *SlackLink) Init() error {
s.client = slack.New(s.ApiKey)
_, err := s.client.AuthTest()
if err != nil {
// TODO move this in watch so it can start without internet
return errs.WithE(err, "Failed to connect to slack")
}
return nil
}
func (s *SlackLink) Watch(shutdown <-chan struct{}, events chan<- channels.Event) {
rtm := s.client.NewRTM()
defer rtm.Disconnect()
go rtm.ManageConnection()
for {
select {
case <-shutdown:
return
case msg := <-rtm.IncomingEvents:
fmt.Print(msg)
}
}
}
func (s *SlackLink) Trigger(cxt context.Context, action channels.Action) error {
msgParams := slack.NewPostMessageParameters()
msgParams.LinkNames = 1
if _, _, err := s.client.PostMessage("mytest", "`coucou` @alemaire toi", msgParams); err != nil {
return errs.WithEF(err, s.GetFields(), "Failed to post message to slack")
}
return nil
}
////////////////////////////////
type SlackChannel struct {
channels.CommonChannel
}
func (c *SlackChannel) NewLink() channels.Link {
return &SlackLink{}
}
func init() {
f := SlackChannel{channels.CommonChannel{
Id: "slack",
Name: "Slack",
Description: "read and write to slack",
}}
channels.RegisterChannel(&f)
}