-
Notifications
You must be signed in to change notification settings - Fork 6
/
slack.go
68 lines (55 loc) · 1.29 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
// Copyright 2015 Bowery, Inc.
package slack
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
var (
slackAddr = "https://slack.com/api"
)
const (
postMessageURI = "chat.postMessage"
)
type slackPostMessageRes struct {
Ok bool
Error string
}
// Client represents a slack api client. A Client is
// used for making requests to the slack api.
type Client struct {
token string
}
// NewClient returns a Client with the provided api token.
func NewClient(token string) *Client {
return &Client{token}
}
// SendMessage sends a text message to a specific channel
// with a specific username.
func (c *Client) SendMessage(channel, message, username string) error {
if channel == "" || message == "" || username == "" {
return errors.New("channel, message, and username required")
}
payload := url.Values{}
payload.Set("token", c.token)
payload.Set("channel", channel)
payload.Set("text", message)
payload.Set("username", username)
res, err := http.PostForm(fmt.Sprintf("%s/%s", slackAddr, postMessageURI), payload)
if err != nil {
return err
}
defer res.Body.Close()
resBody := new(slackPostMessageRes)
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(resBody)
if err != nil {
return err
}
if !resBody.Ok {
return errors.New(resBody.Error)
}
return nil
}