Edited at

GoでSlack Incoming WebHooksを使ってみる

More than 1 year has passed since last update.

Go言語の勉強も含めてSlackにPostしてみた。

https://{{team}}.slack.com/services/new/incoming-webhook

Channel選択してAdd

PostするGoを書く


slack.go

package main

import (
"strings"
"fmt"
"net/http"
"net/url"
)

func main() {
apiUrl := "https://{{team}}.slack.com"
path := "/services/hooks/incoming-webhook"
u, _ := url.ParseRequestURI(apiUrl)
u.Path = path

query := url.Values{}
query.Set("token", "**********")
u.RawQuery = query.Encode()

urlStr := fmt.Sprintf("%v", u)

data := url.Values{}
data.Set("payload", "{\"text\": \"元気ですか?!\"}")

client := &http.Client{}
r, _ := http.NewRequest("POST", urlStr, strings.NewReader(data.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, _ := client.Do(r)
fmt.Println(resp.Status)
}


エラーは全部無視してしまった!