Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
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)
// 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}),
)
// 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)
}
// 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)
// 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
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
// 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:
// 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)
// 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)
}
// 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: