Skip to content

Instantly share code, notes, and snippets.

@yukihirai0505
Created February 18, 2019 10:15
Show Gist options
  • Save yukihirai0505/6eaa69321f40648ce5ee8637c396a2e0 to your computer and use it in GitHub Desktop.
Save yukihirai0505/6eaa69321f40648ce5ee8637c396a2e0 to your computer and use it in GitHub Desktop.
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