LoginSignup
2
0

More than 3 years have passed since last update.

go langでslackと連携 api

Last updated at Posted at 2019-12-22
main.go
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)

var (
    // change token
    IncomingUrl string = "https://hooks.slack.com/services/hogehoge/hogehoge"
)

type Slack struct {
    Text       string `json:"text"`
    Username   string `json:"username"`
    Icon_emoji string `json:"icon_emoji"`
    Icon_url   string `json:"icon_url"`
    Channel    string `json:"channel"`
}

func main() {
    arg := "nekosasa"

    params, _ := json.Marshal(Slack{ //json形式のbinary 数字がいっぱい並んでる

        fmt.Sprintf("%s", arg),
        "arigatou",
        "dossy_bot",
        "http://www.icons101.com/icons/66/NuoveXT_by_Alexandre_Moore/128/slackware.png",
        "#test"})
    //paramsがjson形式の[]uint8のbinary string(params)で中身は見れる
    resp, _ := http.PostForm( //postを整形して実行まで行う
        IncomingUrl,
        url.Values{"payload": {string(params)}},
    )

    body, _ := ioutil.ReadAll(resp.Body)
    //ioutil.Readallはerrがないか確認するもの
    defer resp.Body.Close()
    //respはreadしたらちゃんとcloseすべきである

    println(string(body)) //err == nilならreturn ok
}

ほぼ自分用

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0