DEV Community

D
D

Posted on

1 2

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

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay