Last active
March 15, 2023 17:59
-
-
Save gabytal/df93a1bcf91a59ccc7b73ff4a43231ff to your computer and use it in GitHub Desktop.
slack.go
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 main | |
import ( | |
"fmt" | |
"github.com/slack-go/slack" | |
) | |
func main() { | |
name = "" | |
var slackToken int | |
slackToken = "" | |
sendSlackNotification(slackToken, name) | |
} | |
func sendSlackNotification(slackToken string, name string) { | |
if name == "" { | |
panic("Error: empty 'Name' variable is not allowed, exiting..") | |
} | |
messages := map[string]string{ | |
"hello": fmt.Sprintf("Hello from %s!", name), | |
} | |
colors := map[string]string{ | |
"green": "#36a64f", | |
} | |
client := slack.New(fmt.Sprintf("xoxb-%s-4480796885458-5S7PzwdLan42HLPnw0uMOvB4", slackToken)) | |
// Create the Slack attachment that we will send to the channel | |
attachment := slack.Attachment{ | |
AuthorName: name, | |
Text: messages["hello"], | |
Color: colors["green"], | |
MarkdownIn: []string{name}, | |
Fields: []slack.AttachmentField{ | |
{Title: "Challenge accomplished"}, | |
{Value: ""}, | |
{Short: false}, | |
}} | |
channelID, timestamp, err := client.PostMessage( | |
"C04E4LUF62W", | |
slack.MsgOptionAttachments(attachment), | |
) | |
if err != nil { | |
fmt.Printf("got error while sending slack message: %s", err) | |
return | |
} | |
fmt.Printf("message successfully sent to channel %s at %s", channelID, timestamp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment