Created
January 4, 2019 21:23
-
-
Save uberswe/1a6b92d3def719a52fb2a5dba1cc3b5d to your computer and use it in GitHub Desktop.
Post a simple Slack message with Go using a bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Created by Markus Tenghamn @ GoPHP.io | |
// | |
// Post a simple Slack message using a bot | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
type PostMessage struct { | |
Channel string `json:"channel"` | |
Text string `json:"text"` | |
} | |
func main() { | |
// Slack endpoint for chat.postMessage | |
url := "https://slack.com/api/chat.postMessage" | |
token := "your-token" | |
postMessage := &PostMessage{ | |
Channel: "#general", | |
Text: "This is a test message from GoPHP.io", | |
} | |
b := new(bytes.Buffer) | |
err := json.NewEncoder(b).Encode(postMessage) | |
if err != nil { | |
panic(err) | |
} | |
req, err := http.NewRequest("POST", url, b) | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Authorization", "Bearer " + token) | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println("response Body:", string(body)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment