From 373949adf83693676466a586d01c58c242017f06 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Mon, 20 Jun 2022 02:01:45 -0300 Subject: [PATCH] Improved command output using MD and now the actions are 100% configurable --- internal/slack/handler.go | 75 ++++++++++++--------------------------- 1 file changed, 22 insertions(+), 53 deletions(-) diff --git a/internal/slack/handler.go b/internal/slack/handler.go index 8d9a7d4..b7c5b7d 100644 --- a/internal/slack/handler.go +++ b/internal/slack/handler.go @@ -64,16 +64,9 @@ func Start() { //fmt.Println("Value:", action.Value) var output string var err error - switch action.Value { - case "poweron": - // fmt.Println("Encender!") - output, err = executeAction(action.Value) - case "poweroff": - // fmt.Println("Apagar!") - output, err = executeAction(action.Value) - } + output, err = executeAction(action.Value) if err == nil { - finalMessage := fmt.Sprintf("Chi chi chi chi amo! Output: %s", output) + finalMessage := fmt.Sprintf("Chi chi chi chi amo!\n*Output*\n```\n%s\n```", output) replyToAction(callback.Channel.ID, finalMessage, client) } case slack.InteractionTypeMessageAction: @@ -145,10 +138,11 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) } func executeAction(actionName string) (string, error) { + fmt.Println("executeAction actionName:", actionName, "actions:", config.Config.Actions) var output_str string - switch actionName { - case "poweron": - cmd := exec.Command("/bin/bash", os.Getenv("POWERON_SCRIPT_PATH")) + for _, action := range config.Config.Actions { + if action.Name == actionName { + cmd := exec.Command("/bin/bash", action.Path) output, err := cmd.Output() if err != nil { @@ -156,20 +150,8 @@ func executeAction(actionName string) (string, error) { return "", err } output_str = string(output) - //fmt.Println("output:", output_str) - // return output - case "poweroff": - cmd := exec.Command("/bin/bash", os.Getenv("POWEROFF_SCRIPT_PATH")) - output, err := cmd.Output() - - if err != nil { - fmt.Printf("error %s", err) - return "", err - } - output_str = string(output) - //fmt.Println("output:", output_str) + } } - return output_str, nil } @@ -186,6 +168,19 @@ func replyToAction(channelName string, message string, client *slack.Client) err return nil } +func getAttachmentButtons() []slack.AttachmentAction{ + var actions []slack.AttachmentAction + for _, action := range config.Config.Actions { + slackAction := slack.AttachmentAction{} + slackAction.Name = action.Name + slackAction.Value = action.Name + slackAction.Text = action.DisplayName + slackAction.Type = "button" + actions=append(actions, slackAction) + } + return actions +} + func handleAppMessagedEvent(event *slackevents.MessageEvent, client *slack.Client) error { //fmt.Println("handleAppMessagedEvent event:", event, "BotID:", event.BotID) if event.BotID != "" { @@ -199,20 +194,7 @@ func handleAppMessagedEvent(event *slackevents.MessageEvent, client *slack.Clien attachment.Text = "Por el momento esto es lo que se hacer" attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") attachment.Color = "#3d3d3d" - powerOnAction := slack.AttachmentAction{} - powerOnAction.Name = "poweron" - powerOnAction.Text = "Encender" - powerOnAction.Value = "poweron" - powerOnAction.Type = "button" - - powerOffAction := slack.AttachmentAction{} - powerOffAction.Name = "poweroff" - powerOffAction.Text = "Apagar" - powerOffAction.Value = "poweroff" - powerOffAction.Type = "button" - powerOffAction.Style = "danger" - - actions := []slack.AttachmentAction{powerOnAction, powerOffAction} + actions := getAttachmentButtons() attachment.Actions = actions // Send the message to the channel @@ -237,20 +219,7 @@ func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Cli attachment.Text = "Por el momento esto es lo que se hacer" attachment.Pretext = fmt.Sprintf("Como te puedo ayudar?") attachment.Color = "#3d3d3d" - powerOnAction := slack.AttachmentAction{} - powerOnAction.Name = "poweron" - powerOnAction.Text = "Encender" - powerOnAction.Value = "poweron" - powerOnAction.Type = "button" - - powerOffAction := slack.AttachmentAction{} - powerOffAction.Name = "poweroff" - powerOffAction.Text = "Apagar" - powerOffAction.Value = "poweroff" - powerOffAction.Type = "button" - powerOffAction.Style = "danger" - - actions := []slack.AttachmentAction{powerOnAction, powerOffAction} + actions := getAttachmentButtons() //fmt.Println(actions) attachment.Actions = actions