Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created August 10, 2021 13:41
Show Gist options
  • Save percybolmer/17d27d00adb266595da6b91ed949163d to your computer and use it in GitHub Desktop.
Save percybolmer/17d27d00adb266595da6b91ed949163d to your computer and use it in GitHub Desktop.
// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned
func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error {
// Grab the user name based on the ID of the one who mentioned the bot
user, err := client.GetUserInfo(event.User)
if err != nil {
return err
}
// Check if the user said Hello to the bot
text := strings.ToLower(event.Text)
// Create the attachment and assigned based on the message
attachment := slack.Attachment{}
// Add Some default context like user who mentioned the bot
attachment.Fields = []slack.AttachmentField{
{
Title: "Date",
Value: time.Now().String(),
}, {
Title: "Initializer",
Value: user.Name,
},
}
if strings.Contains(text, "hello") {
// Greet the user
attachment.Text = fmt.Sprintf("Hello %s", user.Name)
attachment.Pretext = "Greetings"
attachment.Color = "#4af030"
} else {
// Send a message to the user
attachment.Text = fmt.Sprintf("How can I help you %s?", user.Name)
attachment.Pretext = "How can I be of service"
attachment.Color = "#3d3d3d"
}
// Send the message to the channel
// The Channel is available in the event message
_, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment))
if err != nil {
return fmt.Errorf("failed to post message: %w", err)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment