DEV Community

D
D

Posted on

GoでSlackIncomingWebHooksを使ってみる

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

https://{team}.slack.com/services/new/incoming-webhook
Channel選択してAdd

Postする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)
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)