Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created December 19, 2016 14:05
Show Gist options
  • Save rodkranz/e35a7f2169e7472b2e36facaa8bea056 to your computer and use it in GitHub Desktop.
Save rodkranz/e35a7f2169e7472b2e36facaa8bea056 to your computer and use it in GitHub Desktop.
Send Github status in slack Raw
// Copyright 2016 rodkranz. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
/**
_____ _ _
/ ____| | | | |
| | ___ _ __ ___| |_ __ _ _ __ | |_ ___
| | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
| |___| (_) | | | \__ \ || (_| | | | | |_\__ \
\_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
**/
const (
GITHUB_API string = "https://status.github.com/api/last-message.json"
SLACK_API string = "https://hooks.slack.com/services/?????????????????????????"
DATE_FORMAT string = "01/02/2006 15:04"
)
/**
_____ _ _ _ _ _ _____ _ _
/ ____(_) | | | | | | | / ____| | | |
| | __ _| |_| |__| |_ _| |__ | (___ | |_ __ _| |_ _ _ ___
| | |_ | | __| __ | | | | '_ \ \___ \| __/ _` | __| | | / __|
| |__| | | |_| | | | |_| | |_) | ____) | || (_| | |_| |_| \__ \
\_____|_|\__|_| |_|\__,_|_.__/ |_____/ \__\__,_|\__|\__,_|___/
**/
type GitHubStatus struct {
Status string `json:"status"`
Body string `json:"body"`
CreatedOn time.Time `json:"created_on"`
}
func (m *GitHubStatus) Unmarshal(b []byte) error {
return json.Unmarshal(b, m)
}
func GetData(url string) (bs []byte, err error) {
res, err := http.Get(url)
if err != nil {
err = errors.New(fmt.Sprintf("Error to fetch data: %v", err))
return
}
defer res.Body.Close()
if bs, err = ioutil.ReadAll(res.Body); err != nil {
err = errors.New(fmt.Sprintf("Error to read data: %v", err))
}
return
}
/**
_____ _ _ __ __
/ ____| | | | | \/ |
| (___ | | __ _ ___| | __ | \ / | ___ ___ ___ __ _ __ _ ___
\___ \| |/ _` |/ __| |/ / | |\/| |/ _ \/ __/ __|/ _` |/ _` |/ _ \
____) | | (_| | (__| < | | | | __/\__ \__ \ (_| | (_| | __/
|_____/|_|\__,_|\___|_|\_\ |_| |_|\___||___/___/\__,_|\__, |\___|
__/ |
|___/
**/
type SlackMessage struct {
Channel string `json:"channel,omitempty"`
Username string `json:"username,omitempty"`
Text string `json:"text"`
Emotion string `json:"icon_emoji,omitempty"`
}
func (s *SlackMessage) Marshal() (string, error) {
bs, err := json.Marshal(s)
if err != nil {
return "", errors.New(fmt.Sprintf("Error to marshal %T: %v", &s, err))
}
return string(bs), nil
}
func (s *SlackMessage) Hydrate(g *GitHubStatus) {
s.Text = fmt.Sprintf(
"At %v and github status is %v and %v",
strings.ToLower(g.CreatedOn.Format(DATE_FORMAT)),
g.Status,
strings.ToLower(g.Body))
}
/**
_____
/ ____|
| | ___ _ __ ___ _ __ ___ _ _ _ __ ___
| | / _ \| '_ ` _ \| '_ ` _ \| | | | '_ ` _ \
| |___| (_) | | | | | | | | | | | |_| | | | | | |
\_____\___/|_| |_| |_|_| |_| |_|\__,_|_| |_| |_|
**/
func checkError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func FetchGitHubStatus() *GitHubStatus {
m := new(GitHubStatus)
bs, err := GetData(GITHUB_API)
checkError(err)
err = m.Unmarshal(bs)
checkError(err)
return m
}
func PostSlack(s *SlackMessage) {
msg, err := s.Marshal()
if err != nil {
return
}
v := url.Values{}
v.Set("payload", msg)
http.PostForm(SLACK_API, v)
}
/**
__ __ _
| \/ | (_)
| \ / | __ _ _ _ __
| |\/| |/ _` | | '_ \
| | | | (_| | | | | |
|_| |_|\__,_|_|_| |_|
*/
func main() {
g := FetchGitHubStatus()
msg := &SlackMessage{
Channel: "#test-go-bot",
Username: "Gopher",
}
msg.Hydrate(g)
PostSlack(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment