Closed trajan0x closed 2 weeks ago
To strip links from commands in the Slack bot, follow these steps:
package cmd
import (
"regexp"
)
func stripLinks(input string) string {
linkRegex := regexp.MustCompile(`<https?://[^|>]+\|([^>]+)>`)
return linkRegex.ReplaceAllString(input, "$1")
}
Action
function in commands.go
to use stripLinks
:Action: func(c *cli.Context) error {
configFile, err := os.ReadFile(c.String(fileFlag.Name))
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
var cfg config.Config
err = yaml.Unmarshal(configFile, &cfg)
if (err != nil) {
return fmt.Errorf("failed to unmarshal config file: %w", err)
}
if cfg.SlackAppToken == "" {
return fmt.Errorf("slack app token is required")
}
if cfg.SlackBotToken == "" {
return fmt.Errorf("slack bot token is required")
}
botServer := botmd.NewBot(metrics.Get(), cfg)
err = botServer.Start(c.Context)
if err != nil {
return fmt.Errorf("failed to start bot: %w", err)
}
// Example usage of stripLinks
command := "<https://example.com|example>"
strippedCommand := stripLinks(command)
fmt.Println(strippedCommand) // Output: example
return nil
},
This will ensure that any links in the commands are stripped before processing.
/contrib/opbot/cmd/commands.go /contrib/opbot/cmd/cmd.go /contrib/opbot
Add a function to strip links from commands, as these can be annoying when pasting from other messages.
Slack links are probbaly formatted w/ mkdwn or something