This repository has been archived on 2022-08-18. You can view files and clone it, but cannot push or open issues or pull requests.
rageshake/slack.go

49 lines
967 B
Go
Raw Normal View History

package main
import (
"strings"
"fmt"
"net/http"
)
type slackClient struct {
webHook string
name string
face string
}
func NewSlackClient(webHook string) *slackClient {
return &slackClient{
webHook: webHook,
name: "Notifier",
face: "robot_face"}
}
func (slack *slackClient) Name(name string) {
slack.name = name
}
func (slack *slackClient) Face(face string) {
slack.face = face
}
func (slack slackClient) Notify(text string) error {
json := buildRequest(text, slack)
req, err := http.NewRequest("POST", slack.webHook, strings.NewReader(json))
if err != nil {
return fmt.Errorf("Can't connect to host %s: %s", slack.webHook, err.Error())
}
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
_, err = client.Do(req)
return err
}
func buildRequest(text string, slack slackClient) string {
return fmt.Sprintf(`{"text":"%s", "username": "%s", "icon_emoji": ":%s:"}`, text, slack.name, slack.face)
}