Skip to content

Commit

Permalink
Add server/events/slack/slack_client.go
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-wu-hs committed Nov 2, 2017
1 parent eeb39c5 commit d5d8e12
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions server/events/slack/slack_client.go
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
}

0 comments on commit d5d8e12

Please sign in to comment.