synapsecns / sanguine

Synapse Monorepo
MIT License
35 stars 25 forks source link

[opbpot] strip links from commands #2783

Closed trajan0x closed 2 weeks ago

trajan0x commented 2 weeks ago

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

greptile-apps[bot] commented 2 weeks ago

To strip links from commands in the Slack bot, follow these steps:

  1. Add a function to strip links:
package cmd

import (
    "regexp"
)

func stripLinks(input string) string {
    linkRegex := regexp.MustCompile(`<https?://[^|>]+\|([^>]+)>`)
    return linkRegex.ReplaceAllString(input, "$1")
}
  1. Modify the 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.

References

/contrib/opbot/cmd/commands.go /contrib/opbot/cmd/cmd.go /contrib/opbot

Ask Greptile