diff --git a/.env.example b/.env.example index f7af9c7..f32ac25 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ SLACK_AUTH_TOKEN="" SLACK_CHANNEL_ID="" SLACK_APP_TOKEN="" -POWERON_SCRIPT_PATH="" -POWEROFF_SCRIPT_PATH="" \ No newline at end of file +BOT_ACTION_1="poweron,Encender Server,/some/path/encender.sh" +BOT_ACTION_2="poweroff,Apagar Server,/some/path/apagar.sh" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6711a1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bot +.env \ No newline at end of file diff --git a/internal/slack/handler.go b/internal/slack/handler.go index 66d7a44..8d9a7d4 100644 --- a/internal/slack/handler.go +++ b/internal/slack/handler.go @@ -8,7 +8,6 @@ import ( "os" "os/exec" "rodleyserverbot/slack-bot/config" - "strings" "github.com/slack-go/slack" "github.com/slack-go/slack/slackevents" "github.com/slack-go/slack/socketmode" @@ -19,7 +18,7 @@ func Start() { // Set debug to true while developing // Also add a ApplicationToken option to the client client := slack.New( - config.Config.SlackAppToken, + config.Config.SlackAuthToken, slack.OptionDebug(false), slack.OptionAppLevelToken(config.Config.SlackAppToken)) // go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead @@ -49,7 +48,7 @@ func Start() { switch event.Type { // handle EventInteractive events case socketmode.EventTypeInteractive: - // fmt.Println("Recivimos un Interactive!", event) + //fmt.Println("Recivimos un Interactive!", event) callback, ok := event.Data.(slack.InteractionCallback) @@ -75,7 +74,7 @@ func Start() { } if err == nil { finalMessage := fmt.Sprintf("Chi chi chi chi amo! Output: %s", output) - replyToAction(callback.Channel.Name, finalMessage, client) + replyToAction(callback.Channel.ID, finalMessage, client) } case slack.InteractionTypeMessageAction: //fmt.Println("is a message action?") @@ -89,25 +88,27 @@ func Start() { // See https://api.slack.com/apis/connections/socket-implement#modal case slack.InteractionTypeDialogSubmission: default: - + //fmt.Println("Don't know what type it is") } // handle EventAPI events case socketmode.EventTypeEventsAPI: // The Event sent on the channel is not the same as the EventAPI events so we need to type cast it eventsAPIEvent, ok := event.Data.(slackevents.EventsAPIEvent) if !ok { - log.Printf("Could not type cast the event to the EventsAPIEvent: %v\n", event) + // log.Printf("Could not type cast the event to the EventsAPIEvent: %v\n", event) continue } // We need to send an Acknowledge to the slack server socketClient.Ack(*event.Request) // Now we have an Events API event, but this event type can in turn be many types, so we actually need another type switch - // log.Println(eventsAPIEvent) + // log.Println("EventsAPIEvent:", eventsAPIEvent) err := handleEventMessage(eventsAPIEvent, client) if err != nil { // Replace with actual err handeling - log.Fatal(err) + // log.Fatal(err) } + default: + //fmt.Println("Event received no matching TYPE:", event.Type, "event:", event) } } @@ -129,8 +130,13 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client) switch ev := innerEvent.Data.(type) { case *slackevents.AppMentionEvent: // The application has been mentioned since this Event is a Mention event - log.Println(ev) + //log.Println("App Mentioned!", ev) handleAppMentionEvent(ev, client) + case *slackevents.MessageEvent: + //log.Println("MessageEvent!", ev) + handleAppMessagedEvent(ev, client) + default: + //fmt.Println("Not handled EventMessage!:", ev) } default: return errors.New("unsupported event type") @@ -168,68 +174,86 @@ func executeAction(actionName string) (string, error) { } func replyToAction(channelName string, message string, client *slack.Client) error { + //fmt.Println("replyToAction channel:", channelName, "Message:", message) attachment := slack.Attachment{} attachment.Text = message attachment.Color = "#4af030" _, _, err := client.PostMessage(channelName, slack.MsgOptionAttachments(attachment)) if err != nil { - return fmt.Errorf("failed to post message: %w", err) + //fmt.Println("failed to post message:", err) + return err } return nil } -// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned -func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error { +func handleAppMessagedEvent(event *slackevents.MessageEvent, client *slack.Client) error { + //fmt.Println("handleAppMessagedEvent event:", event, "BotID:", event.BotID) + if 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" + 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" - // Grab the user name based on the ID of the one who mentioned the bot - user, err := client.GetUserInfo(event.User) + actions := []slack.AttachmentAction{powerOnAction, powerOffAction} + attachment.Actions = actions + + // Send the message to the channel + // The Channel is available in the event message + _, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment)) if err != nil { - return err + return fmt.Errorf("failed to post message: %w", err) } - // Check if the user said Hello to the bot - text := strings.ToLower(event.Text) + return nil +} + +// handleAppMentionEvent is used to take care of the AppMentionEvent when the bot is mentioned +func handleAppMentionEvent(event *slackevents.AppMentionEvent, client *slack.Client) error { + // fmt.Println("handleAppMentionEvent event:", event) + var err error // Create the attachment and assigned based on the message attachment := slack.Attachment{} - // Add Some default context like user who mentioned the bot - // attachment.Fields = []slack.AttachmentField{ - // { - // Title: "Date", - // Value: time.Now().String(), - // }, { - // Title: "Initializer", - // Value: user.Name, - // }, - // } attachment.CallbackID = "server_action" - if strings.Contains(text, "hello") { - // Greet the user - attachment.Text = fmt.Sprintf("Hello %s", user.Name) - attachment.Pretext = "Greetings" - attachment.Color = "#4af030" - } else { - // 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 %s?", user.Name) - 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} - //fmt.Println(actions) - attachment.Actions = actions - } + // 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" + 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} + //fmt.Println(actions) + attachment.Actions = actions + // Send the message to the channel // The Channel is available in the event message _, _, err = client.PostMessage(event.Channel, slack.MsgOptionAttachments(attachment))