Last active
November 28, 2018 11:31
-
-
Save johnmanjiro13/e3cbba01c8b5295ddf5d644273ed719b to your computer and use it in GitHub Desktop.
This file contains 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 slack | |
import ( | |
"bytes" | |
"encoding/json" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
) | |
type payload struct { | |
Channel string `json:"channel"` | |
Text string `json:"text"` | |
Username string `json:"username"` | |
IconEmoji string `json:"icon_emoji"` | |
AsUser bool `json:"as_user"` | |
} | |
func SendSlack(message string) { | |
req, err := createRequest(message) | |
if err != nil { | |
log.Printf(err) | |
os.Exit(1) | |
} | |
client := http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Printf(err) | |
os.Exit(1) | |
} | |
defer resp.Body.Close() | |
} | |
func createRequest(message string) (*http.Request, error) { | |
channelID := os.Getenv("SLACK_CHANNEL") | |
endpoint := os.Getenv("SLACK_ENDPOINT") | |
data := payload{ | |
channelID, | |
message, | |
"botName", | |
":ghost:", | |
false, | |
} | |
jsonStr, err := json.Marshal(data) | |
if err != nil { | |
return nil, err | |
} | |
values := url.Values{} | |
values.Set("payload", string(jsonStr)) | |
req, err := http.NewRequest( | |
"POST", | |
endpoint, | |
bytes.NewBufferString(values.Encode()), | |
) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
return req, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment