/
service.go
66 lines (55 loc) · 1.48 KB
/
service.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
package bot
import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/etcinit/phabulous/app/factories"
"github.com/etcinit/phabulous/app/messages"
"github.com/jacobstr/confer"
"github.com/nlopes/slack"
)
var (
// ErrMissingFeedChannel is used when the feed channel is not configured.
ErrMissingFeedChannel = errors.New("Missing feed channel")
)
// SlackService provides access to the Slack service.
type SlackService struct {
Config *confer.Config `inject:""`
Logger *logrus.Logger `inject:""`
Factory *factories.GonduitFactory `inject:""`
Slack *slack.Client
Bot *Bot
}
// SimplePost posts a simple message to Slack. Most parameters are set to
// defaults.
func (s *SlackService) SimplePost(
channelName string,
storyText string,
icon messages.Icon,
asUser bool,
) {
user := s.Config.GetString("slack.username")
if s.Bot != nil {
user = s.Bot.slackInfo.User.Name
}
s.Slack.PostMessage(
channelName,
storyText,
slack.PostMessageParameters{
Username: user,
IconURL: string(icon),
AsUser: asUser,
},
)
}
// FeedPost posts a message to Slack on the default bot channel.
func (s *SlackService) FeedPost(storyText string) error {
if s.GetFeedChannel() == "" {
return ErrMissingFeedChannel
}
s.SimplePost(s.GetFeedChannel(), storyText, messages.IconDefault, false)
return nil
}
// GetFeedChannel returns the default channel for the bot.
func (s *SlackService) GetFeedChannel() string {
return s.Config.GetString("channels.feed")
}