-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server/events/slack/slack_client.go
- Loading branch information
1 parent
eeb39c5
commit d5d8e12
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package slack | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/nlopes/slack" | ||
) | ||
|
||
type Client interface { | ||
PostMessage(text string) (string, error) | ||
} | ||
|
||
type ConcreteClient struct { | ||
client *slack.Client | ||
channel string | ||
} | ||
|
||
func NewClient(slackToken string, channelName string) (*ConcreteClient, error) { | ||
slackClient := slack.New(slackToken) | ||
|
||
if _, err := slackClient.AuthTest(); err != nil { | ||
return nil, err | ||
} | ||
|
||
// https://api.slack.com/faq | ||
// 'How do I find a channel's ID if I only have its #name?' | ||
// says need to look through all channels and match the name | ||
channels, err := slackClient.GetChannels(true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, c := range channels { | ||
if c.Name == channelName { | ||
// channel exists, no errors | ||
return &ConcreteClient{ | ||
client: slackClient, | ||
channel: channelName, | ||
}, nil | ||
} | ||
} | ||
|
||
return nil, errors.New("channel_not_found") | ||
} | ||
|
||
func (s *ConcreteClient) PostMessage(text string) (string, error) { | ||
params := slack.NewPostMessageParameters() | ||
params.AsUser = true | ||
params.EscapeText = false | ||
|
||
_, timestamp, err := s.client.PostMessage(s.channel, text, params) | ||
return timestamp, err | ||
} |