-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslackbot.go
53 lines (46 loc) · 1.06 KB
/
slackbot.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
//Package slackbot allows posting messages to Slack using the Slackbot API
package slackbot
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/url"
)
// SlackBot stores the the current config and methods
type SlackBot struct {
url string
}
// New configures a new SlackBot instance with the given url
func New(url string) SlackBot {
return SlackBot{url: url}
}
func (bot SlackBot) buildURL(channel string) (*url.URL, error) {
u, err := url.Parse(bot.url)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("channel", channel)
u.RawQuery = q.Encode()
return u, nil
}
// PostMessage sends the given message to the given channel as Slackbot
func (bot SlackBot) PostMessage(channel, message string) error {
url, err := bot.buildURL(channel)
if err != nil {
log.Printf("Error building the URL: %v", err)
return err
}
r, err := http.Post(url.String(), "text/plain", bytes.NewBufferString(message))
if err != nil {
return err
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
log.Println(string(b))
return nil
}