Browse Source

Merge pull request '202407 - Handling of message with buttons with blocks to allow for more than 5' (#1) from 2024_more_than_5_actions into master

Reviewed-on: http://git.rodley.ar/rodo/go-slackbot/pulls/1
master
rodo 1 year ago
parent
commit
4b1897ceda
  1. 33
      config/app.go
  2. BIN
      internal/.DS_Store
  3. 77
      internal/slack/handler.go

33
config/app.go

@ -1,12 +1,13 @@
package config
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"encoding/csv"
"strings"
"io"
"github.com/joho/godotenv"
)
@ -23,6 +24,7 @@ type ActionScript struct {
}
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++

BIN
internal/.DS_Store

Binary file not shown.

77
internal/slack/handler.go

@ -6,9 +6,10 @@ import (
"fmt"
"log"
"os"
"time"
"os/exec"
"rodleyserverbot/slack-bot/config"
"time"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
@ -58,7 +59,9 @@ func Start() {
socketClient.Ack(*event.Request)
switch callback.Type {
case slack.InteractionTypeInteractionMessage:
action := *callback.ActionCallback.AttachmentActions[0]
case slack.InteractionTypeMessageAction:
case slack.InteractionTypeBlockActions:
action := *callback.ActionCallback.BlockActions[0]
var output string
var err error
@ -68,12 +71,13 @@ func Start() {
finalMessage := fmt.Sprintf("Resultado*\n```\n%s\n```", output)
replyToAction(callback.Channel.ID, finalMessage, client)
handleAppMessagedEvent(nil, callback.Channel.ID, client)
err := handleAppMessagedEvent(nil, callback.Channel.ID, client)
if err != nil {
log.Println("Error handling event:", err)
}
} else {
log.Println("Error executing action", err)
}
case slack.InteractionTypeMessageAction:
case slack.InteractionTypeBlockActions:
// See https://api.slack.com/apis/connections/socket-implement#button
case slack.InteractionTypeShortcut:
case slack.InteractionTypeViewSubmission:
@ -97,6 +101,7 @@ func Start() {
if err != nil {
// Replace with actual err handeling
// log.Fatal(err)
log.Println("Error handling event:", err)
}
default:
}
@ -120,10 +125,16 @@ func handleEventMessage(event slackevents.EventsAPIEvent, client *slack.Client)
case *slackevents.AppMentionEvent:
// The application has been mentioned since this Event is a Mention event
//log.Println("App Mentioned!", ev)
handleAppMentionEvent(ev, client)
err := handleAppMentionEvent(ev, client)
if err != nil {
return fmt.Errorf("failed to handle app mention event: %w", err)
}
case *slackevents.MessageEvent:
//log.Println("MessageEvent!", ev)
handleAppMessagedEvent(ev, ev.Channel, client)
err := handleAppMessagedEvent(ev, ev.Channel, client)
if err != nil {
return fmt.Errorf("failed to handle app message event: %w", err)
}
default:
}
default:
@ -201,24 +212,52 @@ func getAttachmentButtons() []slack.AttachmentAction{
return actions
}
func buildResponseMessage() (slack.MsgOption, error) {
var blocks []slack.Block
sectionBlock := slack.NewSectionBlock(
slack.NewTextBlockObject("mrkdwn", "Esto es lo que se hacer:", false, false),
nil,
nil,
)
blocks = append(blocks, sectionBlock)
var buttons []slack.BlockElement
// Slack only allows 5 buttons per action block
for i, action := range config.Config.Actions {
if i > 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

Loading…
Cancel
Save