Created
March 23, 2016 01:58
-
-
Save tcotav/451f8f197998a79c5c43 to your computer and use it in GitHub Desktop.
Rudimentary Golang Hipchat Notification Sender -- no frills :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package hipchat | |
| import ( | |
| "bytes" | |
| "encoding/json" | |
| "net/http" | |
| ) | |
| type HipchatMsg struct { | |
| Color string `json:"color"` | |
| Message string `json:"message"` | |
| Notify bool `json:"notify"` | |
| Message_Format string `json:"message_format"` | |
| } | |
| const ( | |
| MsgType_Err = "problem" | |
| MsgType_Norm = "normal" | |
| ) | |
| const ( | |
| defaultMsgFormat = "text" | |
| htmlMsgFormat = "html" | |
| ) | |
| func SendMessage(url string, msg string, msgType string) (string, error) { | |
| return sendMessage(url, msg, msgType, defaultMsgFormat) | |
| } | |
| func SendMessageHtml(url string, msg string, msgType string) (string, error) { | |
| return sendMessage(url, msg, msgType, htmlMsgFormat) | |
| } | |
| func sendMessage(url string, msg string, msgType string, format string) (string, error) { | |
| var msgColor string | |
| if msgType == "problem" { | |
| msgColor = "red" | |
| } else { | |
| msgColor = "gray" | |
| } | |
| return send(url, HipchatMsg{Color: msgColor, Message: msg, Notify: true, Message_Format: format}) | |
| } | |
| // ref: https://www.hipchat.com/docs/apiv2/method/send_room_notification | |
| func send(hcUrl string, msg HipchatMsg) (string, error) { | |
| jsonStr, err := json.Marshal(msg) | |
| if err != nil { | |
| return "", err | |
| } | |
| req, err := http.NewRequest("POST", hcUrl, bytes.NewBuffer(jsonStr)) | |
| if err != nil { | |
| return "", err | |
| } | |
| //req.Header.Set("X-Custom-Header", "myvalue") | |
| req.Header.Set("Content-Type", "application/json") | |
| client := &http.Client{} | |
| resp, err := client.Do(req) | |
| if err != nil { | |
| return "", err | |
| } | |
| defer resp.Body.Close() | |
| return resp.Status, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To get the hcUrl to send a message to your target room, just grab the URL listed in the tutorial next to text "send messages to this room by posting to this URL" in this image.