Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Last active August 29, 2015 14:25
Show Gist options
  • Save hirokazumiyaji/8355c0288ea84ef30b0b to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/8355c0288ea84ef30b0b to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
type NotifyMessage struct {
Color string `json:"color"`
Message string `json:"message"`
Notify bool `json:"notify"`
MessageFormat string `json:"message_format"`
}
func (m *NotifyMessage) Post(roomId, authToken string) (*http.Response, error) {
data, err := json.Marshal(m)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(
"POST",
fmt.Sprintf("https://api.hipchat.com/v2/room/%s/notification?auth_token=%s", roomId, authToken),
bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}
func main() {
message := &NotifyMessage{
Color: "gray",
Message: "Test",
Notify: false,
MessageFormat: "html",
}
res, err := message.Post("<RoomId or Name>", "<Access Token>")
if err != nil {
fmt.Println("Request Error: %v", err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println("response Status:", res.Status)
fmt.Println("response Headers:", res.Header)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println("response Body:", string(body))
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
type NotifyMessage struct {
Color string `json:"color"`
Message string `json:"message"`
Notify bool `json:"notify"`
MessageFormat string `json:"message_format"`
}
func (m *NotifyMessage) Post(roomId, authToken string) (*http.Response, error) {
return http.PostForm(
fmt.Sprintf("https://api.hipchat.com/v2/room/%s/notification?auth_token=%s", roomId, authToken),
url.Values{
"color": m.Color,
"message": m.Message,
"notify": m.Notify,
"message_format": m.MessageFormat,
})
}
func main() {
message := &NotifyMessage{
Color: "gray",
Message: "Test",
Notify: false,
MessageFormat: "html",
}
res, err := message.Post("<RoomId or Name>", "<Access Token>")
if err != nil {
fmt.Println("Request Error: %v", err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println("response Status:", res.Status)
fmt.Println("response Headers:", res.Header)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println("response Body:", string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment