diff --git a/.env.example b/.env.example index 0b181ce..f7af9c7 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ SLACK_AUTH_TOKEN="" SLACK_CHANNEL_ID="" SLACK_APP_TOKEN="" +POWERON_SCRIPT_PATH="" +POWEROFF_SCRIPT_PATH="" \ No newline at end of file diff --git a/main.go b/main.go index a9f6aab..bffbf0c 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "log" "os" "strings" - + "os/exec" "github.com/joho/godotenv" "github.com/slack-go/slack" "github.com/slack-go/slack/slackevents" @@ -24,7 +24,7 @@ func main() { // Create a new client to slack by giving token // Set debug to true while developing // Also add a ApplicationToken option to the client - client := slack.New(token, slack.OptionDebug(true), slack.OptionAppLevelToken(appToken)) + client := slack.New(token, slack.OptionDebug(false), slack.OptionAppLevelToken(appToken)) // go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead socketClient := socketmode.New( client, @@ -66,21 +66,27 @@ func main() { //fmt.Println("type:", callback.Type, "callBackId:", callback.CallbackID, "aactions[0]", *callback.ActionCallback.AttachmentActions[0]) action := *callback.ActionCallback.AttachmentActions[0] //fmt.Println("Value:", action.Value) + var output string + var err error switch action.Value { case "poweron": - fmt.Println("Encender!") + // fmt.Println("Encender!") + output, err = executeAction(action.Value) case "poweroff": - fmt.Println("Apagar!") + // fmt.Println("Apagar!") + output, err = executeAction(action.Value) + } + if err == nil { + finalMessage := fmt.Sprintf("Chi chi chi chi amo! Output: %s", output) + replyToAction(callback.Channel.Name, finalMessage, client) } - - replyToAction(callback.Channel.Name, "Chi chi chi chi amo!", client) case slack.InteractionTypeMessageAction: - fmt.Println("is a message action?") + //fmt.Println("is a message action?") case slack.InteractionTypeBlockActions: // See https://api.slack.com/apis/connections/socket-implement#button - fmt.Println("button clicked!") + //fmt.Println("button clicked!") case slack.InteractionTypeShortcut: case slack.InteractionTypeViewSubmission: // See https://api.slack.com/apis/connections/socket-implement#modal @@ -135,6 +141,35 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) return nil } +func executeAction(actionName string) (string, error) { + var output_str string + switch actionName { + case "poweron": + cmd := exec.Command("/bin/bash", os.Getenv("POWERON_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 + 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 +} + func replyToAction(channelName string, message string, client *slack.Client) error { attachment := slack.Attachment{} attachment.Text = message @@ -194,7 +229,7 @@ func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Cli powerOffAction.Style = "danger" actions := []slack.AttachmentAction{powerOnAction, powerOffAction} - fmt.Println(actions) + //fmt.Println(actions) attachment.Actions = actions }