Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go Slack postMessage (attachements ver)
package slack
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"os"
)
func PostMessage(channel string, title string, link string, message string) string {
attachements := [1]map[string]string{{
"pretext": title,
"title": title,
"title_link": link,
"text": message,
"color": "#2ecc71",
}}
d, _ := json.Marshal(attachements)
data := url.Values{}
data.Set("token", os.Getenv("SLACK_API_TOKEN"))
data.Add("channel", channel)
data.Add("username", "your bot name")
data.Add("link_names", "1")
data.Add("attachments", string(d[:]))
client := &http.Client{}
r, _ := http.NewRequest(
"POST",
"https://slack.com/api/chat.postMessage",
bytes.NewBufferString(data.Encode()),
)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, _ := client.Do(r)
return resp.Status
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment