This file contains hidden or 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
case socketmode.EventTypeInteractive: | |
interaction, ok := event.Data.(slack.InteractionCallback) | |
if !ok { | |
log.Printf("Could not type cast the message to a Interaction callback: %v\n", interaction) | |
continue | |
} | |
err := handleInteractionEvent(interaction, client) | |
if err != nil { | |
log.Fatal(err) |
This file contains hidden or 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
// handleIsArticleGood will trigger a Yes or No question to the initializer | |
func handleIsArticleGood(command slack.SlashCommand, client *slack.Client) (interface{}, error) { | |
// Create the attachment and assigned based on the message | |
attachment := slack.Attachment{} | |
// Create the checkbox element | |
checkbox := slack.NewCheckboxGroupsBlockElement("answer", | |
slack.NewOptionBlockObject("yes", &slack.TextBlockObject{Text: "Yes", Type: slack.MarkdownType}, &slack.TextBlockObject{Text: "Did you Enjoy it?", Type: slack.MarkdownType}), | |
slack.NewOptionBlockObject("no", &slack.TextBlockObject{Text: "No", Type: slack.MarkdownType}, &slack.TextBlockObject{Text: "Did you Dislike it?", Type: slack.MarkdownType}), | |
) |
This file contains hidden or 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
// handleSlashCommand will take a slash command and route to the appropriate function | |
func handleSlashCommand(command slack.SlashCommand, client *slack.Client) (interface{}, 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 nil, handleHelloCommand(command, client) | |
case "/was-this-article-useful": | |
return handleIsArticleGood(command, client) | |
} |
This file contains hidden or 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
// Handle Slash Commands | |
case socketmode.EventTypeSlashCommand: | |
// Just like before, type cast to the correct event type, this time a SlashEvent | |
command, ok := event.Data.(slack.SlashCommand) | |
if !ok { | |
log.Printf("Could not type cast the message to a SlashCommand: %v\n", command) | |
continue | |
} | |
// handleSlashCommand will take care of the command | |
payload, err := handleSlashCommand(command, client) |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
func main() { | |
// Load Env variables from .dot file | |
godotenv.Load(".env") | |
token := os.Getenv("SLACK_AUTH_TOKEN") | |
appToken := os.Getenv("SLACK_APP_TOKEN") | |
// Create a new client to slack by giving token | |
// Set debug to true while developing | |
// Also add a ApplicationToken option to the client |
This file contains hidden or 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
// handleEventMessage will take an event and handle it properly based on the type of event | |
func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) error { | |
switch event.Type { | |
// First we check if this is an CallbackEvent | |
case slackevents.CallbackEvent: | |
innerEvent := event.InnerEvent | |
// Yet Another Type switch on the actual Data to see if its an AppMentionEvent | |
switch ev := innerEvent.Data.(type) { | |
case *slackevents.AppMentionEvent: |
This file contains hidden or 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
// 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) |
This file contains hidden or 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
// Now we have an Events API event, but this event type can in turn be many types, so we actually need another type switch | |
err := handleEventMessage(eventsAPIEvent) | |
if err != nil { | |
// Replace with actual err handeling | |
log.Fatal(err) | |
} |
This file contains hidden or 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
// handleEventMessage will take an event and handle it properly based on the type of event | |
func handleEventMessage(event slackevents.EventsAPIEvent) error { | |
switch event.Type { | |
// First we check if this is an CallbackEvent | |
case slackevents.CallbackEvent: | |
innerEvent := event.InnerEvent | |
// Yet Another Type switch on the actual Data to see if its an AppMentionEvent | |
switch ev := innerEvent.Data.(type) { | |
case *slackevents.AppMentionEvent: |