Browse Source

Script exection and replying with output now working

pull/1/head
rodley82 4 years ago
parent
commit
575a7c83d8
  1. 2
      .env.example
  2. 53
      main.go

2
.env.example

@ -1,3 +1,5 @@
SLACK_AUTH_TOKEN="" SLACK_AUTH_TOKEN=""
SLACK_CHANNEL_ID="" SLACK_CHANNEL_ID=""
SLACK_APP_TOKEN="" SLACK_APP_TOKEN=""
POWERON_SCRIPT_PATH=""
POWEROFF_SCRIPT_PATH=""

53
main.go

@ -7,7 +7,7 @@ import (
"log" "log"
"os" "os"
"strings" "strings"
"os/exec"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents" "github.com/slack-go/slack/slackevents"
@ -24,7 +24,7 @@ func main() {
// Create a new client to slack by giving token // Create a new client to slack by giving token
// Set debug to true while developing // Set debug to true while developing
// Also add a ApplicationToken option to the client // 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 // 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( socketClient := socketmode.New(
client, client,
@ -66,21 +66,27 @@ func main() {
//fmt.Println("type:", callback.Type, "callBackId:", callback.CallbackID, "aactions[0]", *callback.ActionCallback.AttachmentActions[0]) //fmt.Println("type:", callback.Type, "callBackId:", callback.CallbackID, "aactions[0]", *callback.ActionCallback.AttachmentActions[0])
action := *callback.ActionCallback.AttachmentActions[0] action := *callback.ActionCallback.AttachmentActions[0]
//fmt.Println("Value:", action.Value) //fmt.Println("Value:", action.Value)
var output string
var err error
switch action.Value { switch action.Value {
case "poweron": case "poweron":
fmt.Println("Encender!") // fmt.Println("Encender!")
output, err = executeAction(action.Value)
case "poweroff": 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: case slack.InteractionTypeMessageAction:
fmt.Println("is a message action?") //fmt.Println("is a message action?")
case slack.InteractionTypeBlockActions: case slack.InteractionTypeBlockActions:
// See https://api.slack.com/apis/connections/socket-implement#button // See https://api.slack.com/apis/connections/socket-implement#button
fmt.Println("button clicked!") //fmt.Println("button clicked!")
case slack.InteractionTypeShortcut: case slack.InteractionTypeShortcut:
case slack.InteractionTypeViewSubmission: case slack.InteractionTypeViewSubmission:
// See https://api.slack.com/apis/connections/socket-implement#modal // See https://api.slack.com/apis/connections/socket-implement#modal
@ -135,6 +141,35 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client)
return nil 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 { func replyToAction(channelName string, message string, client *slack.Client) error {
attachment := slack.Attachment{} attachment := slack.Attachment{}
attachment.Text = message attachment.Text = message
@ -194,7 +229,7 @@ func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Cli
powerOffAction.Style = "danger" powerOffAction.Style = "danger"
actions := []slack.AttachmentAction{powerOnAction, powerOffAction} actions := []slack.AttachmentAction{powerOnAction, powerOffAction}
fmt.Println(actions) //fmt.Println(actions)
attachment.Actions = actions attachment.Actions = actions
} }

Loading…
Cancel
Save