Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created August 10, 2021 21:06
Show Gist options
  • Save percybolmer/747b0787dc892de7c32cc393df1a08ea to your computer and use it in GitHub Desktop.
Save percybolmer/747b0787dc892de7c32cc393df1a08ea to your computer and use it in GitHub Desktop.
// handleSlashCommand will take a slash command and route to the appropriate function
func handleSlashCommand(command slack.SlashCommand, client *slack.Client) error {
// We need to switch depending on the command
switch command.Command {
case "/hello":
// This was a hello command, so pass it along to the proper function
return handleHelloCommand(command, client)
}
return nil
}
// handleHelloCommand will take care of /hello submissions
func handleHelloCommand(command slack.SlashCommand, client *slack.Client) error {
// The Input is found in the text field so
// 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: command.UserName,
},
}
// Greet the user
attachment.Text = fmt.Sprintf("Hello %s", command.Text)
attachment.Color = "#4af030"
// Send the message to the channel
// The Channel is available in the command.ChannelID
_, _, err := client.PostMessage(command.ChannelID, 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