summaryrefslogblamecommitdiff
path: root/message.go
blob: e9aaf74eb4f1529bc699229d49d27e486793fa7f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                            


                                                                           
















                                                                                          
package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"io/ioutil"
	"log"
)

func sendMessage(responseUrl, origText, text string) error {
	jm := message{
		Text:         origText,
		ResponseType: "in_channel",
		Attachments:  []attachment{{Text: text, Color: "#36a64f"}},
	}
	b, _ := json.Marshal(jm)

	resp, err := getClient().Post(responseUrl, "application/json", bytes.NewReader(b))
	if err != nil {
		log.Println(err)
		return err
	}
	defer resp.Body.Close()
	s, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 300 || resp.StatusCode < 200 {
		log.Println("unexpected status " + resp.Status)
		log.Println(string(s))
		return errors.New("unexpected status " + resp.Status)
	}
	return nil
}