From 156c11fb7e4991a9f3dd09b036ba19f835d4da5b Mon Sep 17 00:00:00 2001 From: rodley82 Date: Mon, 22 Jul 2024 17:17:55 -0300 Subject: [PATCH] Handling of message with buttons with blocks to allow for more than 5 --- config/app.go | 43 ++++++++++++++---- internal/.DS_Store | Bin 0 -> 6148 bytes internal/slack/handler.go | 89 +++++++++++++++++++++++++++----------- 3 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 internal/.DS_Store diff --git a/config/app.go b/config/app.go index 7fcfa38..879be12 100644 --- a/config/app.go +++ b/config/app.go @@ -1,28 +1,30 @@ package config import ( + "encoding/csv" "fmt" + "io" "log" "os" - "encoding/csv" "strings" - "io" + "github.com/joho/godotenv" ) type ConfigEntries struct { SlackAuthToken string - SlackAppToken string - Actions []ActionScript + SlackAppToken string + Actions []ActionScript } type ActionScript struct { - Name string + Name string DisplayName string - Path string + Path string } var Config ConfigEntries + // var Actions []ActionScript func init() { @@ -38,6 +40,10 @@ func init() { Config.SlackAuthToken = os.Getenv("SLACK_AUTH_TOKEN") Config.SlackAppToken = os.Getenv("SLACK_APP_TOKEN") actionsLoaded := initalizeDefinedActions() + if actionsLoaded == 0 { + log.Println("No actions found in the environment variables") + os.Exit(1) + } log.Println("Finished loading config. Actions found:", actionsLoaded) log.Println("Actions:", Config.Actions) } @@ -47,13 +53,17 @@ func init() { // `Name,Display Name,Path` // For example: // `poweron,Encender Server,/some/script.sh` +// +// Each action name must be unique func initalizeDefinedActions() int { log.Println("initalizeDefinedActions") - var count = 1 + var count = 0 var value string var exists bool + + actionNames := make(map[string]bool) for { - envVarName := fmt.Sprintf("BOT_ACTION_%d", count) + envVarName := fmt.Sprintf("BOT_ACTION_%d", count+1) log.Println("Checking for env var:", envVarName) value, exists = os.LookupEnv(envVarName) if !exists { @@ -61,6 +71,21 @@ func initalizeDefinedActions() int { } log.Println("Action found:", value) action := actionStringToActionScript(value) + + exists = actionNames[action.Name] + if exists { + log.Println("The action", action.Name, "is already defined.") + os.Exit(1) + } else { + actionNames[action.Name] = true + } + + _, err := os.Stat(action.Path) + if err != nil { + log.Println("The specified path", action.Path, " does not exist.", err) + os.Exit(1) + } + // Actions = append(Actions, action) Config.Actions = append(Config.Actions, action) count++ @@ -68,7 +93,7 @@ func initalizeDefinedActions() int { return count } -func actionStringToActionScript(actionString string) ActionScript{ +func actionStringToActionScript(actionString string) ActionScript { var action ActionScript var csvArr []string var err error diff --git a/internal/.DS_Store b/internal/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ed9df2dcc156e6e6b72ab1cf17c73b755ebb21a5 GIT binary patch literal 6148 zcmeHKI|>3Z5S{S@f{mqRuHX%V=n1@lqF|vYC|YmjxjdS0KFzY&X`#G<$x9~l67q_j z9TCy_Z8sB{h{yt#fNaGY_tuP&$K`Lyrkz6%(4C|5bkMz$@!?a-(I z6`%rCfC^B7Pb-iWb~OIu`L@D>0#K-dj)?GNs z1R?^{paO%c* 0 && i%5 == 0 { + actionBlock := slack.NewActionBlock("", buttons...) + blocks = append(blocks, actionBlock) + buttons = []slack.BlockElement{} + } + buttonElement := slack.NewButtonBlockElement(action.Name, action.Name, slack.NewTextBlockObject("plain_text", action.DisplayName, false, false)) + buttons = append(buttons, buttonElement) + } + + if len(buttons) > 0 { + actionBlock := slack.NewActionBlock("", buttons...) + blocks = append(blocks, actionBlock) + } + + message := slack.MsgOptionBlocks(blocks...) + return message, nil + +} + +func handleAppMessagedEvent(event *slackevents.MessageEvent, channel string, client *slack.Client) error { if event != nil && event.BotID != "" { // We're not interested in messages from ourselves or other bots return nil } - attachment := slack.Attachment{} var err error - attachment.CallbackID = "server_action" - // Send a message to the user - attachment.Text = "Por el momento esto es lo que se hacer" - attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") - attachment.Color = "#3d3d3d" - actions := getAttachmentButtons() - attachment.Actions = actions + message, err := buildResponseMessage() + if err != nil { + return fmt.Errorf("failed to build response message: %w", err) + } // Send the message to the channel // The Channel is available in the event message - _, _, err = client.PostMessage(channel, slack.MsgOptionAttachments(attachment)) + _, _, err = client.PostMessage(channel, message) if err != nil { return fmt.Errorf("failed to post message: %w", err) } @@ -234,7 +273,7 @@ func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Cli // Send a message to the user attachment.Text = "Por el momento esto es lo que se hacer" - attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") + attachment.Pretext = "Como te puedo ayudar?" attachment.Color = "#3d3d3d" actions := getAttachmentButtons() attachment.Actions = actions -- 2.30.2