Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active September 7, 2022 18:12
Show Gist options
  • Save percybolmer/ae1b5548d0d50938f750fbb8a8847232 to your computer and use it in GitHub Desktop.
Save percybolmer/ae1b5548d0d50938f750fbb8a8847232 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"time"
"github.com/joho/godotenv"
"github.com/slack-go/slack"
)
func main() {
// Load Env variables from .dot file
godotenv.Load(".env")
token := os.Getenv("SLACK_AUTH_TOKEN")
channelID := os.Getenv("SLACK_CHANNEL_ID")
// Create a new client to slack by giving token
// Set debug to true while developing
client := slack.New(token, slack.OptionDebug(true))
// Create the Slack attachment that we will send to the channel
attachment := slack.Attachment{
Pretext: "Super Bot Message",
Text: "some text",
// Color Styles the Text, making it possible to have like Warnings etc.
Color: "#36a64f",
// Fields are Optional extra data!
Fields: []slack.AttachmentField{
{
Title: "Date",
Value: time.Now().String(),
},
},
}
// PostMessage will send the message away.
// First parameter is just the channelID, makes no sense to accept it
_, timestamp, err := client.PostMessage(
channelID,
// uncomment the item below to add a extra Header to the message, try it out :)
//slack.MsgOptionText("New message from bot", false),
slack.MsgOptionAttachments(attachment),
)
if err != nil {
panic(err)
}
fmt.Printf("Message sent at %s", timestamp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment